Ubuntu Syslog日志审计技巧
一 快速自检与基线
sudo systemctl status rsyslog;如启用内核/命令级审计,检查 auditd:sudo systemctl status auditd。sudo auditctl -l(若提示无权限或命令不存在,说明 auditd 未安装或未运行)。journalctl -k)。二 配置Syslog采集与转发
/etc/rsyslog.d/40-audit.conf):
auth,authpriv.* /var/log/auth.log*.err /var/log/error.log*.* @192.0.2.10:514;如需可靠传输可用 TCP(在 rsyslog 中启用相应模块并使用 @@ 前缀)。& stop 终止后续匹配;必要时关闭重复消息压缩(如 RepeatedMsgReduction off)以便取证完整性。sudo systemctl restart rsyslog。三 命令行高效审计与报表
tail -f /var/log/sysloggrep -i "error" /var/log/syslogjournalctl --since "2025-11-15 00:00:00" --until "2025-11-15 12:00:00"awk '/Jun 17/ {count++} END {print count}' /var/log/syslogawk '/ERROR/ {print $1,$2,$3,$9}' /var/log/error.logsudo apt-get install logwatch,可自定义 /etc/logwatch/conf/logwatch.conf 输出范围与收件人。#!/usr/bin/env bash
LOG=/var/log/error.log
MAIL=admin@example.com
if grep -qi "ERROR" "$LOG"; then
echo "Error found in $LOG" | mail -s "Syslog Alert" "$MAIL"
fi
加入定时任务:0 * * * * /path/to/alert.sh。四 进阶 命令历史与集中化检索
/etc/profile.d/bash.audit.sh 中为 root 与交互式 bash 用户注入记录逻辑,使用 logger -t history[$$] -p local4.debug "user cmd" 将命令写入 local4 设施;local4.* /var/log/history.log 并重启 rsyslog;.bash_history 的新增行,同时清空已上报内容(注意多终端时序与并发写入问题)。@timestamp、host、program、severity 等字段建立索引,便于 Kibana 可视化与快速检索。五 安全与合规要点
/etc/logrotate.d/syslog),满足容量与合规要求。sudo 提权、*.err 错误激增等设定阈值告警,结合 Logwatch 或集中平台实现日报/周报与异常趋势识别。