一、日志管理概述
Linux系统日志是记录系统运行状态、用户操作和应用程序信息的重要工具,对于故障排查和安全审计至关重要。
学习交流加群风哥QQ113257174
1.1 主要日志文件
| 日志文件 | 说明 |
|---|---|
| /var/log/messages | 系统主日志文件 |
| /var/log/secure | 安全相关日志 |
| /var/log/auth.log | 认证日志(Debian系) |
| /var/log/syslog | 系统日志(Debian系) |
| /var/log/cron | 定时任务日志 |
| /var/log/boot.log | 系统启动日志 |
| /var/log/dmesg | 内核启动日志 |
| /var/log/audit/audit.log | 审计日志 |
二、rsyslog服务
2.1 rsyslog配置
# 主配置文件
/etc/rsyslog.conf
/etc/rsyslog.d/*.conf
# 配置格式
# 设施.级别 动作
# 设施类型
auth, authpriv # 认证相关
cron # 定时任务
daemon # 守护进程
kern # 内核
lpr # 打印
mail # 邮件
news # 新闻
syslog # syslog自身
user # 用户程序
local0-local7 # 自定义
# 日志级别
emerg # 紧急
alert # 警报
crit # 严重
err # 错误
warning # 警告
notice # 通知
info # 信息
debug # 调试
none # 不记录
# 配置示例
# /etc/rsyslog.conf
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
mail.* -/var/log/maillog
cron.* /var/log/cron
*.emerg :omusrmsg:*
local7.* /var/log/boot.log
2.2 rsyslog服务管理
# 启动服务
systemctl start rsyslog
# 停止服务
systemctl stop rsyslog
# 重启服务
systemctl restart rsyslog
# 查看状态
systemctl status rsyslog
# 测试配置
rsyslogd -N1
# 查看日志
tail -f /var/log/messages
# 输出示例
Jan 15 10:00:01 server systemd: Started Session 123 of user root.
Jan 15 10:00:05 server sshd[12345]: Accepted password for root from 192.168.1.100 port 54321 ssh2
Jan 15 10:01:00 server cron[12346]: (root) CMD (/usr/local/bin/backup.sh)
Jan 15 10:00:01 server systemd: Started Session 123 of user root.
Jan 15 10:00:05 server sshd[12345]: Accepted password for root from 192.168.1.100 port 54321 ssh2
Jan 15 10:01:00 server cron[12346]: (root) CMD (/usr/local/bin/backup.sh)
三、journalctl日志
3.1 journalctl基本使用
# 查看所有日志
journalctl
# 查看最近的日志
journalctl -n 100
# 实时查看日志
journalctl -f
# 按时间过滤
journalctl --since "2024-01-15 10:00:00"
journalctl --until "2024-01-15 12:00:00"
journalctl --since today
journalctl --since yesterday
journalctl --since "1 hour ago"
# 按优先级过滤
journalctl -p err
journalctl -p warning
journalctl -p 3 # 0=emerg, 1=alert, 2=crit, 3=err, 4=warning, 5=notice, 6=info, 7=debug
# 按服务过滤
journalctl -u nginx
journalctl -u sshd
journalctl -u docker
# 按用户过滤
journalctl _UID=0
# 按进程过滤
journalctl _PID=12345
# 组合过滤
journalctl -u nginx --since today -p err
3.2 journalctl输出格式
# JSON格式
journalctl -u nginx -o json
# 详细格式
journalctl -u nginx -o verbose
# 简洁格式
journalctl -u nginx -o short-precise
# 导出日志
journalctl --since "2024-01-01" > logs.txt
# 查看磁盘使用
journalctl --disk-usage
# 清理日志
journalctl --vacuum-size=100M
journalctl --vacuum-time=7d
四、日志轮转
4.1 logrotate配置
# 主配置文件
/etc/logrotate.conf
# 子配置目录
/etc/logrotate.d/
# 配置示例
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
# 配置选项说明
daily # 每天轮转
weekly # 每周轮转
monthly # 每月轮转
rotate N # 保留N个旧日志
compress # 压缩旧日志
delaycompress # 延迟压缩
missingok # 日志不存在不报错
notifempty # 空日志不轮转
create # 创建新日志文件
dateext # 使用日期作为后缀
size # 按大小轮转
sharedscripts # 共享脚本
postrotate/endscript # 轮转后执行脚本
4.2 logrotate管理
# 手动执行轮转
logrotate -f /etc/logrotate.d/nginx
# 调试模式
logrotate -d /etc/logrotate.d/nginx
# 强制轮转
logrotate -vf /etc/logrotate.conf
# 查看状态
cat /var/lib/logrotate/logrotate.status
五、日志分析
5.1 常用分析命令
# 查看登录失败
grep "Failed password" /var/log/secure
grep "authentication failure" /var/log/auth.log
# 查看成功登录
grep "Accepted password" /var/log/secure
grep "session opened" /var/log/auth.log
# 统计登录失败IP
grep "Failed password" /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr
# 查看sudo操作
grep "sudo:" /var/log/secure
# 查看用户操作
grep "COMMAND" /var/log/secure
# 查看内核错误
dmesg | grep -i error
dmesg | grep -i fail
# 查看OOM
dmesg | grep -i "out of memory"
# 查看磁盘错误
grep -i "I/O error" /var/log/messages
5.2 日志搜索技巧
# 多条件搜索
grep -E "error|fail|critical" /var/log/messages
# 显示上下文
grep -C 5 "error" /var/log/messages
# 统计错误数量
grep -c "error" /var/log/messages
# 忽略大小写
grep -i "error" /var/log/messages
# 正则表达式
grep -E "Jan 15 (10|11):" /var/log/messages
# 使用awk分析
awk '/error/ {print $1, $2, $3, $0}' /var/log/messages
# 使用sed处理
sed -n '/2024-01-15/,/2024-01-16/p' /var/log/messages
六、审计日志
6.1 auditd配置
# 安装audit
yum install -y audit
# 启动服务
systemctl start auditd
systemctl enable auditd
# 配置文件
/etc/audit/auditd.conf
/etc/audit/audit.rules
# 添加审计规则
# 监控文件访问
auditctl -w /etc/passwd -p wa -k passwd_changes
# 监控目录
auditctl -w /etc/ -p wa -k etc_changes
# 监控系统调用
auditctl -a always,exit -F arch=b64 -S open -S openat -k file_access
# 监控用户操作
auditctl -a always,exit -F arch=b64 -S execve -F uid=1000 -k user_commands
# 查看规则
auditctl -l
# 删除规则
auditctl -D
6.2 审计日志查询
# 查看审计日志
ausearch -m all
# 按类型查询
ausearch -m USER_LOGIN
# 按用户查询
ausearch -ua zhangsan
# 按文件查询
ausearch -f /etc/passwd
# 按关键词查询
ausearch -k passwd_changes
# 按时间查询
ausearch --start 2024-01-15 --end 2024-01-16
# 生成报告
aureport
aureport --file
aureport --user
aureport --login
七、远程日志
7.1 日志服务器配置
# 服务器端配置
# /etc/rsyslog.conf
# 启用UDP接收
module(load="imudp")
input(type="imudp" port="514")
# 启用TCP接收
module(load="imtcp")
input(type="imtcp" port="514")
# 存储远程日志
$template RemoteLogs,"/var/log/%HOSTNAME%/%PROGRAMNAME%.log"
*.* ?RemoteLogs
# 重启服务
systemctl restart rsyslog
7.2 客户端配置
# 客户端配置
# /etc/rsyslog.conf
# 发送到远程服务器
*.* @@192.168.1.100:514 # TCP
*.* @192.168.1.100:514 # UDP
# 重启服务
systemctl restart rsyslog
八、最佳实践
| 配置项 | 建议 |
|---|---|
| 日志保留 | 至少90天 |
| 日志轮转 | 按大小或时间 |
| 远程备份 | 集中存储 |
| 日志监控 | 实时告警 |
注意事项:
- 定期检查日志磁盘空间
- 配置合理的日志轮转策略
- 重要日志应远程备份
- 定期审计日志内容
九、总结
日志管理是系统运维的重要环节。通过本培训文档,您应该掌握了:
- 主要日志文件的位置和作用
- rsyslog服务配置
- journalctl日志查询
- logrotate日志轮转
- 日志分析方法
- audit审计日志配置
IT运维培训文档系列 | 第59篇 | Linux系统日志管理培训
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
