然后添加路由规则,让所有通向网通的数据查询标示为11的路由表:
ip rule add to 60.0.0.0/13 table 11
ip rule add to 60.8.0.0/15 table 11
ip rule add to 60.10.0.0/16 table 11
这些规则,大家可以借鉴一下routeros的,网上很多,我就不说了,没有的可以跟我要。我的是MMS的,应该是比较全了。
经过我们的设置所有通向网通的数据会查询路由表11,而通向其他的地方的数据,通向电信。(如果还有其他的线路,再增加路由表,再增加策略就可以了)要注意的是,在配置网卡的时候,先不要配置网关,不要使用老的ifconfig命令来配置网关,而要使用iproute2来配置网关,否则会造成冲突,或者 使用 ip route replace 命令来替换 ip route add 命令。
还有一个做法是:
Internet
| |
| |
电信 网通
| |
|
交换机
|
eth1 eth2
Server
eth0
|
_____________
客户机 客户机 客户机 客户机192.168.0.0/24 网关192.168.0.254
其中eth0 ip 192.168.0.0/24
eth1 222.168.1.3/255.255.255.252
eth2 218.62.3.3/255.255.255.252
默认网关为网通的218.62.3.2
加上ip route add的电信网关 222.168.1.2
iptables -t nat -I POSTROUTING -s 192.168.0.0/24 -d $电信ip -j SNAT --to 222.168.1.3
***
***
***
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to 218.62.3.3
这样呢就实现了一个网段的电信网通自动切换
前期测试:如果单独切换ip,内网同时并存192.168.0.254/24 192.168.2.254/24两个网关,速度很稳定,可以实现预期效果
# echo "200 DIANXIN" >;>; /etc/iproute2/rt_table(这个是添加到文件,执行一次即可)
# ip route replace default via 222.168.1.2 table DIANXIN
# ip rule add fwmark 1 table DIANXIN(这个注意顺序,用ip rule可以查看)
# iptables -t nat -F
# iptables -t mangle -F
# iptables -t mangle -A PREROUTING -i eth0 -s 192.168.0.0/24 -d 222.222.5.0/15 -j MARK --set-mark 1
# iptables -t mangle -A PREROUTING -i eth0 -s 192.168.0.0/24 -d 222.240.0.0/13 -j MARK --set-mark 1
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d 222.222.5.0/15 -j SNAT --to $DIANXIN
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d 222.240.0.0/13 -j SNAT --to $DIANXIN
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to $接网通线路网卡的地址 # ip route flush cache
这样的结果,是访问222.222.5.0/15和222.240.0.0/13走电信网卡、电信路由,伪装成电信出口地址,其他默认网通。
更多内容请看PCdog.com--FTP服务器 Linux服务器配置 操作系统专题
