在 CentOS 中使用 iptables 记录日志(LOG),一般步骤如下(以 CentOS 6 / 7 传统 iptables 为例,CentOS 8+ 默认是 nftables,但也可用 iptables 兼容层)。
例如,记录所有进入的 TCP 流量:
iptables -I INPUT -p tcp -j LOG --log-prefix "IPTABLES-TCP-IN: "
说明:
-j LOG:使用 LOG 目标--log-prefix:日志前缀,方便后面过滤防止日志爆炸:
iptables -I INPUT -p tcp -m limit --limit 5/min -j LOG --log-prefix "IPTABLES-TCP-IN: "
tail -f /var/log/messages
journalctl -f | grep IPTABLES
或
dmesg | grep IPTABLES
编辑:
vi /etc/rsyslog.conf
添加:
:msg, contains, "IPTABLES-TCP-IN" /var/log/iptables.log
& stop
重启 rsyslog:
systemctl restart rsyslog
查看:
tail -f /var/log/iptables.log
# 拒绝并记录 SSH 暴力破解
iptables -A INPUT -p tcp --dport 22 -m limit --limit 3/min -j LOG --log-prefix "SSH-ATTACK: "
iptables -A INPUT -p tcp --dport 22 -j DROP
CentOS 8 默认是 nftables:
nft add rule inet filter input tcp dport 22 log prefix "NFT-SSH: "
如果你坚持用 iptables:
yum install iptables-services
systemctl stop nftables
systemctl start iptables
-m limit如果你愿意,我可以:
你用的是 CentOS 几?