Vinc3nt's Life

Linux 筆記:看懂 ip route 與 route

2025-01-16
develop
linux
route-table
route
ip-route
最後更新:2025-01-26
7分鐘
1275字

前陣子因為實驗需求,我經常使用 ip routeroute 來操作與查看 routing table。在這段期間,我做了不少研究和實驗,現在趁著記憶還很清晰,把這些知識整理成文章分享給大家。

ip route 路由表記錄格式

每條 route entry 的基本格式如下: <destination> via <gateway> dev <interface> proto <protocol> src <source> <options>

讓我們來看看各個欄位的含義:

  1. default:預設路由,當封包無法匹配到其他更具體的 routing rules 時,就會套用這條路由。
  2. via <gateway>:指定 next hop 的 IP address,系統會透過這個 gateway 來轉發封包。
  3. dev <interface>:指定用來傳送該路由流量的 network interface,例如 eth0
  4. proto <protocol>:標示路由的來源,最常見的有:
    • kernel:系統開機時由 kernel 自動建立。就算修改這個條目,系統很有可能重新建立它
    • dhcp:透過 DHCP protocol 動態取得
    • static:管理員手動設定
  5. scope:定義路由的作用範圍:
    • global:具有 gateway 的 unicast route,可轉發到外部網路
    • link:local direct route,不需經過 gateway 即可到達
    • host:僅針對 local host 的路由
  6. metric:route priority,數字越小代表優先級越高
  7. src:指定 source IP address,用於設定 local interface 在該路由下使用的 IP。換句話說,當你的電腦要使用這條路由傳送封包時,它會使用這個 IP 位址作為封包的「發送者地址」

特別要注意的是,src option 僅會影響該 host 本身產生的網路封包。對於那些經由本機轉發的封包,因為它們本身就已經帶有 source IP,所以 src 參數的設定不會對它們造成影響,除非你特別使用 NAT 來進行修改。

ip routeroute -n 輸出欄位的對照與說明

在 Linux 系統中,ip routeroute -n 都是查看 routing table 的常見工具。說到這兩個指令的差異,雖然就排版的角度來看,我個人比較喜歡 route -n 的呈現方式,但不得不說 ip route 確實有它的優勢:不僅在大多數 Linux distributions 中都是預設安裝的,而且還能提供更豐富的路由資訊。

讓我們透過實際的例子來看看,這兩個指令的輸出該如何轉換和解讀:

default

  • route -n 的輸出:
    0.0.0.0 172.22.112.1 0.0.0.0 UG 0 0 0 eth0
  • ip route 的輸出:
    default via 172.22.112.1 dev eth0 proto kernel

這條路由的含義是:當封包無法匹配到其他路由規則時(也就是預設路由),系統會將流量透過 eth0 介面發送到閘道器 172.22.112.1

讓我們來看看欄位的對應關係:

  • Destination: route -n 中的 0.0.0.0 對應到 ip route 中的 default 關鍵字,兩者都代表預設路由。
  • Gateway: 兩個指令都清楚地標示出閘道器的 IP 位址是 172.22.112.1(分別以 Gatewayvia 表示)。
  • Genmask:route -n 中使用 0.0.0.0 作為子網路遮罩,表示會匹配所有 IP。而在 ip route 中,這個概念已經包含在 default 關鍵字裡了。
  • Flags: route -n 中的 UG 旗標包含兩層含義:
    • U(Up):表示路由已啟用
    • G(Gateway):表示此路由需要透過閘道器轉發
  • Iface: 兩者都指出使用的是 eth0 介面(分別以 Ifacedev 表示)。
  • Others: MetricRefUse 這些資訊在 ip route 的預設輸出中是看不到的。

再來看另一個例子:

default

  • route -n 的輸出:
    172.18.0.0 0.0.0.0 255.255.255.0 U 0 0 0 my-br
  • ip route 的輸出:
    172.18.0.0/24 dev my-br proto kernel scope link src 172.18.0.1 linkdown

這條路由告訴我們:前往 172.18.0.0/24 網路的流量應該透過 my-br interface 傳送,並使用 172.18.0.1 作為來源 IP。不過要特別注意的是,因為 linkdown 狀態顯示該 interface 目前處於關閉狀態,所以這條路由實際上是無法使用的。

讓我們來看看欄位的對應關係:

  • Destination: 兩者都指向 172.18.0.0 這個網段,但 ip route 採用更現代的 CIDR notation /24
  • Gateway: route -n 中的 0.0.0.0 表示這是 directly connected network,不需要 gateway。同樣地,ip route 中沒有 via keyword 也是相同的意思。
  • Netmask: 255.255.255.0 等同於 CIDR 的 /24 notation。
  • Flags: 這裡就能看出 ip route 的優勢了 —— 它不只顯示了基本的 U(Up)status,還額外標示出 linkdown status,讓我們立即知道 interface 已經關閉。
  • Others:
    • src 172.18.0.1:指定 interface 的 IP address
    • scope link:表示這是 local direct route
    • proto kernel:說明這條 route 是由 kernel 所設定的

絕大多數情況,route -n 輸出的 Genmask = 255.255.255.255 時,Flags 會包含 H。對應 route -n 輸出的欄位是 subnet mask = /32。意思是這個路由規則,只服務 1 個特定 IP。

ip route 常用指令

Terminal window
1
# 新增一條 route entry
2
# ip route add [type] [prefix] via [next-hop] dev [interface] [table table-id] [src source-ip]
3
ip route add 192.168.0.0/24 via 192.168.1.1 dev eth0
4
5
# 移除特定的 route entry
6
# ip route delete <route-spec>
7
# <route-spec> 不需要照順序,也不需要全部條件命中
8
ip route delete default via 192.168.1.1 dev eth0
9
10
# 顯示 routing table 的內容
11
# 預設只會顯示 main table(table id 254)
12
ip route show table all
13
# 也可以指定特定 table
14
ip route show table local
15
2 collapsed lines
16
# 清除 routing cache
17
ip route flush cache
本文標題:Linux 筆記:看懂 ip route 與 route
文章作者:Vincent Lin
發布時間:2025-01-16