CentOS 兩張網卡接兩個 ISP 的 Route 設定
- 一台主機的兩張網卡各連接一個 ISP 的網路
- 希望透過哪張網卡過來的連線, 可以直接由那張網卡回去
- 這台主機的相關環境如下:
- eth0 : 192.168.11.248 / 255.255.255.0 (Gateway pdc.e2160 : 192.168.11.251)
- eth1 : 192.168.12.246 / 255.255.255.0 (Gateway c2q.q9400 : 192.168.12.250)
- 預設 default gateway 走 192.168.11.251 有異常時, 自動改走 192.168.12.250
設定程序
- 編輯 rt_tables
vi /etc/iproute2/rt_tables
: # # local # #1 inr.ruhep 1 c2q.q9400 2 pdc.e2160
- 定義 route 規則
ip route add 192.168.11.0/24 dev eth0 src 192.168.11.248 table pdc.e2160 ip route add default via 192.168.11.251 table pdc.e2160 ip rule add from 192.168.11.248 table pdc.e2160 ip route add 192.168.12.0/24 dev eth1 src 192.168.12.246 table c2q.q9400 ip route add default via 192.168.12.250 table c2q.q9400 ip rule add from 192.168.12.246 table c2q.q9400 ip route add default scope global nexthop via 192.168.11.251 dev eth0 weight 1 nexthop via 192.168.12.250 dev eth1 weight 4
- 編輯 route-ethx rule-ethx
vi /etc/sysconfig/network-scripts/route-eth0
192.168.11.0/24 dev eth0 src 192.168.11.248 table pdc.e2160 default via 192.168.11.251 table pdc.e2160
vi /etc/sysconfig/network-scripts/rule-eth0
from 192.168.11.248 table pdc.e2160
vi /etc/sysconfig/network-scripts/route-eth1
192.168.12.0/24 dev eth1 src 192.168.12.246 table c2q.q9400 default via 192.168.12.250 table c2q.q9400 default scope global nexthop via 192.168.11.251 dev eth0 weight 1 nexthop via 192.168.12.250 dev eth1 weight 4
vi /etc/sysconfig/network-scripts/rule-eth1
from 192.168.12.246 table c2q.q9400
- 檢視設定結果
[root@xen-server ~]# ip route show 192.168.12.0/24 dev eth1 proto kernel scope link src 192.168.12.246 192.168.11.0/24 dev eth0 proto kernel scope link src 192.168.11.248 169.254.0.0/16 dev eth1 scope link default nexthop via 192.168.11.251 dev eth0 weight 1 nexthop via 192.168.12.250 dev eth1 weight 4
發生異常強制設定程序
- 假設 eth0 : 192.168.11.248 / 255.255.255.0 (Gateway pdc.e2160 : 192.168.11.251) 這個 ISP 確定短時間無法修復, 簡單的拿掉設定方式如下:
vi /etc/sysconfig/network-scripts/route-eth1
192.168.12.0/24 dev eth1 src 192.168.12.246 table c2q.q9400 default via 192.168.12.250 table c2q.q9400 #default scope global nexthop via 192.168.11.251 dev eth0 weight 1 nexthop via 192.168.12.250 dev eth1 weight 4 default scope global nexthop via 192.168.12.250 dev eth1 weight 4
service network restart
- 檢視設定結果
[root@xen-server ~]# ip route show 192.168.12.0/24 dev eth1 proto kernel scope link src 192.168.12.246 192.168.11.0/24 dev eth0 proto kernel scope link src 192.168.11.248 169.254.0.0/16 dev eth1 scope link default via 192.168.12.250 dev eth1