為什麼 iptables 規則會影響 ping localhost?

為什麼 iptables 規則會影響 ping localhost?

下面是我的 centos 系統中的 iptable 防火牆規則。此時我無法 ping 本機。ping localhost失敗了。

但一旦我iptables -F刷新了規則,我就能夠 ping 通本地主機了。問題是為什麼?此 iptables 設定如何影響 ping localhost (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 

相關內容