以下是我的 centos 系统中的 iptable 防火墙规则。此时我无法 ping 本地主机。ping localhost
失败了。
但是,当我iptables -F
刷新规则后,我就能 ping 本地主机了。问题是为什么?这个 iptables 设置如何影响 ping 本地主机 (127.0.0.1) 的功能?
root@localhost:~# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:67
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:67
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:6080
LPASS udp -- 0.0.0.0/0 0.0.0.0/0 state NEW udp spt:68 dpt:67
LPASS all -- 192.168.122.49 0.0.0.0/0
LPASS tcp -- 192.168.122.0/24 0.0.0.0/0 tcp spt:21
LPASS all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
LPASS tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:21
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state NEW
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpts:8505:8506
LPASS tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:443
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:6000
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpts:6001:6100
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 state NEW udp dpt:161
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:24
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:8081
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:2222
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
LDROP all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 192.168.122.0/24 ctstate RELATED,ESTABLISHED
ACCEPT all -- 192.168.122.0/24 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
ACCEPT tcp -- 0.0.0.0/0 192.168.122.49 tcp dpt:3389
ACCEPT all -- 192.168.122.49 0.0.0.0/0
DROP all -- 192.168.122.0/24 0.0.0.0/0
LFDROP all -- 0.0.0.0/0 0.0.0.0/0 PHYSDEV match --physdev-in vnet+ --physdev-out vnet+ --physdev-is-bridged
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state NEW
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:68
Chain LDROP (1 references)
target prot opt source destination
DROP all -- 0.0.0.0/0 0.0.0.0/0
Chain LFDROP (1 references)
target prot opt source destination
DROP all -- 0.0.0.0/0 0.0.0.0/0
Chain LPASS (6 references)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ifconfig 显示接口已启动。
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 65922 bytes 5788036 (5.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 65922 bytes 5788036 (5.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
答案1
这是因为一切从接口出去并进入接口的信息通过 iptables 或防火墙规则进行解析。
因此,当数据包必须从接口传到自身时,无论是 ICMP、HTTP、FTP 还是任何其他协议。它将通过与外部 IP 相同的过程。例如,对于 HTTP,它将启动 TCP 连接,然后向本地主机发送 HTTP 请求,所有通信都将通过洛界面。
现在例如你继续做一个输出规则如下:
ACCEPT ICMP -- 127.0.0.1 127.0.0.1
并尝试运行 ICMP,它仍然无法通过,您还需要如下所示的输入规则才能使其工作:
ACCEPT ICMP -- 127.0.0.1 127.0.0.1