如何将 111.111.111.111 的 ping 操作记录到 /var/log/iptables.log 中?

如何将 111.111.111.111 的 ping 操作记录到 /var/log/iptables.log 中?

我的操作系统:centos 7。

yum remove firewalld
yum install  -y iptables
yum install -y iptables-services

我想将来自 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

现在从 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

2.iptables -A INPUT -p icmp --icmp-type 8 --source 111.111.111.111 -j LOG无法工作

cat /var/log/iptables.log

其中什么都没有,如何将 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.

这是否意味着 iptables 日志信息无法写入指定文件?
iptables 日志信息只能通过以下方式写入指定文件:

dmesg > /var/log/iptables.log

或者

dmesg >> /var/log/iptables.log

是否有另一种智能方法可以将所有信息自动记录到 /var/log/iptables.log 中?

答案1

让我们看看您的设置的作用:

iptables -N LOGGING

LOGGING您创建一个名为(以后永远不会获得任何规则)的新链。

iptables -A INPUT -j LOGGING

遍历输入链的所有数据包都会跳转到LOGGING。此规则之后的所有规则都将被忽略,因为您永远不会从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

记录后,您想要丢弃数据包,这意味着不会生成回复。不确定这是否是您的意图。


所以,不要这样做。使用 清除所有这些规则iptables -F INPUT,然后执行

iptables -A INPUT -p icmp --icmp-type 8 --source 111.111.111.111 -j LOG

没有其他的。现在执行 ping 操作,看看它是否显示在系统日志中(在我的系统上确实如此)。

如果确实需要,您可以添加一条规则来随后删除 ping。

答案2

vps 的类型很重要。
我的 vps 类型是 openvz。
日志文件将通过 dmesg 显示,并且不会自动写入客户日志文件,这是 openvz 中的一个错误。
将我的 vps 更改为 kvm vps,问题解决。

相关内容