我的操作系统:centos 7。
yum remove firewalld
yum install -y iptables
yum install -y iptables-services
Run Code Online (Sandbox Code Playgroud)
我想将来自 111.1111.111.111 的所有 ping 记录到 iptables.log 中。
cat /etc/rsyslog.conf
kern.* /var/log/iptables.log
systemctl restart rsyslog
iptables -N LOGGING
iptables -A INPUT -j LOGGING
iptables -A INPUT -p icmp --icmp-type 8 --source 111.111.111.111 -j LOG
iptables -A INPUT -p icmp --icmp-type 8 --source 111.111.111.111 -j DROP
service iptables save
service iptables restart
Run Code Online (Sandbox Code Playgroud)
现在从 111.111.111.111 ping 到我的 vps。
1.iptables -A INPUT -p icmp --icmp-type 8 --source 111.111.111.111 -j DROP
可以工作。
ping 104.223.65.117
PING 104.223.65.117 (104.223.65.117) 56(84) bytes of data. ^C --- 104.223.65.117
ping statistics --- 23 packets transmitted, 0 received, 100% packet loss, time 22003ms
Run Code Online (Sandbox Code Playgroud)
2.iptables -A INPUT -p icmp --icmp-type 8 --source 111.111.111.111 -j LOG
不能工作
cat /var/log/iptables.log
Run Code Online (Sandbox Code Playgroud)
什么都没有,如何将 111.111.111.111 的 ping 操作记录到 /var/log/iptables.log 中?
我在这里得到了一些材料:dmesg 和 iptables
dmesg reads from the kernel log buffer. Since iptables uses kernel logging
facility, that is where iptables log records appear in the first place.
There is no way you can make iptables log entries not appear in dmesg.
Run Code Online (Sandbox Code Playgroud)
这是否意味着无法将 iptables 日志信息写入指定文件?
iptables 日志信息只能通过这种方式写入指定文件:
dmesg > /var/log/iptables.log
Run Code Online (Sandbox Code Playgroud)
或者
dmesg >> /var/log/iptables.log
Run Code Online (Sandbox Code Playgroud)
?
是否有另一种聪明的方法将所有信息自动记录到 /var/log/iptables.log 中?
让我们看看你的设置做了什么:
iptables -N LOGGING
Run Code Online (Sandbox Code Playgroud)
您创建了一个名为的新链LOGGING
(以后永远不会获得任何规则)。
iptables -A INPUT -j LOGGING
Run Code Online (Sandbox Code Playgroud)
遍历输入链的所有数据包都跳转到LOGGING
. 此规则之后的所有规则都将被忽略,因为您永远不会从LOGGING
链中返回。
iptables -A INPUT -p icmp --icmp-type 8 --source 111.111.111.111 -j LOG
Run Code Online (Sandbox Code Playgroud)
这条规则是在上面的跳转之后插入的,永远不会执行。
iptables -A INPUT -p icmp --icmp-type 8 --source 111.111.111.111 -j DROP
Run Code Online (Sandbox Code Playgroud)
记录后,您想丢弃数据包,这意味着不会生成回复。不确定这是否是您的意图。
所以,不要这样做。用 清除所有这些规则iptables -F INPUT
,然后只做
iptables -A INPUT -p icmp --icmp-type 8 --source 111.111.111.111 -j LOG
Run Code Online (Sandbox Code Playgroud)
没有其他的。现在执行 ping 操作,看看它是否显示在系统日志中(在我的系统上显示)。
如果您确实需要,您可以添加规则以在之后删除 ping。