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 靜態路由配置詳解:ip route 現代語法與永久化最佳實踐

編輯:Linux 教學

本文聚焦 ip route (iproute2) 現代工具鏈,提供從臨時操作到生產環境永久化、策略路由、IPv6、除錯的完整實戰指南,取代舊有 route 指令。

核心語法結構

ip route [add|del|change|replace|show] 目標 [via 閘道] [dev 介面] [metric 數值] [scope 值] [table 表名]

臨時操作速查

場景指令
新增網路路由ip route add 10.10.10.0/24 via 192.168.1.254 dev eth0
新增主機路由ip route add 192.168.100.5 via 192.168.1.254
新增預設閘道ip route add default via 192.168.1.1
指定 metric (優先序)ip route add 10.0.0.0/8 via 192.168.1.254 metric 100
刪除路由ip route del 10.10.10.0/24
修改閘道ip route change 10.10.10.0/24 via 192.168.1.253
替換 (不存在則新增)ip route replace 10.10.10.0/24 via 192.168.1.253
查看主路由表ip route show
查看含快取/統計ip -s route show
查詢實際轉發路徑ip route get 8.8.8.8

永久化:發行版原生方案 (生產環境標準)

RHEL/CentOS/Fedora/Rocky/AlmaLinux: /etc/sysconfig/network-scripts/route-介面

# /etc/sysconfig/network-scripts/route-eth0
# 格式:目標 via 閘道 dev 介面
192.168.10.0/24 via 192.168.1.254 dev eth0
192.168.20.0/24 via 192.168.1.254 dev eth0
default via 192.168.1.1 dev eth0

# IPv6
2001:db8:1::/64 via fe80::1 dev eth0

網路服務重啟自動套用:systemctl restart network 或 nmcli con reload。

Debian/Ubuntu (Netplan, 18.04+): /etc/netplan/*.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      addresses: [192.168.1.10/24]
      gateway4: 192.168.1.1
      routes:
        - to: 192.168.10.0/24
          via: 192.168.1.254
        - to: 192.168.20.0/24
          via: 192.168.1.254
      # IPv6
      addresses:
        - 2001:db8::10/64
      gateway6: 2001:db8::1
      routes:
        - to: 2001:db8:1::/64
          via: fe80::1
          on-link: true  # 鏈路本地閘道需加此參數
      route-metric: 100

套用:netplan apply。

Debian/Ubuntu (傳統 ifupdown): /etc/network/interfaces

auto eth0
iface eth0 inet static
    address 192.168.1.10/24
    gateway 192.168.1.1
    up ip route add 192.168.10.0/24 via 192.168.1.254 dev eth0
    down ip route del 192.168.10.0/24 via 192.168.1.254 dev eth0

通用後備:/etc/rc.local (systemd 服務)

# 1. 建立服務
cat > /etc/systemd/system/rc-local.service <<'EOF'
[Unit]
Description=/etc/rc.local Compatibility
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target
EOF

# 2. 編輯 /etc/rc.local
chmod +x /etc/rc.local
cat > /etc/rc.local <<'EOF'
#!/bin/bash
ip route add 192.168.10.0/24 via 192.168.1.254 dev eth0
ip route add 192.168.20.0/24 via 192.168.1.254 dev eth0
exit 0
EOF

# 3. 啟用
systemctl enable --now rc-local.service

進階:Policy Routing (策略路由 / 多閘道)

# 1. 定義自訂路由表
echo "100 isp1" >> /etc/iproute2/rt_tables
echo "101 isp2" >> /etc/iproute2/rt_tables

# 2. 表 100 對應 ISP1
ip route add default via 192.168.1.1 dev eth0 table 100
ip route add 192.168.1.0/24 dev eth0 table 100
ip rule add from 192.168.1.10 table 100 prio 100

# 3. 表 101 對應 ISP2
ip route add default via 192.168.2.1 dev eth1 table 101
ip route add 192.168.2.0/24 dev eth1 table 101
ip rule add from 192.168.2.10 table 101 prio 101

# 4. 永久化 (route-eth0 格式不支援 rule,需 rc.local 或 NetworkManager dispatcher)
# NetworkManager dispatcher 腳本 /etc/NetworkManager/dispatcher.d/99-policy-routing
#!/bin/bash
if [ "$2" = "up" ]; then
  ip rule add from 192.168.1.10 table 100 prio 100
  ip rule add from 192.168.2.10 table 101 prio 101
fi

IPv6 特殊處理

  • 鏈路本地閘道 (fe80::/10) 需指定介面:via fe80::1 dev eth0
  • Netplan 需 on-link: true
  • 預設路由常由 RA (Router Advertisement) 自動獲得,靜態添加需關閉 RA 或調整 metric

除錯檢查清單

# 1. 閘道 L2 可達?
arping -I eth0 192.168.1.254
# 2. 路由表確認
ip route show
ip route get 目標IP
# 3. 策略規則
ip rule show
# 4. 封包實測
tcpdump -i eth0 -n host 目標IP
traceroute 目標IP

結語

掌握 ip route 現代語法、發行版原生永久化配置、Policy Routing 多閘道 三大核心,即可應對從單機到複雜網路拓撲的所有靜態路由需求。棄用 route、netstat、ifconfig 舊三件套,擁抱 ip、ss、tc 核心原生工具鏈。

Copyright © Windows教程網 All Rights Reserved