出于某种原因,我的 NAT 只允许本地网络上的连接。当我 ping 本地网络时,数据包以 NAT 网关作为来源。当我 ping 远程网络(Internet 主机等)时,数据包源是设备的内部 IP 地址,并且响应没有正确发回。关于可能配置不正确的任何想法?
router ~ # iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -i eth2 -j ACCEPT
-A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
router ~ # iptables -S -t nat
-P PREROUTING ACCEPT
-P INPUT ACCEPT
-P OUTPUT ACCEPT
-P POSTROUTING ACCEPT
-A POSTROUTING -o eth0 -j MASQUERADE
内部网络是 192.168.0.0/16,外部(NAT 本地)是 10.72.16.0/22。
更新了 AB 的信息
router ~ # ip -br link; ip -4 -br addr; ip route; ip rule
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
eth0 UP 00:15:5d:e8:47:39 <BROADCAST,MULTICAST,UP,LOWER_UP>
eth1 UP 00:15:5d:e8:47:3a <BROADCAST,MULTICAST,UP,LOWER_UP>
eth2 UP 00:15:5d:e8:47:46 <BROADCAST,MULTICAST,UP,LOWER_UP>
sit0@NONE DOWN 0.0.0.0 <NOARP>
lo UNKNOWN 127.0.0.1/8
eth0 UP 10.72.16.140/22
eth1 UP 10.72.21.14/22
eth2 UP 192.168.0.1/16
default via 10.72.20.1 dev eth1
default via 10.72.16.1 dev eth0
10.72.16.0/22 dev eth0 proto kernel scope link src 10.72.16.140
10.72.20.0/22 dev eth1 proto kernel scope link src 10.72.21.14
192.168.0.0/16 dev eth2 proto kernel scope link src 192.168.0.1
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
内部主机也是如此:
int_host ~ # ip -br link; ip -4 -br addr; ip route; ip rule
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
eth0 UP 52:69:6e:00:81:95 <BROADCAST,MULTICAST,UP,LOWER_UP>
usb0 DOWN 52:69:6e:00:00:00 <NO- CARRIER,BROADCAST,MULTICAST,UP>
lo UNKNOWN 127.0.0.1/8
eth0 UP 192.168.0.5/16
usb0 DOWN 192.168.127.5/24 169.254.0.1/16
default via 192.168.0.1 dev eth0
169.254.0.0/16 dev usb0 proto kernel scope link src 169.254.0.1 linkdown
192.168.0.0/16 dev eth0 proto kernel scope link src 192.168.0.5
192.168.127.0/24 dev usb0 proto kernel scope link src 192.168.127.5 linkdown
RTNETLINK answers: Address family not supported by protocol
Dump terminated
从内部主机 ping 到外部(本地)主机:
int_host ~ # ping 10.72.16.50
PING 10.72.16.50 (10.72.16.50) 56(84) bytes of data.
64 bytes from 10.72.16.50: icmp_seq=1 ttl=127 time=1.37 ms
^C
从内部主机 ping 到外部(外部)主机:
int_host ~ # ping 172.18.221.227
PING 172.18.221.227 (172.18.221.227) 56(84) bytes of data.
^C
--- 172.18.221.227 ping statistics ---
8 packets transmitted, 0 received, 100% packet loss, time 7243ms
Wireshark 显示 ping 的来源是内部主机的内部 IP 地址:
4842 60.182197 192.168.0.5 172.18.221.227 ICMP 98 Echo (ping) request id=0x567d, seq=7/1792, ttl=62 (reply in 4843)
4843 60.182365 172.18.221.227 192.168.0.5 ICMP 98 Echo (ping) reply id=0x567d, seq=7/1792, ttl=128 (request in 4842)
正如预期的那样,响应永远不会被发回,因为它会发送到不存在的 192.168.0.5 主机而不是 10.72.16.140 的路由器(然后发送到内部 192.168.0.5 地址)。
路由按顺序应用。所以默认路由(以及给出的示例
172.18.221.227
)正在通过,eth1
因为它在路由表中是第一个。唯一的MASQUERADE
规则适用于通过 的数据包eth0
。正如 OP 评论的那样,POSTROUTING
改变路线不可能改变路线。因此,根本没有对通过的数据包进行任何更改eth1
,保留原始 RFC1918 IP。因此,将规则更改为也
MASQUERADE
适用于eth1
修复它。如果
INPUT
策略设置(再次?)为,则还应考虑将...DROP
复制到也适用于。state
ESTABLISHED
eth1