技术指南

How Routers Choose a Route: Longest Prefix Match, Administrative Distance, and Metrics

作者: Subnetica · 发布于 2026年8月24日 · 更新于 2026年8月24日

When several routes could reach a destination, the answer depends on whether you are comparing different prefixes, different sources for the same prefix, or different paths inside one routing protocol. Keeping those decisions separate prevents many route-selection mistakes.

1. Longest prefix match chooses the matching destination range

Suppose the forwarding table contains:

0.0.0.0/0
10.0.0.0/8
10.20.0.0/16
10.20.30.0/24

A packet for 10.20.30.42 matches all four entries, but the /24 is the most specific match and wins. A packet for 10.20.31.42 matches the default, /8, and /16, so the /16 wins. This forwarding lookup happens after the system decides which candidate for each prefix is installed.

A more-specific prefix can beat a less-specific route regardless of the less-specific route’s source preference. A static 10.0.0.0/8 does not override an installed 10.20.30.0/24 merely because static routes are preferred in a particular vendor’s table.

2. Source preference compares routes to the same prefix

If static, OSPF, and BGP all offer 10.20.30.0/24, the implementation needs a rule for preferring one source. Cisco IOS calls this administrative distance and assigns familiar defaults such as connected 0, static 1, OSPF 110, and RIP 120. Those numbers are Cisco defaults, not a universal law.

FRRouting has its own administrative-distance configuration, while Linux route lookup exposes metrics, tables, and policy routing rather than Cisco’s model. Do not copy a Cisco number into a Linux explanation without identifying the layer and implementation. In FRR, show ip route shows the selected RIB view; on Linux, ip route shows kernel routes and ip rule reveals policy-table selection.

3. Protocol metrics choose among paths from one protocol

OSPF commonly sums interface costs; RIP compares hop count and treats a path beyond its finite maximum as unreachable. The exact metric calculation and tie-breaking rules belong to the protocol, not to longest-prefix match.

Which route wins?

Specificity

Routes are 172.16.0.0/12 via R1 and 172.16.20.0/24 via R2. Destination 172.16.20.9 uses R2. Destination 172.17.20.9 uses R1.

Same prefix, different sources

Static and OSPF both offer 192.0.2.0/24 in Cisco IOS. With default distances, static wins. If OSPF instead offers 192.0.2.0/25, that more-specific route wins for addresses in the /25 even though OSPF is less preferred as a source.

Equal-cost paths

Two OSPF paths have equal cost and pass the platform’s ECMP rules. The device can install multiple next hops and distribute flows across them. Per-packet versus per-flow behavior depends on platform and configuration.

Next-hop resolution, RIB, and FIB

A selected route still needs a usable next hop. Recursive resolution may use another route to reach that address; if resolution fails, the route may not enter the forwarding table. A RIB is the selected control-plane view; a FIB is the installed forwarding representation. The FRRouting and Linux routing-table guide explains the protocol-daemon, Zebra, and kernel boundaries.

# Cisco-style
show ip route
show ip route 10.20.30.42
show ip cef 10.20.30.42

# Linux
ip route
ip route get 10.20.30.42
ip rule
ip route show table all

Static routes are manually supplied candidates; dynamic protocols learn candidates and withdraw or replace them. A default route is simply the least-specific /0 fallback.

When the route exists but traffic takes another path

  1. Ask for the exact destination with show ip route <destination> or ip route get <destination>.
  2. Look for a more-specific connected, VPN, or policy route.
  3. Check ip rule and all Linux tables; ordinary ip route may not be the selected table.
  4. Inspect ECMP next hops and hashing behavior.
  5. Verify recursive resolution, neighbor state, and interface status.
  6. Compare FRR’s RIB view with the kernel FIB and account for recent reconvergence.
  7. Check the return path; asymmetric routing can make a correct forward lookup look broken.

Use the Routing curriculum, then return to the subnetting guide for examples where an incorrect prefix creates an unexpectedly specific route.

延伸阅读

继续练习这套诊断思路

读懂路由表或计算前缀固然有用;但在真实网络中排查故障更胜一筹。Subnetica 将课程、评分检测和动手实操的虚拟实验结合在一起,使用 FRRouting 和 Linux,并通过 Cisco 风格兼容层支持受支持的命令和输出。这并非 Cisco IOS。

subnetica© 2026 · 学习,练习,掌握。
关于我们常见问题隐私政策条款与可接受使用政策无障碍声明contact@subneti.ca
CCNA 是 Cisco Systems, Inc. 的注册商标。CompTIA Network+ 和 CompTIA Security+ 是 CompTIA, Inc. 的注册商标。Subnetica 是一个独立的学习平台,与 Cisco Systems, Inc. 或 CompTIA, Inc. 没有任何关联,也未获得其认可或赞助。