伺服器雙網卡 (eth0, eth1) 同屬一網段、共用一網關。預設路由僅走 eth0,導致 eth1 接收的封包回應也從 eth0 送出,造成非對稱路由、效能下降、連線中斷。需實現來源 IP 決定出介面:eth0 進來走 eth0 回,eth1 進來走 eth1 回。
建立兩張獨立路由表,依來源 IP 選表,強制原路返回。
vim /etc/iproute2/rt_tables
# 末尾新增
252 net_eth0
251 net_eth1
# eth0 表
ip route flush table net_eth0
ip route add default via 112.253.xx.5 dev eth0 src 112.253.xx.45 table net_eth0
ip rule add from 112.253.xx.45 table net_eth0 priority 100
# eth1 表
ip route flush table net_eth1
ip route add default via 112.253.xx.5 dev eth1 src 112.253.xx.160 table net_eth1
ip rule add from 112.253.xx.160 table net_eth1 priority 101
cat > /etc/sysconfig/network-scripts/route-eth0 <<'EOF'
default via 112.253.xx.5 dev eth0 src 112.253.xx.45 table net_eth0
EOF
cat > /etc/sysconfig/network-scripts/rule-eth0 <<'EOF'
from 112.253.xx.45 table net_eth0 priority 100
EOF
# eth1 同理,對應修改 IP 與介面名
ip rule list:確認規則優先順序正確。ip route show table net_eth0 / net_eth1:確認各表預設路由。iftop -i eth0 / iftop -i eth1:觀察雙向流量均衡分佈。
若 eth0 接電信、eth1 接聯通,網關不同。同樣建立兩表,但 default via 指向各自網關,規則依來源 IP 或 fwmark (配合 iptables 標記) 分流。此為多線 BGP 接入基礎架構。
sysctl -w net.ipv4.conf.all.rp_filter=2 # 鬆散模式
sysctl -w net.ipv4.conf.eth0.rp_filter=2
sysctl -w net.ipv4.conf.eth1.rp_filter=2
寫入 `/etc/sysctl.d/99-rp-filter.conf` 永久生效。net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2