雙線機房提供電信、聯通兩條獨立線路與 IP。若採用**靜態路由**手動維護聯通網段,缺點明顯:
核心邏輯:根據來源 IP 決定回包走哪條線路。電信 IP 進來 → 走電信閘道回包;聯通 IP 進來 → 走聯通閘道回包。
| 項目 | 電信 | 聯通 |
|---|---|---|
| 閘道 | 59.83.100.1 | 221.214.100.1 |
| 本機 IP | 59.83.100.10 (eth0) | 221.214.100.10 (eth1) |
# 編輯 /etc/iproute2/rt_tables 252 tel # 電信路由表 251 cnc # 聯通路由表
#!/bin/bash # 預設路由走電信 ip route replace default via 59.83.100.1 dev eth0 # === 電信路由表 tel === ip route flush table tel ip route add default via 59.83.100.1 dev eth0 src 59.83.100.10 table tel ip rule add from 59.83.100.10 table tel # === 聯通路由表 cnc === ip route flush table cnc ip route add default via 221.214.100.1 dev eth1 src 221.214.100.10 table cnc ip rule add from 221.214.100.10 table cnc
指令解析:
-ip route add ... table tel:在 tel 表建立預設路由。
-ip rule add from IP table tel:來源 IP 為電信 IP 時,查 tel 表。
-src參數確保外發封包來源 IP 正確。
# 查看規則 ip rule show # 查看各表路由 ip route show table tel ip route show table cnc # 測試:從電信/聯通客戶端分別訪問,抓包確認回包介面正確
ip route add default scope global nexthop via $GW1 dev $IF1 weight 1 nexthop via $GW2 dev $IF2 weight 1 實現預設路由負載分擔(需核心支援)。策略路由完美解決雙線非對稱路由問題,配置一次永久生效,無需維護龐大靜態路由表。關鍵在於 ip rule 根據來源 IP 選擇路由表,配合 src 確保回包路徑正確。