设置
ubuntu linux VM(virtual-box)配置了两个接口,eth0和eth1。
eth0位于桥接网络上并直接连接到外部网络。
eth1位于“nat 网络”上,该网络也连接到外部网络。
问题
无法通过 eth0 进行 ping 操作。能够通过 TCP 连接到主机。
ping -I eth0 -c2 google.com
PING google.com (172.217.1.238) from 10.254.185.16 eth0: 56(84) bytes of data.
From company.com (10.254.185.16) icmp_seq=1 Destination Host Unreachable
From company.com (10.254.185.16) icmp_seq=2 Destination Host Unreachable
--- google.com ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1008ms
pipe 2
telnet -b 10.254.185.16 google.com 80
Trying 172.217.1.238...
Connected to google.com.
Escape character is '^]'.
ping 通过 eth1(默认路由)正常工作。
ping -I eth1 -c2 google.com
PING google.com (172.217.1.238) from 10.0.2.4 eth1: 56(84) bytes of data.
64 bytes from lax17s02-in-f14.1e100.net (172.217.1.238): icmp_seq=1 ttl=49 time=11.5 ms
64 bytes from lax17s02-in-f14.1e100.net (172.217.1.238): icmp_seq=2 ttl=49 time=11.3 ms
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 11.310/11.446/11.582/0.136 ms
细节
默认路由是通过 eth1。
route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.0.2.1 0.0.0.0 UG 0 0 0 eth1
10.0.2.0 * 255.255.255.0 U 0 0 0 eth1
10.254.184.0 * 255.255.248.0 U 0 0 0 eth0
192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0
ip route
default via 10.0.2.1 dev eth1
10.0.2.0/24 dev eth1 proto kernel scope link src 10.0.2.4
10.254.184.0/21 dev eth0 proto kernel scope link src 10.254.185.16
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
eth0 通过另一个路由表配置
ip route show table eth0
default via 10.254.184.1 dev eth0
10.254.184.0/21 dev eth0 scope link src 10.254.185.16
ifconfig eth0
eth0 Link encap:Ethernet HWaddr 08:00:27:6f:a1:e6
inet addr:10.254.185.16 Bcast:10.254.191.255 Mask:255.255.248.0
inet6 addr: fe80::a00:27ff:fe6f:a1e6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2123 errors:0 dropped:0 overruns:0 frame:0
TX packets:1280 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:237141 (237.1 KB) TX bytes:225214 (225.2 KB)
iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT udp -- anywhere anywhere udp dpt:bootps
ACCEPT tcp -- anywhere anywhere tcp dpt:bootps
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere 192.168.122.0/24 ctstate RELATED,ESTABLISHED
ACCEPT all -- 192.168.122.0/24 anywhere
ACCEPT all -- anywhere anywhere
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:bootpc
IP 规则
ip rule
0: from all lookup local
32763: from all to 10.246.240.0/20 lookup eth0
32764: from 10.246.240.0/20 lookup eth0
32765: from 10.246.242.68 lookup eth0
32766: from all lookup main
32767: from all lookup default
Traceroute 输出
traceroute -T r2d2.company.com
traceroute to r2d2.company.com (10.254.194.217), 30 hops max, 60 byte packets
1 nambi-ubuntu-dell-t5600.company.com (10.254.194.217) 13.181 ms 13.164 ms 13.142 ms
traceroute -I r2d2.company.com
traceroute to r2d2.company.com (10.246.20.141), 30 hops max, 60 byte packets
1 10.0.2.1 (10.0.2.1) 0.178 ms 0.139 ms 0.137 ms
2 * * *
3 te1-30-sjl1-2-cc01.company.com (10.246.100.81) 3.193 ms 3.174 ms 3.520 ms
4 te1-2-sjl2-2-cd02.companyy.com (10.246.100.62) 2.771 ms 2.853 ms 2.835 ms
5 * * *
6 * * *
7 * * *
8 * * *
9 * * *
10 * * *
11 * * *
12 * * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * * *
18 * * *
19 * * *
20 * * *
21 * * *
22 * * *
23 * * *
24 * * *
25 * * *
26 * * *
27 * * *
28 * * *
29 * * *
30 * * *
答案1
不要使用简单的 ping,而要使用traceroute -I
。您还可以使用 iptables 记录您的 ping 和带有传出接口的 telnet 数据包。
我对像您这样的奇怪配置的记忆有些模糊,但您很可能会发现,即使您指定了源 IP,也并不意味着您的数据包会通过该接口离开!我想说您的 ping 数据包会通过,但您的 telnet 数据包不会,并且有东西在正确地对它们进行 PATting 和反向路由。您说 eth0 有一个不同的路由表,并且您显示了该表,但我没有看到将数据包发送到该表的规则。执行ip rule list
以查看这些规则。
答案2
- 您要求
ping -I eth0
,因此 ping 将尝试使用10.254.185.16
作为源 IP 发送数据包。 - 根据您的 IP 规则,系统将使用主路由表并使用默认网关路由 ICMP 数据包
10.0.2.1
以到达172.217.1.238
。
=>10.0.2.1
无法从源 IP 访问10.254.185.16
(它们不在同一个子网中),因此出现无法访问的 ping 响应:
来自 company.com (10.254.185.16) icmp_seq=1 目标主机无法访问
为了解决这个问题,您可以添加以下内容ip rule
以确保正确的表查找:
ip rule add from 10.254.185.16/32 lookup eth0