调试iptables

发布时间 2023-06-09 15:16:38作者: 王景迁

开启调试iptables的内核模块

modprobe nf_log_ipv4
sysctl net.netfilter.nf_log.2

添加iptables trace规则

# raw只在PREROUTING和OUTPUT才有
iptables -t raw -A PREROUTING -p icmp -j TRACE
iptables -t raw -A OUTPUT -p icmp -j TRACE

启动容器后验证

docker run -itd busybox:1.36 sh

tail -f /var/log/messages

从宿主机上ping容器IP
发送流程
OUTPUT->POSTROUTING->docker0->容器网卡
响应流程
容器网卡->docker0->PREROUTING->INPUT

恢复

iptables -t raw -D PREROUTING 1
iptables -t raw -D OUTPUT 1
modprobe -r nf_log_ipv4

参考资料

Linux中调试 iptables