How does OSPF choose the best route when there are multiple paths?
I have a topology with two routers that can reach the same destination via different paths. OSPF picks one but I don't understand why. Is it based on hop count like RIP? How does the cost metric work and how can I influence which path gets chosen?
- The cost formula is reference bandwidth / interface bandwidth. Default reference is 100Mbps, so a GigE link gets cost 1 and a FastEthernet link gets cost 1 too (both hit the floor). You can change it with auto-cost reference-bandwidth 10000 to make GigE and FastE have different costs. — Yaron,
- One thing that tripped me up — OSPF doesn't look at hop count at all. A 3-hop path through GigE links beats a 1-hop path through a FastEthernet link because the total cost is lower. You can also manually set the cost per interface with ip ospf cost. — itayitayy,
- Wait so OSPF is purely cost-based, not hop count at all? That's a big difference from RIP. I tested ip ospf cost on an interface in the lab and it immediately changed the path. This is way more flexible than I thought. — NivSa,
1 Answer
OSPF runs Dijkstra's SPF algorithm on the link-state database and picks the path with the lowest total cost to the destination. Cost is per-interface, and by default:
cost = reference_bandwidth / interface_bandwidth
Reference bandwidth defaults to 100 Mbps, so a 100 Mbps link = cost 1, a 10 Mbps link = cost 10. OSPF sums the outbound interface costs along the path and the lowest cumulative cost wins.
Key points:
- It's cost only — OSPF ignores hop count entirely. A 3-hop fast path beats a 1-hop slow path.
- If two paths tie on cost, OSPF installs both and load-balances (equal-cost multipath, 4 routes by default).
- Anything above 100 Mbps looks identical (cost 1) unless you raise the reference with
auto-cost reference-bandwidth.
Check it with show ip ospf neighbor (adjacency up first), then show ip route ospf for the chosen path and show ip ospf interface to see the cost on each link. In the lab, wire two routers with a couple of parallel paths, bump one interface's bandwidth down, and watch the route flip in the routing table.
- Worth adding: if you want to force a specific path without touching bandwidth, set `ip ospf cost <n>` directly on the interface. That's cleaner than fiddling with the `bandwidth` command, which also affects EIGRP if it's running. — Shira Sivroni,