本文聚焦 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 |
# /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。
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。
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
# 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
# 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
fe80::/10) 需指定介面:via fe80::1 dev eth0on-link: true# 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 核心原生工具鏈。