Technical guide
How Routers Choose a Route: Longest Prefix Match, Administrative Distance, and Metrics
By Subnetica · Published August 24, 2026 · Updated August 24, 2026
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/24A 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 allStatic 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
- Ask for the exact destination with
show ip route <destination>orip route get <destination>. - Look for a more-specific connected, VPN, or policy route.
- Check
ip ruleand all Linux tables; ordinaryip routemay not be the selected table. - Inspect ECMP next hops and hashing behavior.
- Verify recursive resolution, neighbor state, and interface status.
- Compare FRR’s RIB view with the kernel FIB and account for recent reconvergence.
- 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.
