利用路由表设置多网卡默认路由
Example:
电信IP:1.1.1.2 Gateway:1.1.1.1
联通IP:2.2.2.2 Gateway:2.2.2.1
移动IP:3.3.3.2 Gateway:3.3.3.1
- 配置路由表字段
vim /etc/iproute2/rt_tables # # reserved values # 255 local 254 main 253 default 0 unspec # # local # #1 inr.ruhep 252 dx 251 lt 250 yd
- 编写路由脚本
vim route.sh #!/bin/env sh ip route flush table dx ip rule add from 1.1.1.2 table dx ip route add default via 1.1.1.1 dev eth0 src 1.1.1.2 table dx ip route flush table lt ip rule add from 2.2.2.2 table lt ip route add 0.0.0.0/0 via 2.2.2.1 dev eth1 src 2.2.2.2 table lt ip route flush table yd ip rule add from 3.3.3.2 table yd ip route add 0.0.0.0/0 via 3.3.3.1 dev eth2 src 3.3.3.2 table yd
- 添加执行权限
chmod +x route.sh /etc/rc.d/rc.local
- 添加开机启动项
echo "/root/route.sh" >> /etc/rc.d/rc.local
-
ip route flush table 100 ip rule add from 1.1.1.2 table 100 ip route add default via 1.1.1.1 dev em1 src 1.1.1.2 table 100 prio 50 ip route flush table 200 ip rule add from 2.2.2.2 table 200 ip route add default via 2.2.2.1 dev em2 src 2.2.2.2 table 200 prio 50