指令語法
route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]
參數說明
| 參數 | 說明 |
-f | 清除所有閘道項目的路由表 |
-p | 搭配 add 使用,使路由永久保存(重開機不失效) |
Command | 指定操作:Add/Change/Delete/Print |
Destination | 網路目標位址(如 10.41.0.0、0.0.0.0 代表預設路由) |
mask Netmask | 子網遮罩(如 255.255.255.0) |
Gateway | 下一跳閘道 IP 位址 |
metric Metric | 成本值(1-9999),路由選擇時參考 |
if Interface | 指定介面索引(可用 route print 查看) |
常用範例
1. 顯示完整路由表
route print
2. 顯示特定網段路由
route print 10.*
3. 新增預設路由
route add 0.0.0.0 mask 0.0.0.0 192.168.12.1
4. 新增靜態路由
route add 10.41.0.0 mask 255.255.0.0 10.27.0.1
5. 新增永久靜態路由
route -p add 10.41.0.0 mask 255.255.0.0 10.27.0.1
6. 指定度量值
route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 metric 7
7. 指定介面索引
route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 if 0x3
8. 刪除路由
route delete 10.41.0.0 mask 255.255.0.0
9. 刪除特定網段所有路由
route delete 10.*
10. 修改路由下一跳
route change 10.41.0.0 mask 255.255.0.0 10.27.0.25
現代替代工具:ip route
route 屬於 net-tools 套件,已漸漸被 iproute2 的 ip route 取代。對應關係如下:
| route 指令 | ip route 對應指令 |
route add ... | ip route add ... |
route delete ... | ip route del ... |
route change ... | ip route replace ... |
route print | ip route show |
route -p add ... | 編輯 /etc/sysconfig/network-scripts/route-eth0 或 /etc/network/interfaces |
永久生效配置建議
- RedHat/CentOS:在
/etc/sysconfig/network-scripts/route-eth0 加入 10.41.0.0/16 via 10.27.0.1
- Debian/Ubuntu:在
/etc/network/interfaces 的介面區塊加入 up ip route add 10.41.0.0/16 via 10.27.0.1
注意事項
- 修改路由表需 root 權限。
- 使用
metric 可控制多條路由的優先順序,數值越小優先級越高。
- 介面索引可用
route print 或 ip link show 取得。
- 生產環境建議改用
ip route 並配合永久配置檔,避免重開機失效。