一、概述:为什么日志是系统的"黑匣子"
在 Linux 系统中,日志是运维和安全工作的核心基础设施。无论是排查故障、追踪安全事件,还是满足合规审计要求,日志都是不可替代的第一手资料。现代 Linux 的日志体系并非单一组件,而是一个分层协作的生态系统,涉及内核、用户空间守护进程、配置文件、存储管理和分析工具等多个层次。
本文将系统性地介绍 Linux 日志体系的完整架构,涵盖传统 syslog 协议、systemd-journald、rsyslog、auditd 安全审计、日志轮转(logrotate)以及现代集中化日志架构。
二、整体架构:五层日志体系
#mermaid-svg-oFGxWSSQZENae4n9{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-oFGxWSSQZENae4n9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-oFGxWSSQZENae4n9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-oFGxWSSQZENae4n9 .error-icon{fill:#552222;}#mermaid-svg-oFGxWSSQZENae4n9 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-oFGxWSSQZENae4n9 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-oFGxWSSQZENae4n9 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-oFGxWSSQZENae4n9 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-oFGxWSSQZENae4n9 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-oFGxWSSQZENae4n9 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-oFGxWSSQZENae4n9 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-oFGxWSSQZENae4n9 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-oFGxWSSQZENae4n9 .marker.cross{stroke:#333333;}#mermaid-svg-oFGxWSSQZENae4n9 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-oFGxWSSQZENae4n9 p{margin:0;}#mermaid-svg-oFGxWSSQZENae4n9 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-oFGxWSSQZENae4n9 .cluster-label text{fill:#333;}#mermaid-svg-oFGxWSSQZENae4n9 .cluster-label span{color:#333;}#mermaid-svg-oFGxWSSQZENae4n9 .cluster-label span p{background-color:transparent;}#mermaid-svg-oFGxWSSQZENae4n9 .label text,#mermaid-svg-oFGxWSSQZENae4n9 span{fill:#333;color:#333;}#mermaid-svg-oFGxWSSQZENae4n9 .node rect,#mermaid-svg-oFGxWSSQZENae4n9 .node circle,#mermaid-svg-oFGxWSSQZENae4n9 .node ellipse,#mermaid-svg-oFGxWSSQZENae4n9 .node polygon,#mermaid-svg-oFGxWSSQZENae4n9 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-oFGxWSSQZENae4n9 .rough-node .label text,#mermaid-svg-oFGxWSSQZENae4n9 .node .label text,#mermaid-svg-oFGxWSSQZENae4n9 .image-shape .label,#mermaid-svg-oFGxWSSQZENae4n9 .icon-shape .label{text-anchor:middle;}#mermaid-svg-oFGxWSSQZENae4n9 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-oFGxWSSQZENae4n9 .rough-node .label,#mermaid-svg-oFGxWSSQZENae4n9 .node .label,#mermaid-svg-oFGxWSSQZENae4n9 .image-shape .label,#mermaid-svg-oFGxWSSQZENae4n9 .icon-shape .label{text-align:center;}#mermaid-svg-oFGxWSSQZENae4n9 .node.clickable{cursor:pointer;}#mermaid-svg-oFGxWSSQZENae4n9 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-oFGxWSSQZENae4n9 .arrowheadPath{fill:#333333;}#mermaid-svg-oFGxWSSQZENae4n9 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-oFGxWSSQZENae4n9 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-oFGxWSSQZENae4n9 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-oFGxWSSQZENae4n9 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-oFGxWSSQZENae4n9 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-oFGxWSSQZENae4n9 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-oFGxWSSQZENae4n9 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-oFGxWSSQZENae4n9 .cluster text{fill:#333;}#mermaid-svg-oFGxWSSQZENae4n9 .cluster span{color:#333;}#mermaid-svg-oFGxWSSQZENae4n9 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-oFGxWSSQZENae4n9 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-oFGxWSSQZENae4n9 rect.text{fill:none;stroke-width:0;}#mermaid-svg-oFGxWSSQZENae4n9 .icon-shape,#mermaid-svg-oFGxWSSQZENae4n9 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-oFGxWSSQZENae4n9 .icon-shape p,#mermaid-svg-oFGxWSSQZENae4n9 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-oFGxWSSQZENae4n9 .icon-shape .label rect,#mermaid-svg-oFGxWSSQZENae4n9 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-oFGxWSSQZENae4n9 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-oFGxWSSQZENae4n9 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-oFGxWSSQZENae4n9 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 分析与收集层
存储层
应用层
系统服务层
内核层
内核消息 kmsg
auditd 内核钩子
系统调用审计
systemd-journald
结构化二进制日志
rsyslog
传统文本日志
syslog-ng
高级日志路由
auditd
安全审计守护进程
stdout/stderr
systemd 服务
syslog() API
传统应用
直接文件写入
应用自定义日志
/var/log/journal/
journald 二进制存储
/var/log/messages
/var/log/secure
rsyslog 文本文件
/var/log/audit/
auditd 审计日志
journalctl 查询
grep/awk/tail 分析
ELK / Loki / Grafana
SIEM / 安全分析
三、核心组件详解
3.1 Syslog 协议:日志世界的"通用语言"
Syslog 是 Unix/Linux 世界的事实标准日志协议,由 RFC 5424 定义。它通过两个维度对日志进行分类:
Facility(设施/来源)
| Facility | 编号 | 说明 |
|---|---|---|
kern |
0 | 内核消息 |
user |
1 | 用户级程序 |
mail |
2 | 邮件系统 |
daemon |
3 | 系统守护进程 |
auth |
4 | 认证/授权消息 |
syslog |
5 | syslog 守护进程自身 |
lpr |
6 | 打印机子系统 |
news |
7 | 新闻子系统 |
uucp |
8 | UUCP 子系统 |
cron |
9 | 定时任务 |
authpriv |
10 | 私有认证信息 |
ftp |
11 | FTP 守护进程 |
ntp |
12 | NTP 子系统 |
local0 ~ local7 |
16~23 | 用户自定义设施 |
Priority(优先级/严重级别)
| 级别 | 数值 | 名称 | 说明 |
|---|---|---|---|
| 0 | Emergency | 紧急 | 系统不可用 |
| 1 | Alert | 警报 | 必须立即处理 |
| 2 | Critical | 严重 | 严重情况 |
| 3 | Error | 错误 | 一般错误 |
| 4 | Warning | 警告 | 潜在问题 |
| 5 | Notice | 注意 | 正常但重要的事件 |
| 6 | Info | 信息 | 一般信息 |
| 7 | Debug | 调试 | 调试信息 |
Priority 值计算公式 :PRI = Facility × 8 + Severity。例如,内核的 Emergency 消息优先级为 0×8+0=0,用户程序的 Notice 消息为 1×8+5=13。
3.2 systemd-journald:现代 Linux 的日志中枢
systemd-journald 是 systemd 套件中的日志组件,自 systemd 成为主流 init 系统后,它已成为大多数 Linux 发行版的默认日志收集器。
核心特性
- 二进制格式 :日志以结构化二进制形式存储在
/var/log/journal/(持久化)或/run/log/journal/(运行时),无法直接用cat读取 - 丰富元数据:每条日志附带 PID、UID、GID、_COMM(命令名)、_UNIT(systemd 单元名)、_BOOT_ID(启动标识)等字段
- 启动早期日志:能捕获 initrd 和内核启动阶段的日志,这是传统 syslog 无法做到的
- 自动轮转:内置存储管理,自动压缩和清理旧日志
- 单一查询入口 :通过
journalctl命令统一查询
常用 journalctl 命令
bash
# 查看所有日志
journalctl
# 查看特定服务日志
journalctl -u nginx.service
# 查看内核日志
journalctl -k
# 实时跟踪
journalctl -f
# 查看上次启动的日志
journalctl -b -1
# 按时间范围查询
journalctl --since "2024-01-01 00:00:00" --until "2024-01-02 00:00:00"
# 按优先级过滤
journalctl -p err
# 查看特定进程
journalctl _PID=1234
# 查看特定用户
journalctl _UID=1000
journald 配置
配置文件位于 /etc/systemd/journald.conf:
ini
[Journal]
Storage=persistent # persistent/auto/volatile/none
SystemMaxUse=500M # 最大占用空间
SystemMaxFileSize=100M # 单个日志文件上限
MaxRetentionSec=1month # 最大保留时间
Compress=yes # 启用压缩
ForwardToSyslog=yes # 转发到 syslog
ForwardToWall=yes # 转发到控制台
3.3 rsyslog:传统但强大的日志处理器
rsyslog 是 sysklogd 的继任者,是绝大多数 Linux 发行版的默认 syslog 守护进程。它负责将日志从 journald 接收后,按照规则分类、格式化并写入 /var/log/ 下的各个文件。
日志数据流
远程日志服务器 logrotate /var/log/*.log rsyslog systemd-journald 应用程序 远程日志服务器 logrotate /var/log/*.log rsyslog systemd-journald 应用程序 #mermaid-svg-9KxznZIOLlXayrMK{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-9KxznZIOLlXayrMK .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-9KxznZIOLlXayrMK .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-9KxznZIOLlXayrMK .error-icon{fill:#552222;}#mermaid-svg-9KxznZIOLlXayrMK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9KxznZIOLlXayrMK .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-9KxznZIOLlXayrMK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9KxznZIOLlXayrMK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9KxznZIOLlXayrMK .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-9KxznZIOLlXayrMK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9KxznZIOLlXayrMK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9KxznZIOLlXayrMK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9KxznZIOLlXayrMK .marker.cross{stroke:#333333;}#mermaid-svg-9KxznZIOLlXayrMK svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9KxznZIOLlXayrMK p{margin:0;}#mermaid-svg-9KxznZIOLlXayrMK .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-9KxznZIOLlXayrMK text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-9KxznZIOLlXayrMK .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-9KxznZIOLlXayrMK .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-9KxznZIOLlXayrMK .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-9KxznZIOLlXayrMK #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-9KxznZIOLlXayrMK .sequenceNumber{fill:white;}#mermaid-svg-9KxznZIOLlXayrMK #sequencenumber{fill:#333;}#mermaid-svg-9KxznZIOLlXayrMK #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-9KxznZIOLlXayrMK .messageText{fill:#333;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-9KxznZIOLlXayrMK .labelText,#mermaid-svg-9KxznZIOLlXayrMK .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .loopText,#mermaid-svg-9KxznZIOLlXayrMK .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-9KxznZIOLlXayrMK .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-9KxznZIOLlXayrMK .noteText,#mermaid-svg-9KxznZIOLlXayrMK .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-9KxznZIOLlXayrMK .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-9KxznZIOLlXayrMK .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-9KxznZIOLlXayrMK .actorPopupMenu{position:absolute;}#mermaid-svg-9KxznZIOLlXayrMK .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-9KxznZIOLlXayrMK .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-9KxznZIOLlXayrMK .actor-man circle,#mermaid-svg-9KxznZIOLlXayrMK line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-9KxznZIOLlXayrMK :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} messages, secure, cron, maillog... stdout/stderr / syslog() 结构化存储 带元数据(PID, UID, 时间戳) 通过 /run/systemd/journal/syslog 转发 过滤/分类/格式化 写入文本日志文件 定期轮转/压缩/删除 TCP/UDP/TLS 转发
rsyslog 配置
主配置文件 /etc/rsyslog.conf 和 /etc/rsyslog.d/*.conf:
bash
# 基本语法:selector action
# selector = facility.priority
# action = 文件路径/远程主机/数据库等
# 将所有 info 级别及以上日志写入 messages
*.info;mail.none;authpriv.none;cron.none /var/log/messages
# 认证日志
authpriv.* /var/log/secure
# 邮件日志
mail.* -/var/log/maillog
# cron 日志
cron.* /var/log/cron
# 紧急消息广播给所有用户
*.emerg :omusrmsg:*
# 远程日志转发(TCP 加密)
*.* action(type="omfwd" target="log.example.com" port="514" protocol="tcp" StreamDriver="gtls" StreamDriverMode="1")
/var/log 目录标准结构
根据 FHS(Filesystem Hierarchy Standard),/var/log 是系统可变日志文件的标准存放位置。
| 文件/目录 | 发行版 | 内容 |
|---|---|---|
/var/log/messages |
RHEL/CentOS/Fedora | 一般系统日志 |
/var/log/syslog |
Debian/Ubuntu | 一般系统日志 |
/var/log/secure |
RHEL | 认证、安全相关 |
/var/log/auth.log |
Debian | 认证、sudo、SSH |
/var/log/kern.log |
通用 | 内核消息 |
/var/log/cron |
通用 | 定时任务日志 |
/var/log/maillog |
RHEL | 邮件系统日志 |
/var/log/mail.log |
Debian | 邮件系统日志 |
/var/log/dmesg |
通用 | 硬件/内核启动信息 |
/var/log/boot.log |
通用 | 启动过程日志 |
/var/log/audit/ |
通用 | auditd 审计日志 |
/var/log/journal/ |
systemd | journald 二进制存储 |
3.4 journald vs rsyslog:互补而非竞争
#mermaid-svg-7hBUhfmLNJsnuXTX{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7hBUhfmLNJsnuXTX .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7hBUhfmLNJsnuXTX .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7hBUhfmLNJsnuXTX .error-icon{fill:#552222;}#mermaid-svg-7hBUhfmLNJsnuXTX .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7hBUhfmLNJsnuXTX .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7hBUhfmLNJsnuXTX .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7hBUhfmLNJsnuXTX .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7hBUhfmLNJsnuXTX .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7hBUhfmLNJsnuXTX .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7hBUhfmLNJsnuXTX .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7hBUhfmLNJsnuXTX .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7hBUhfmLNJsnuXTX .marker.cross{stroke:#333333;}#mermaid-svg-7hBUhfmLNJsnuXTX svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7hBUhfmLNJsnuXTX p{margin:0;}#mermaid-svg-7hBUhfmLNJsnuXTX .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-7hBUhfmLNJsnuXTX .cluster-label text{fill:#333;}#mermaid-svg-7hBUhfmLNJsnuXTX .cluster-label span{color:#333;}#mermaid-svg-7hBUhfmLNJsnuXTX .cluster-label span p{background-color:transparent;}#mermaid-svg-7hBUhfmLNJsnuXTX .label text,#mermaid-svg-7hBUhfmLNJsnuXTX span{fill:#333;color:#333;}#mermaid-svg-7hBUhfmLNJsnuXTX .node rect,#mermaid-svg-7hBUhfmLNJsnuXTX .node circle,#mermaid-svg-7hBUhfmLNJsnuXTX .node ellipse,#mermaid-svg-7hBUhfmLNJsnuXTX .node polygon,#mermaid-svg-7hBUhfmLNJsnuXTX .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7hBUhfmLNJsnuXTX .rough-node .label text,#mermaid-svg-7hBUhfmLNJsnuXTX .node .label text,#mermaid-svg-7hBUhfmLNJsnuXTX .image-shape .label,#mermaid-svg-7hBUhfmLNJsnuXTX .icon-shape .label{text-anchor:middle;}#mermaid-svg-7hBUhfmLNJsnuXTX .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-7hBUhfmLNJsnuXTX .rough-node .label,#mermaid-svg-7hBUhfmLNJsnuXTX .node .label,#mermaid-svg-7hBUhfmLNJsnuXTX .image-shape .label,#mermaid-svg-7hBUhfmLNJsnuXTX .icon-shape .label{text-align:center;}#mermaid-svg-7hBUhfmLNJsnuXTX .node.clickable{cursor:pointer;}#mermaid-svg-7hBUhfmLNJsnuXTX .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-7hBUhfmLNJsnuXTX .arrowheadPath{fill:#333333;}#mermaid-svg-7hBUhfmLNJsnuXTX .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7hBUhfmLNJsnuXTX .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7hBUhfmLNJsnuXTX .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7hBUhfmLNJsnuXTX .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-7hBUhfmLNJsnuXTX .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7hBUhfmLNJsnuXTX .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-7hBUhfmLNJsnuXTX .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7hBUhfmLNJsnuXTX .cluster text{fill:#333;}#mermaid-svg-7hBUhfmLNJsnuXTX .cluster span{color:#333;}#mermaid-svg-7hBUhfmLNJsnuXTX div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7hBUhfmLNJsnuXTX .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-7hBUhfmLNJsnuXTX rect.text{fill:none;stroke-width:0;}#mermaid-svg-7hBUhfmLNJsnuXTX .icon-shape,#mermaid-svg-7hBUhfmLNJsnuXTX .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7hBUhfmLNJsnuXTX .icon-shape p,#mermaid-svg-7hBUhfmLNJsnuXTX .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-7hBUhfmLNJsnuXTX .icon-shape .label rect,#mermaid-svg-7hBUhfmLNJsnuXTX .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7hBUhfmLNJsnuXTX .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-7hBUhfmLNJsnuXTX .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-7hBUhfmLNJsnuXTX :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 互补共存
imjournal 读取
rsyslog
纯文本格式
标准 syslog 字段
facility, priority
grep/awk/less 分析
logrotate 外部管理
网络转发
TCP/UDP/TLS/RELP
数据库/消息队列
输出
systemd-journald
二进制格式
结构化元数据
PID, UID, _COMM, _UNIT
journalctl 查询
自动轮转/压缩
启动早期日志
initrd, kmsg
| 特性 | journald | rsyslog |
|---|---|---|
| 格式 | 结构化二进制 | 纯文本 |
| 查询 | journalctl |
grep/awk/tail |
| 元数据 | 丰富(PID、UID、UNIT 等) | 标准 syslog 字段 |
| 启动日志 | 支持 initrd/kmsg | 不支持早期启动 |
| 网络转发 | 有限 | 强大(TCP/UDP/TLS/RELP) |
| 外部集成 | 依赖 syslog 转发 | 直接支持数据库、消息队列 |
| 轮转管理 | 内置自动管理 | 依赖 logrotate |
| 企业场景 | 单机排查 | 集中化日志架构 |
最佳实践 :两者共存,journald 负责收集和本地快速查询,rsyslog 负责持久化文本归档和网络转发。
3.5 auditd:内核级安全审计框架
auditd 是 Linux 内核提供的安全审计子系统,与 syslog 不同,它专注于**追踪"谁做了什么"**而非"发生了什么"。它通过内核钩子拦截系统调用,记录安全相关事件。
#mermaid-svg-nJSKqhGwXqQ5aPMy{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-nJSKqhGwXqQ5aPMy .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-nJSKqhGwXqQ5aPMy .error-icon{fill:#552222;}#mermaid-svg-nJSKqhGwXqQ5aPMy .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-nJSKqhGwXqQ5aPMy .marker{fill:#333333;stroke:#333333;}#mermaid-svg-nJSKqhGwXqQ5aPMy .marker.cross{stroke:#333333;}#mermaid-svg-nJSKqhGwXqQ5aPMy svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-nJSKqhGwXqQ5aPMy p{margin:0;}#mermaid-svg-nJSKqhGwXqQ5aPMy .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-nJSKqhGwXqQ5aPMy .cluster-label text{fill:#333;}#mermaid-svg-nJSKqhGwXqQ5aPMy .cluster-label span{color:#333;}#mermaid-svg-nJSKqhGwXqQ5aPMy .cluster-label span p{background-color:transparent;}#mermaid-svg-nJSKqhGwXqQ5aPMy .label text,#mermaid-svg-nJSKqhGwXqQ5aPMy span{fill:#333;color:#333;}#mermaid-svg-nJSKqhGwXqQ5aPMy .node rect,#mermaid-svg-nJSKqhGwXqQ5aPMy .node circle,#mermaid-svg-nJSKqhGwXqQ5aPMy .node ellipse,#mermaid-svg-nJSKqhGwXqQ5aPMy .node polygon,#mermaid-svg-nJSKqhGwXqQ5aPMy .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-nJSKqhGwXqQ5aPMy .rough-node .label text,#mermaid-svg-nJSKqhGwXqQ5aPMy .node .label text,#mermaid-svg-nJSKqhGwXqQ5aPMy .image-shape .label,#mermaid-svg-nJSKqhGwXqQ5aPMy .icon-shape .label{text-anchor:middle;}#mermaid-svg-nJSKqhGwXqQ5aPMy .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-nJSKqhGwXqQ5aPMy .rough-node .label,#mermaid-svg-nJSKqhGwXqQ5aPMy .node .label,#mermaid-svg-nJSKqhGwXqQ5aPMy .image-shape .label,#mermaid-svg-nJSKqhGwXqQ5aPMy .icon-shape .label{text-align:center;}#mermaid-svg-nJSKqhGwXqQ5aPMy .node.clickable{cursor:pointer;}#mermaid-svg-nJSKqhGwXqQ5aPMy .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-nJSKqhGwXqQ5aPMy .arrowheadPath{fill:#333333;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-nJSKqhGwXqQ5aPMy .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-nJSKqhGwXqQ5aPMy .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-nJSKqhGwXqQ5aPMy .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-nJSKqhGwXqQ5aPMy .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-nJSKqhGwXqQ5aPMy .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-nJSKqhGwXqQ5aPMy .cluster text{fill:#333;}#mermaid-svg-nJSKqhGwXqQ5aPMy .cluster span{color:#333;}#mermaid-svg-nJSKqhGwXqQ5aPMy div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-nJSKqhGwXqQ5aPMy .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-nJSKqhGwXqQ5aPMy rect.text{fill:none;stroke-width:0;}#mermaid-svg-nJSKqhGwXqQ5aPMy .icon-shape,#mermaid-svg-nJSKqhGwXqQ5aPMy .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-nJSKqhGwXqQ5aPMy .icon-shape p,#mermaid-svg-nJSKqhGwXqQ5aPMy .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-nJSKqhGwXqQ5aPMy .icon-shape .label rect,#mermaid-svg-nJSKqhGwXqQ5aPMy .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-nJSKqhGwXqQ5aPMy .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-nJSKqhGwXqQ5aPMy .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-nJSKqhGwXqQ5aPMy :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 输出目标
审计规则
用户空间
内核审计子系统
系统调用拦截
execve, open, connect...
文件系统监控
inotify + 审计规则
SELinux / AppArmor
策略事件
auditd 守护进程
auditctl 规则管理
ausearch 日志搜索
aureport 报告生成
audispd 插件分发
/etc/audit/audit.rules
/etc/audit/rules.d/*.rules
/var/log/audit/audit.log
远程 auditd
SIEM / Elasticsearch
auditd 核心工具
| 工具 | 功能 |
|---|---|
auditd |
审计守护进程,写入日志 |
auditctl |
动态管理审计规则 |
ausearch |
搜索审计日志 |
aureport |
生成审计报告 |
augenrules |
从规则目录生成规则文件 |
审计规则示例
bash
# 监控 /etc/passwd 的所有访问
auditctl -a always,exit -F arch=b64 -F path=/etc/passwd -F perm=rwxa
# 监控 /etc/security 目录
auditctl -a always,exit -F arch=b64 -F dir=/etc/security
# 监控所有用户的命令执行(execve)
auditctl -a always,exit -F arch=b64 -S execve -C uid!=euid
# 查看当前规则
auditctl -l
# 删除所有规则
auditctl -D
持久化规则写入 /etc/audit/rules.d/:
bash
# /etc/audit/rules.d/custom.rules
-a always,exit -F arch=b64 -F path=/etc/passwd -F perm=rwxa
-a always,exit -F arch=b32 -F path=/etc/passwd -F perm=rwxa
-w /etc/shadow -p wa -k identity_changes
-w /sbin/insmod -p x -k module_insertion
日志分析
bash
# 搜索特定关键字的审计事件
ausearch -k identity_changes
# 生成登录报告
aureport --login --summary
# 生成文件访问报告
aureport --file --summary
# 查看用户活动
aureport --user -i
注意:auditd 日志可能非常庞大,一条规则就能在几分钟内淹没日志。生产环境部署前必须充分测试。
3.6 logrotate:日志轮转与生命周期管理
logrotate 是 Linux 系统中管理日志文件生命周期的标准工具,防止日志文件无限增长撑爆磁盘。
配置示例
bash
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily # 每天轮转
missingok # 文件不存在不报错
rotate 14 # 保留 14 个备份
compress # 压缩旧日志
delaycompress # 延迟压缩(最近一个周期不压缩)
notifempty # 空文件不轮转
create 0640 www-data adm # 创建新文件权限
sharedscripts # 共享 postrotate 脚本
postrotate
/bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || true
endscript
}
常用配置指令
| 指令 | 说明 |
|---|---|
daily/weekly/monthly |
轮转周期 |
rotate N |
保留 N 个备份 |
compress |
用 gzip 压缩 |
delaycompress |
延迟一个周期压缩 |
missingok |
日志不存在不报错 |
notifempty |
空文件不轮转 |
create mode owner group |
创建新日志文件的权限 |
dateext |
用日期作为后缀 |
maxsize |
达到指定大小即轮转 |
su user group |
以特定用户权限运行 |
测试与调试
bash
# 测试配置(不实际执行)
logrotate -d /etc/logrotate.conf
# 强制轮转特定配置
logrotate -vf /etc/logrotate.d/nginx
# 查看状态文件
cat /var/lib/logrotate/status
四、日志体系协同工作流程
远程日志服务器 logrotate /var/log/*.log rsyslog systemd-journald 应用程序 远程日志服务器 logrotate /var/log/*.log rsyslog systemd-journald 应用程序 #mermaid-svg-9KxznZIOLlXayrMK{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-9KxznZIOLlXayrMK .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-9KxznZIOLlXayrMK .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-9KxznZIOLlXayrMK .error-icon{fill:#552222;}#mermaid-svg-9KxznZIOLlXayrMK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9KxznZIOLlXayrMK .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-9KxznZIOLlXayrMK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9KxznZIOLlXayrMK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9KxznZIOLlXayrMK .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-9KxznZIOLlXayrMK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9KxznZIOLlXayrMK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9KxznZIOLlXayrMK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9KxznZIOLlXayrMK .marker.cross{stroke:#333333;}#mermaid-svg-9KxznZIOLlXayrMK svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9KxznZIOLlXayrMK p{margin:0;}#mermaid-svg-9KxznZIOLlXayrMK .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-9KxznZIOLlXayrMK text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-9KxznZIOLlXayrMK .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-9KxznZIOLlXayrMK .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-9KxznZIOLlXayrMK .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-9KxznZIOLlXayrMK #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-9KxznZIOLlXayrMK .sequenceNumber{fill:white;}#mermaid-svg-9KxznZIOLlXayrMK #sequencenumber{fill:#333;}#mermaid-svg-9KxznZIOLlXayrMK #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-9KxznZIOLlXayrMK .messageText{fill:#333;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-9KxznZIOLlXayrMK .labelText,#mermaid-svg-9KxznZIOLlXayrMK .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .loopText,#mermaid-svg-9KxznZIOLlXayrMK .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-9KxznZIOLlXayrMK .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-9KxznZIOLlXayrMK .noteText,#mermaid-svg-9KxznZIOLlXayrMK .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-9KxznZIOLlXayrMK .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-9KxznZIOLlXayrMK .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-9KxznZIOLlXayrMK .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-9KxznZIOLlXayrMK .actorPopupMenu{position:absolute;}#mermaid-svg-9KxznZIOLlXayrMK .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-9KxznZIOLlXayrMK .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-9KxznZIOLlXayrMK .actor-man circle,#mermaid-svg-9KxznZIOLlXayrMK line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-9KxznZIOLlXayrMK :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} messages, secure, cron, maillog... stdout/stderr / syslog() 结构化存储 带元数据(PID, UID, 时间戳) 通过 /run/systemd/journal/syslog 转发 过滤/分类/格式化 写入文本日志文件 定期轮转/压缩/删除 TCP/UDP/TLS 转发
- 应用程序 通过
stdout/stderr(systemd 服务)或syslog()API 输出日志 - journald 收集所有日志,以结构化二进制格式存储,附带丰富元数据
- journald 通过 Unix domain socket 将日志转发给 rsyslog
- rsyslog 根据
/etc/rsyslog.conf规则,将日志分类写入/var/log/下的文本文件 - logrotate 定期轮转、压缩、清理旧日志,防止磁盘耗尽
- rsyslog 可选地将日志转发到远程日志服务器或 SIEM
五、现代集中化日志架构
在大型基础设施中,单机日志管理已无法满足需求,需要集中化日志收集、存储和分析。
#mermaid-svg-yM9rFWlscBhfDY8k{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yM9rFWlscBhfDY8k .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yM9rFWlscBhfDY8k .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yM9rFWlscBhfDY8k .error-icon{fill:#552222;}#mermaid-svg-yM9rFWlscBhfDY8k .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yM9rFWlscBhfDY8k .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yM9rFWlscBhfDY8k .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yM9rFWlscBhfDY8k .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yM9rFWlscBhfDY8k .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yM9rFWlscBhfDY8k .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yM9rFWlscBhfDY8k .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yM9rFWlscBhfDY8k .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yM9rFWlscBhfDY8k .marker.cross{stroke:#333333;}#mermaid-svg-yM9rFWlscBhfDY8k svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yM9rFWlscBhfDY8k p{margin:0;}#mermaid-svg-yM9rFWlscBhfDY8k .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-yM9rFWlscBhfDY8k .cluster-label text{fill:#333;}#mermaid-svg-yM9rFWlscBhfDY8k .cluster-label span{color:#333;}#mermaid-svg-yM9rFWlscBhfDY8k .cluster-label span p{background-color:transparent;}#mermaid-svg-yM9rFWlscBhfDY8k .label text,#mermaid-svg-yM9rFWlscBhfDY8k span{fill:#333;color:#333;}#mermaid-svg-yM9rFWlscBhfDY8k .node rect,#mermaid-svg-yM9rFWlscBhfDY8k .node circle,#mermaid-svg-yM9rFWlscBhfDY8k .node ellipse,#mermaid-svg-yM9rFWlscBhfDY8k .node polygon,#mermaid-svg-yM9rFWlscBhfDY8k .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yM9rFWlscBhfDY8k .rough-node .label text,#mermaid-svg-yM9rFWlscBhfDY8k .node .label text,#mermaid-svg-yM9rFWlscBhfDY8k .image-shape .label,#mermaid-svg-yM9rFWlscBhfDY8k .icon-shape .label{text-anchor:middle;}#mermaid-svg-yM9rFWlscBhfDY8k .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yM9rFWlscBhfDY8k .rough-node .label,#mermaid-svg-yM9rFWlscBhfDY8k .node .label,#mermaid-svg-yM9rFWlscBhfDY8k .image-shape .label,#mermaid-svg-yM9rFWlscBhfDY8k .icon-shape .label{text-align:center;}#mermaid-svg-yM9rFWlscBhfDY8k .node.clickable{cursor:pointer;}#mermaid-svg-yM9rFWlscBhfDY8k .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yM9rFWlscBhfDY8k .arrowheadPath{fill:#333333;}#mermaid-svg-yM9rFWlscBhfDY8k .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yM9rFWlscBhfDY8k .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yM9rFWlscBhfDY8k .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yM9rFWlscBhfDY8k .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yM9rFWlscBhfDY8k .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yM9rFWlscBhfDY8k .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yM9rFWlscBhfDY8k .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yM9rFWlscBhfDY8k .cluster text{fill:#333;}#mermaid-svg-yM9rFWlscBhfDY8k .cluster span{color:#333;}#mermaid-svg-yM9rFWlscBhfDY8k div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yM9rFWlscBhfDY8k .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yM9rFWlscBhfDY8k rect.text{fill:none;stroke-width:0;}#mermaid-svg-yM9rFWlscBhfDY8k .icon-shape,#mermaid-svg-yM9rFWlscBhfDY8k .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yM9rFWlscBhfDY8k .icon-shape p,#mermaid-svg-yM9rFWlscBhfDY8k .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yM9rFWlscBhfDY8k .icon-shape .label rect,#mermaid-svg-yM9rFWlscBhfDY8k .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yM9rFWlscBhfDY8k .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yM9rFWlscBhfDY8k .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yM9rFWlscBhfDY8k :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 可视化与分析
集中存储
日志收集层
日志产生节点
服务器 A
journald + rsyslog
服务器 B
容器日志
服务器 C
应用日志
网络设备
syslog
Fluentd / Fluent Bit
Vector
Promtail
Filebeat / Auditbeat
Elasticsearch
全文索引
Loki
标签索引 + Grafana
ClickHouse
列式存储
S3 / 对象存储
长期归档
Kibana
Grafana
自定义 Dashboard
主流技术栈对比
| 方案 | 收集器 | 存储 | 可视化 | 特点 |
|---|---|---|---|---|
| ELK Stack | Filebeat/Logstash | Elasticsearch | Kibana | 全文检索强,生态成熟 |
| PLG Stack | Promtail | Loki | Grafana | 标签索引,与 Prometheus 生态集成 |
| ClickHouse | Vector/Fluentd | ClickHouse | Grafana | 列式存储,分析性能高 |
| 云原生 | Fluent Bit | S3/CloudWatch | 云厂商工具 | 托管服务,免运维 |
六、安全最佳实践
6.1 日志文件权限
敏感日志(如认证日志)应严格限制访问权限:
bash
# 认证日志
chmod 600 /var/log/secure
chown root:root /var/log/secure
# 审计日志
chmod 600 /var/log/audit/audit.log
chown root:root /var/log/audit/audit.log
6.2 日志完整性保护
使用 auditd 或 aide 监控日志文件本身,防止篡改:
bash
# 监控日志目录
auditctl -w /var/log/ -p wa -k log_integrity
6.3 远程日志与防篡改
将日志实时转发到远程只读服务器,即使本地被入侵,日志仍然安全:
bash
# rsyslog 配置:加密转发
*.* action(type="omfwd"
target="log-server.example.com"
port="6514"
protocol="tcp"
StreamDriver="gtls"
StreamDriverMode="1"
StreamDriverAuthMode="anon"
)
6.4 日志保留策略
根据合规要求设置保留期限:
| 日志类型 | 建议保留期 | 法规参考 |
|---|---|---|
| 系统日志 | 30-90 天 | 内部策略 |
| 认证日志 | 1 年 | PCI DSS |
| 审计日志 | 1-7 年 | 等保 2.0 / HIPAA |
| 安全事件 | 永久 | SOC 2 |
七、总结
Linux 的日志体系是一个分层协作、新旧共存的复杂系统:
- journald 作为现代收集器,提供结构化、带元数据的快速查询能力
- rsyslog 作为传统处理器,负责文本持久化和网络转发
- auditd 作为安全审计层,在内核层面追踪关键操作
- logrotate 作为生命周期管理器,防止日志失控
- 集中化架构(ELK/PLG)将单机日志扩展为企业级可观测性平台
理解这些组件如何协同工作,是 Linux 运维和安全工作的基本功。在实际环境中,建议根据规模选择合适的架构:单机场景以 journald + rsyslog + logrotate 为主,企业场景则需要引入集中化收集和分析平台。