现在的位置: 首页 > 综合 > 正文

Linux 不同网段两台主机相互ping通

2019年03月11日 ⁄ 综合 ⁄ 共 1112字 ⁄ 字号 评论关闭
刚起的虚拟机无法相互ping通,属于两个网段,中间通过路由相连接

比如:10.11.12.202 ping 10.11.13.102

首先,ssh到10.11.12.202中,
route -n
查看路由表项,发现没有默认路由:
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.11.12.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0
添加默认路由:
sudo route add default gw 10.11.12.200 dev eth0
设置默认网关gw 10.11.12.200,即连到别的子网的需要通过该网关,这个网关也是路由连接到本子网的网关地址,dev eth0强制设置为eth0口
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.11.12.200    0.0.0.0         UG    0      0        0 eth0
10.11.12.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0

这时候还不能ping通,因为10.11.13.102的网关没设置
ssh到10.11.13.102中,
route -n
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.11.13.1      0.0.0.0         UG    0      0        0 eth0
10.11.13.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0

发现默认网关为10.11.13.1,不是正确的网关,我本地该网段连接外网的网关为10.11.13.100
添加默认网关 并删除原网关
sudo route add default gw 10.11.13.100 dev eth0
sudo route del default gw 10.11.13.1
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.11.13.100    0.0.0.0         UG    0      0        0 eth0
10.11.13.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0
OK 可以ping通了:
[fedora@inst2-8 ~]$ ping 10.11.12.202
PING 10.11.12.202 (10.11.12.202) 56(84) bytes of data.
64 bytes from 10.11.12.202: icmp_seq=1 ttl=63 time=0.980 ms
64 bytes from 10.11.12.202: icmp_seq=2 ttl=63 time=0.951 ms
64 bytes from 10.11.12.202: icmp_seq=3 ttl=63 time=0.672 ms



抱歉!评论已关闭.