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 route 指令完整使用手冊:路由表查看、新增、刪除與修改

編輯:Linux 教學

指令語法

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 printip 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

注意事項

  1. 修改路由表需 root 權限。
  2. 使用 metric 可控制多條路由的優先順序,數值越小優先級越高。
  3. 介面索引可用 route print 或 ip link show 取得。
  4. 生產環境建議改用 ip route 並配合永久配置檔,避免重開機失效。
Copyright © Windows教程網 All Rights Reserved