我在vmware中安装了三个Ubuntu虚拟机。 这些连接如下:
现在我想使用UBUNTU2作为路由器,这样当我在UBUNTU1中运行这些命令时,我得到了来自UBUNTU3的响应,命令如下:
ping 192.168.4.103 -I eth1 ping 192.168.4.103 -I eth2 ping 192.168.4.103 -I eth3
我从UBUNTU1获得了UBUNTU3的回应:
ping 192.168.1.101 ping 192.168.2.101 ping 192.168.3.101
请帮我解决一下这个。
我有这样的路线:
请忽略eth0接口。 它连接到所有的虚拟机。
UBUNTU1:
root@ubuntu:/home# ip route show default via 192.168.8.2 dev eth0 169.254.0.0/16 dev eth2 scope link metric 1000 192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.101 192.168.2.0/24 dev eth2 proto kernel scope link src 192.168.2.101 192.168.3.0/24 dev eth3 proto kernel scope link src 192.168.3.101 192.168.8.0/24 dev eth0 proto kernel scope link src 192.168.8.101 root@ubuntu:/home#
UBUNTU2:
root@ubuntu:/home# ip route show default via 192.168.8.2 dev eth0 metric 100 169.254.0.0/16 dev eth3 scope link metric 1000 192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.102 192.168.2.0/24 dev eth2 proto kernel scope link src 192.168.2.102 192.168.3.0/24 dev eth3 proto kernel scope link src 192.168.3.102 192.168.4.0/24 dev eth4 proto kernel scope link src 192.168.4.102 192.168.8.0/24 dev eth0 proto kernel scope link src 192.168.8.102 root@ubuntu:/home#
UBUNTU3:
root@ubuntu:/home# ip route show default via 192.168.8.2 dev eth0 metric 100 169.254.0.0/16 dev eth1 scope link metric 1000 192.168.4.0/24 dev eth1 proto kernel scope link src 192.168.4.103 192.168.8.0/24 dev eth0 proto kernel scope link src 192.168.8.103 root@ubuntu:/home#
EDIT1
在UBUNTU2中使用命令启用IP转发:
echo 1 > /proc/sys/net/ipv4/ip_forward
然后我将这些路线添加到UBUNTU3:
ip route add 192.168.1.0/24 scope global via 192.168.4.102 dev eth1 ip route add 192.168.2.0/24 scope global via 192.168.4.102 dev eth1 ip route add 192.168.3.0/24 scope global via 192.168.4.102 dev eth1
我把这些路线添加到UBUNTU1:
ip route add 192.168.4.0/24 scope global via 192.168.1.102 dev eth1
在这一刻,我能够从UBUNTU1 ping UBUNTU3,如下所示:
ping 192.168.4.103 -I eth1
而且,我能够从UBUNTU3上ping UBUNTU1,如下所示:
ping 192.168.1.101
但是,我试图在UBUNTU1中添加第二条路线:
ip route add 192.168.4.0/24 scope global via 192.168.2.102 dev eth2
命令失败,路由存在错误。
如何从机器的不同接口路由到同一networking(192.168.4.0/24)。
EDIT2:
我在UBUNTU1中添加了基于规则的路由:
1)在/etc/iproute2/rt_tables
创build额外的表名,现在文件包含:
255 local 254 main 253 default 0 unspec 1 net1 2 net2 3 net3
2)在RPDB中创build规则:
ip rule add from 192.168.1.101 table net1 ip rule add from 192.168.2.101 table net2 ip rule add from 192.168.3.101 table net3
3)在新的路由表中添加路由:
ip route add 192.168.4.0/24 table net1 scope global via 192.168.1.102 dev eth1 src 192.168.1.101 ip route add 192.168.1.0/24 table net1 scope link dev eth1 src 192.168.1.101 ip route add 192.168.4.0/24 table net2 scope global via 192.168.2.102 dev eth2 src 192.168.2.101 ip route add 192.168.2.0/24 table net2 scope link dev eth2 src 192.168.2.101 ip route add 192.168.4.0/24 table net3 scope global via 192.168.3.102 dev eth3 src 192.168.3.101 ip route add 192.168.3.0/24 table net3 scope link dev eth3 src 192.168.3.101
现在我可以从UBUNTU3 ping所有的UBUNUT1的eth1,eth2和eth3,但是我仍然无法ping通来自eth1的UBUNTU3的192.168.4.103(eth1)或者来自UBUNTU1的eth2或eth3。
我已经做到了!
在UBUNTU1上,我有我的/etc/iproute2/rt_tables
文件:
root@ubuntu-primary:/home# cat /etc/iproute2/rt_tables # # reserved values # 255 local 254 main 253 default 1 net1 2 net2 3 net3 0 unspec # # local # #1 inr.ruhep root@ubuntu-primary:/home#
我用命令创build了创build的规则:
ip rule add from 192.168.1.101 table net1 ip rule add oif eth1 table net1 ip rule add from 192.168.2.101 table net2 ip rule add oif eth2 table net2 ip rule add from 192.168.3.101 table net3 ip rule add oif eth3 table net3
现在我在RPDB中的规则如下:
root@ubuntu-primary:/home# ip rule show 0: from all lookup local 32759: from all oif eth3 lookup net3 32760: from all oif eth2 lookup net2 32761: from 192.168.3.101 lookup net3 32762: from 192.168.2.101 lookup net2 32763: from all oif eth1 lookup net1 32765: from 192.168.1.101 lookup net1 32766: from all lookup main 32767: from all lookup default root@ubuntu-primary:/home#
然后,我在每个表中创build了路由,net1,net2和net3用命令:
ip route add 192.168.4.0/24 table net1 scope global via 192.168.1.102 dev eth1 src 192.168.1.101 ip route add 192.168.1.0/24 table net1 scope link dev eth1 src 192.168.1.101 ip route add 192.168.4.0/24 table net2 scope global via 192.168.2.102 dev eth2 src 192.168.2.101 ip route add 192.168.2.0/24 table net2 scope link dev eth2 src 192.168.2.101 ip route add 192.168.4.0/24 table net3 scope global via 192.168.3.102 dev eth3 src 192.168.3.101 ip route add 192.168.3.0/24 table net3 scope link dev eth3 src 192.168.3.101
现在,所有三个新的路由表如下所示:
root@ubuntu-primary:/home# ip route show table net1 192.168.1.0/24 dev eth1 scope link src 192.168.1.101 192.168.4.0/24 via 192.168.1.102 dev eth1 src 192.168.1.101 root@ubuntu-primary:/home# root@ubuntu-primary:/home# ip route show table net2 192.168.2.0/24 dev eth2 scope link src 192.168.2.101 192.168.4.0/24 via 192.168.2.102 dev eth2 src 192.168.2.101 root@ubuntu-primary:/home# root@ubuntu-primary:/home# ip route show table net3 192.168.3.0/24 dev eth3 scope link src 192.168.3.101 192.168.4.0/24 via 192.168.3.102 dev eth3 src 192.168.3.101 root@ubuntu-primary:/home#
我的主要路由表如下所示:
root@ubuntu-primary:/home# ip route show table main 169.254.0.0/16 dev eth1 scope link metric 1000 192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.101 192.168.2.0/24 dev eth2 proto kernel scope link src 192.168.2.101 192.168.3.0/24 dev eth3 proto kernel scope link src 192.168.3.101 root@ubuntu-primary:/home#
然后我将这些路线添加到UBUNTU3:
ip route add 192.168.1.0/24 scope global via 192.168.4.102 dev eth1 ip route add 192.168.2.0/24 scope global via 192.168.4.102 dev eth1 ip route add 192.168.3.0/24 scope global via 192.168.4.102 dev eth1
而且我使用命令在UBUNTU2中启用IP转发:
echo 1 > /proc/sys/net/ipv4/ip_forward
现在一切工作正常。
我可以从UBUNTU3 ping所有的UBUNTU1的eth1,eth2和eth3,并且可以从UBUNTU1 ping到UBUNTU3的eth1,绑定ping应用到以下接口:
ping 192.168.4.103 -I eth1 ping 192.168.4.103 -I eth2 ping 192.168.4.103 -I eth3
您不能将两个不同的网关添加到同一个networking。
从ubuntu1开始,你必须定义哪个接口是到192.168.4.X的路由,你可以通过192.168.1.102或192.168.2.102或192.168.3.102。
一旦你设置路由192.168.1.102,路由192.168.2.102的唯一方法是删除192.168.1.102和添加192.168.1.102。
按照你的想法,你将所有的三个接口作为网关,现在系统如何select使用哪个网关? 随机一个? 也许你正在寻找一个dynamic路由协议,如路由或门控?