Linux系统使用多种日志机制来记录系统和应用程序活动,主要包括:
/var/log/messages
:常规系统消息/var/log/auth.log
或 /var/log/secure
:认证和安全相关日志/var/log/kern.log
:内核日志/var/log/boot.log
:系统启动日志/var/log/dmesg
:设备驱动和内核启动消息/var/log/cron
:定时任务日志/var/log/maillog
或 /var/log/mail.log
:邮件服务器日志/var/log/httpd/
或 /var/log/apache2/
:Apache Web服务器日志/var/log/mysql.log
:MySQL数据库日志# 查看日志文件内容
cat /var/log/messages
less /var/log/messages
# 实时查看日志更新
tail -f /var/log/messages
# 使用journalctl查看systemd日志
journalctl -xe
journalctl -u nginx.service --since "2023-01-01" --until "2023-01-02"
Linux使用logrotate
管理日志轮转,配置文件位于:
- /etc/logrotate.conf
:主配置文件
- /etc/logrotate.d/
:各服务的独立配置
示例配置:
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
/usr/sbin/nginx -s reload
endscript
}
安装和启用:
sudo apt install auditd # Debian/Ubuntu
sudo yum install audit # RHEL/CentOS
sudo systemctl enable --now auditd
常用配置:
# 查看审计规则
sudo auditctl -l
# 监控文件访问
sudo auditctl -w /etc/passwd -p rwxa -k password_file
# 监控目录
sudo auditctl -w /etc/ssh/ -p wa -k ssh_config
# 监控系统调用
sudo auditctl -a always,exit -F arch=b64 -S open -F success=1 -k file_access
审计日志默认位于/var/log/audit/audit.log
,可使用以下工具分析:
# 基本查看
sudo cat /var/log/audit/audit.log
# 使用ausearch工具
sudo ausearch -k password_file # 按key搜索
sudo ausearch -m USER_LOGIN # 按消息类型搜索
# 使用aureport生成报告
sudo aureport --summary
sudo aureport -l # 登录报告
sudo aureport -f # 文件访问报告
解决方案:
- 检查并优化logrotate配置
- 清理旧日志:sudo find /var/log -type f -name "*.log" -size +100M -exec ls -lh {} \;
- 调整应用程序的日志级别
检查步骤:
# 检查服务状态
sudo systemctl status rsyslog
sudo systemctl status auditd
# 检查配置文件语法
sudo rsyslogd -N1
sudo auditctl -s
# 检查磁盘空间
df -h
示例:追踪用户登录
# 查看最近登录
last
lastlog
# 查看SSH登录
grep "sshd" /var/log/auth.log
# 使用审计系统追踪
sudo ausearch -m USER_LOGIN
通过以上方法和工具,您可以有效地管理和审计Linux系统的日志,提高系统的安全性和可维护性。