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 教學

雙網卡服務器常見於 DMZ 隔離、存儲網絡分離、多 ISP 接入等場景。本文以實例演示如何通過 route/ip route 命令配置靜態路由,實現不同目標網段走不同物理接口,並解析默認網關選擇策略與持久化方案。

場景假設

  • eth0:192.168.10.123/24,連接辦公網/管理網
  • eth1:192.168.20.231/24,連接業務網/存儲網

基礎路由添加

# 目標網段 192.168.10.0/24 走 eth0
route add -net 192.168.10.0 netmask 255.255.255.0 dev eth0

# 目標網段 192.168.20.0/24 走 eth1
route add -net 192.168.20.0 netmask 255.255.255.0 dev eth1

現代語法(推薦 ip route,route 已棄用):

ip route add 192.168.10.0/24 dev eth0
ip route add 192.168.20.0/24 dev eth1

默認網關策略:兩種等效思路

思路 A:顯式指定業務網關,其餘走管理網關

# 假設管理網關 192.168.10.1,業務網關 192.168.20.1
ip route add default via 192.168.10.1 dev eth0 metric 100
ip route add default via 192.168.20.1 dev eth1 metric 200

Metric 值決定優先級,數值越小優先級越高。此例中,非 192.168.20.0/24 流量走 eth0(管理網關),192.168.20.0/24 因精確匹配走 eth1。

思路 B:僅配置一條默認路由,另一網段靜態路由覆蓋

ip route add default via 192.168.10.1 dev eth0
ip route add 192.168.20.0/24 dev eth1

效果相同:最長前綴匹配原則使 192.168.20.0/24 走 eth1,其餘走默認網關 eth0。

路由查看與驗證

# 顯示數值格式路由表(不解析主機名,速度快)
route -n
# 或
ip route show

# 驗證具體目標走向
ip route get 192.168.20.50
ip route get 8.8.8.8

刪除路由

route del -net 192.168.10.0 netmask 255.255.255.0
# 或
ip route del 192.168.10.0/24

持久化配置:各發行版差異

進階:策略路由(基於源 IP/標記分流)

當需要「從 192.168.10.x 發出的包走 eth0,從 192.168.20.x 發出的包走 eth1」時,需引入策略路由:

# 建立獨立路由表
echo "200 mgmt" >> /etc/iproute2/rt_tables
echo "201 biz"  >> /etc/iproute2/rt_tables

# 填充路由表
ip route add 192.168.10.0/24 dev eth0 table mgmt
ip route add default via 192.168.10.1 dev eth0 table mgmt
ip route add 192.168.20.0/24 dev eth1 table biz
ip route add default via 192.168.20.1 dev eth1 table biz

# 規則匹配源 IP 選表
ip rule add from 192.168.10.0/24 table mgmt priority 100
ip rule add from 192.168.20.0/24 table biz priority 100

避坑指南:route 命令不支援 metric/priority,複雜場景必須用 ip route;雙默認網關務必設置 metric 避免路由震盪;配置變更後 ip route flush cache 清理緩存;生產環境變更前必須備份並有回滾方案。

Copyright © Windows教程網 All Rights Reserved
發行版配置文件/目錄示例
RHEL/CentOS/Fedora/etc/sysconfig/network-scripts/route-ethX192.168.20.0/24 dev eth1
default via 192.168.10.1 dev eth0
Debian/Ubuntu (ifupdown)/etc/network/interfacesup ip route add 192.168.20.0/24 dev eth1
up ip route add default via 192.168.10.1 dev eth0
Ubuntu 18.04+ (Netplan)/etc/netplan/*.yamlroutes:
- to: 192.168.20.0/24
via: 192.168.20.1
- to: default
via: 192.168.10.1
Arch/systemd-networkd/etc/systemd/network/*.network[Route]
Gateway=192.168.10.1
Metric=100