[root@localhost ~]# ip route show table all 10.1.1.0/24 dev eth0 proto kernel scope link src 10.1.1.247 10.1.2.0/24 dev eth1 proto kernel scope link src 10.1.2.239 169.254.0.0/16 dev eth0 scope link metric 1002 169.254.0.0/16 dev eth1 scope link metric 1003 default via 10.1.1.1 dev eth0 [root@localhost ~]# ip rule show 0: from all lookup local 32766: from all lookup main 32767: from all lookup default
$ cat /etc/iproute2/rt_tables # # reserved values # 255 local 254 main 253 default 252 lan1 251 lan2 0 unspec
然后再添加ip route和ip rule
1 2 3 4 5 6
ip route flush table lan1 ip route add default via 10.1.1.1 dev eth0 src 10.1.1.247 table lan1 ip rule add from 10.1.1.247 table lan1 ip route flush table lan2 ip route add default via 10.1.2.1 dev eth1 src 10.1.2.239 table lan2 ip rule add from 10.1.2.239 table lan2
这个时候我们再查看路由表如下
1 2 3 4 5 6 7
[root@localhost ~]# ip route show all 10.1.1.0/24 dev eth0 proto kernel scope link src 10.1.1.247 10.1.2.0/24 dev eth1 proto kernel scope link src 10.1.2.239 169.254.0.0/16 dev eth0 scope link metric 1002 169.254.0.0/16 dev eth1 scope link metric 1003 default via 10.1.1.1 dev eth0 [root@localhost ~]# ip rule show 0: from all lookup local 32764: from 10.1.2.239 lookup lan2 32765: from 10.1.1.247 lookup lan1 32766: from all lookup main 32767: from all lookup default
ip route flush table lan1 ip route add default via 10.1.1.1 dev eth0 src 10.1.1.247 table lan1 ip rule add from 10.1.1.247 table lan1 ip rule add from 10.1.1.247 to 10.1.1.0/24 table main ip route flush table lan2 ip route add default via 10.1.2.1 dev eth1 src 10.1.2.239 table lan2 ip rule add from 10.1.2.239 table lan2 ip rule add from 10.1.2.239 to 10.1.2.0/24 table main
然后我们查看路由表如下:
1 2 3 4 5 6 7 8 9 10 11 12
[root@localhost ~]# ip route show table all default via 10.1.1.1 dev eth0 table lan1 src 10.1.1.247 10.1.1.0/24 dev eth0 proto kernel scope link src 10.1.1.247 10.1.2.0/24 dev eth1 proto kernel scope link src 10.1.2.239 169.254.0.0/16 dev eth0 scope link metric 1002 169.254.0.0/16 dev eth1 scope link metric 1003 default via 10.1.1.1 dev eth0 default via 10.1.2.1 dev eth1 table lan2 src 10.1.2.239 [root@localhost ~]# ip rule show 0: from all lookup local 32762: from 10.1.2.239 to 10.1.2.0/24 lookup main 32763: from 10.1.2.239 lookup lan2 32764: from 10.1.1.247 to 10.1.1.0/24 lookup main 32765: from 10.1.1.247 lookup lan1 32766: from all lookup main 32767: from all lookup default