Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux雙網卡雙閘道策略路由配置:不同網段流量隔離

編輯:Linux 教學

伺服器同時連接電信與網通兩條線路(不同網段),需實現「來源IP決定出口」:電信IP走電信閘道,網通IP走網通閘道。使用ip rule + ip route 策略路由完美解決。

場景假設

  • eth0:192.168.0.2/24,電信閘道 192.168.0.1
  • eth1:10.0.0.2/24,網通閘道 10.0.0.1

配置步驟

  1. 建立自定義路由表(/etc/iproute2/rt_tables):
    251 tel   # 電信表
    252 cnc   # 網通表
    
  2. 配置IP位址:
    ip addr add 192.168.0.2/24 dev eth0
    ip addr add 10.0.0.2/24 dev eth1
    
  3. 填充電信路由表 (tel):
    ip route add 192.168.0.0/24 dev eth0 src 192.168.0.2 table tel
    ip route add 127.0.0.0/8 dev lo table tel
    ip route add default via 192.168.0.1 dev eth0 table tel
    
  4. 填充網通路由表 (cnc):
    ip route add 10.0.0.0/24 dev eth1 src 10.0.0.2 table cnc
    ip route add 127.0.0.0/8 dev lo table cnc
    ip route add default via 10.0.0.1 dev eth1 table cnc
    
  5. 建立策略規則:根據來源IP選表:
    ip rule add from 192.168.0.2 table tel
    ip rule add from 10.0.0.2 table cnc
    # 優先級自動分配,可用 'ip rule list' 查看
    
  6. 永久生效:將命令寫入/etc/rc.local或NetworkManager dispatcher腳本、systemd服務,或使用/etc/sysconfig/network-scripts/rule-eth0、route-eth0(RHEL/CentOS慣例)。

驗證測試

# 從電信IP發起追蹤
traceroute -s 192.168.0.2 8.8.8.8   # 應走 192.168.0.1

# 從網通IP發起追蹤
traceroute -s 10.0.0.2 8.8.8.8      # 應走 10.0.0.1

# 查看規則
ip rule list
ip route list table tel
ip route list table cnc

進階:批量網段策略

ip rule add from 192.168.0.0/24 table tel
ip rule add from 10.0.0.0/24 table cnc
# 刪除舊單IP規則
ip rule del from 192.168.0.2 table tel
ip rule del from 10.0.0.2 table cnc

常見坑點

  • rp_filter嚴格模式導致非對稱路由丟包:sysctl -w net.ipv4.conf.all.rp_filter=2(鬆散模式)或=0(關閉)。
  • 雙預設閘道未配置策略路由時,核心僅用主路由表第一條,造成回包走錯介面、連線中斷。
  • NAT/防火牆需同步標記連線(iptables -t mangle -A PREROUTING -i eth0 -j CONNMARK --restore-mark等)。

策略路由是Linux多線路負載均衡、流量隔離的核心技術,掌握ip rule/route組合拳,輕松駕馭複雜網路拓撲。

Copyright © Windows教程網 All Rights Reserved