在Linux系统安全审计中,系统日志监控是异常行为检测的核心环节。本文将通过CentOS系统的实际配置案例,展示如何构建有效的日志监控体系。
基础日志收集配置
首先需要确保系统启用了syslog服务并正确配置日志级别。编辑/etc/rsyslog.conf文件,添加以下配置:
# 设置日志级别为info及以上
*.info;mail.none;authpriv.none;cron.none /var/log/messages
# 启用远程日志转发
*.* @192.168.1.100:514
重启rsyslog服务使配置生效:
systemctl restart rsyslog
systemctl enable rsyslog
SSH异常登录检测
通过分析/var/log/secure日志,可以识别异常的SSH登录行为。编写监控脚本/usr/local/bin/ssh_monitor.sh:
#!/bin/bash
LOG_FILE="/var/log/secure"
THRESHOLD=5
IP=$(awk -v threshold=$THRESHOLD '/Failed password/ {print $11}' $LOG_FILE | sort | uniq -c | awk -v threshold=$threshold '$1 >= threshold {print $2}')
for ip in $IP; do
logger "[SECURITY] Suspicious SSH attempts from $ip"
iptables -A INPUT -s $ip -j DROP
echo "Blocked IP: $ip"
done
定时任务安全检查
定期扫描系统中可疑的定时任务:
#!/bin/bash
find /etc/cron.* -type f -exec ls -l {} \;
# 检查是否有异常用户crontab
for user in $(cut -f1 -d: /etc/passwd); do
crontab -l -u $user 2>/dev/null | grep -v "^#" | grep -v "^$"
done
日志轮转配置
为防止日志文件过大,建议配置logrotate:
/var/log/messages {
daily
rotate 7
compress
delaycompress
missingok
notifempty
}
通过以上配置,可以有效监控系统安全事件并及时响应异常行为。建议定期审查日志内容,确保监控策略的有效性。

讨论