🕸️ OSPF

Routing 2: add a second path, switch on OSPF, then break things and time how long the network takes to heal.

Video: OSPF (Networking video)
Prefer to watch? This short video runs this page's main commands on three real FRR routers in Docker, with on-screen captions and a synthetic voiceover (an AI voice trained on the author's own recording). Click to play, or watch it on YouTube.
Routing 2: OSPF on three routers

What you will learn:
   how routers find routes by themselves with OSPF, and what a neighbor is
   why the first neighbors take about 40 seconds to come up
   how OSPF picks a path (cost), and why changing it on one end makes traffic go one way and come back another
   a cable pulled versus a link that silently stops: 0 seconds lost versus 34
   BFD, which cut that 34 seconds to 0.8
   the mistakes that stop neighbors forming, and how to see them
   the Cisco IOS command for each step, side by side

In Routing 1 we typed every route by hand, and the routes could not find another way round when a link broke. OSPF (Open Shortest Path First) fixes both. Each router tells its neighbors which links it has, every router builds the same map of the network from that, and each one works out the shortest path from the map by itself. When a link breaks, the news spreads and every router recalculates.
Every command and every line of output on this page was run for real on 2026-11-21 in the same lab as Routing 1, plus one new link. The routers run FRRouting (FRR) 10.7.1, whose commands were modelled on Cisco's. Where Cisco IOS is different, the Cisco command is shown next to it (Cisco lines are standard IOS syntax for reference; they were not run here).

The lab: now a triangle

Same five boxes as Routing 1, plus link 13: a direct cable between r1 and r3. Now there are two ways from LAN 1 to LAN 3.

           link 13  10.0.13.0/24
         +---------------+
         |               |
   h1 -- r1 ---- r2 ---- r3 -- h3
 10.1.0.10  link 12  link 23   10.3.0.10
            10.0.12.0/24  10.0.23.0/24

   r1: 10.1.0.1 (eth0, LAN 1)   10.0.12.1 (eth1, link 12)   10.0.13.1 (eth2, link 13)
   r2: 10.0.12.2 (eth0, link 12) 10.0.23.2 (eth1, link 23)
   r3: 10.0.23.3 (eth0, link 23) 10.3.0.1 (eth1, LAN 3)    10.0.13.3 (eth2, link 13)

We started from a clean lab: no static routes at all. FRR runs each protocol as its own program, and OSPF (ospfd) and BFD (bfdd) are switched on in /etc/frr/daemons:
$ ps -eo comm | grep -E "zebra|ospfd|bfdd"
zebra
bfdd
ospfd

As in Routing 1, the ping fails because nobody knows about the far LAN:
h1$ ping -c2 -W1 10.3.0.10
2 packets transmitted, 0 packets received, 100% packet loss

Step 1: switch on OSPF

On r1: start OSPF, give the router an ID, and list the networks it should run OSPF on.
r1# configure terminal
r1(config)# router ospf
r1(config-router)# ospf router-id 1.1.1.1
r1(config-router)# network 10.1.0.0/24 area 0
r1(config-router)# network 10.0.12.0/24 area 0
r1(config-router)# network 10.0.13.0/24 area 0
r1(config-router)# end

Cisco IOS takes a process number and a WILDCARD mask (the inverse of the subnet mask):
   router ospf 1
    router-id 1.1.1.1
    network 10.1.0.0 0.0.0.255 area 0
    network 10.0.12.0 0.0.0.255 area 0
    network 10.0.13.0 0.0.0.255 area 0

What these lines mean:
   router-id     a name for this router inside OSPF. It looks like an IP address but it is just a label; 1.1.1.1 is easy to read
   network ...   "every interface with an address in this range takes part in OSPF". It does not advertise the range itself
   area 0        the backbone area. A small network is one area; large ones split into areas around area 0

r1 alone has nobody to talk to yet:
r1# show ip ospf neighbor
Neighbor ID     Pri State           Up Time         Dead Time Address         Interface
(empty)

Now the same on r2 (link 12 and link 23) and r3 (link 23, link 13 and LAN 3):
r2(config)# router ospf
r2(config-router)# ospf router-id 2.2.2.2
r2(config-router)# network 10.0.12.0/24 area 0
r2(config-router)# network 10.0.23.0/24 area 0

r3(config)# router ospf
r3(config-router)# ospf router-id 3.3.3.3
r3(config-router)# network 10.0.23.0/24 area 0
r3(config-router)# network 10.0.13.0/24 area 0
r3(config-router)# network 10.3.0.0/24 area 0

Step 2: neighbors (and the 40-second wait)

Six seconds later, r1 can see both of the others, but they are stuck in "Init":
r1# show ip ospf neighbor
Neighbor ID     Pri State           Up Time         Dead Time Address         Interface
2.2.2.2           1 Init/DROther    6.221s            33.778s 10.0.12.2       eth1:10.0.12.1
3.3.3.3           1 Init/DROther    6.108s            33.891s 10.0.13.3       eth2:10.0.13.1

About 40 seconds later, all three routers were neighbors (47 s for the last one in our run):
r1# show ip ospf neighbor
Neighbor ID     Pri State           Up Time         Dead Time Address         Interface
2.2.2.2           1 Full/DR         6.695s            33.306s 10.0.12.2       eth1:10.0.12.1
3.3.3.3           1 Full/DR         6.584s            33.419s 10.0.13.3       eth2:10.0.13.1

Full means "we have swapped everything we know; our maps match". That is the state you want.

Why the wait? These are Ethernet links, which OSPF treats as a shared network where many routers could be connected. On a shared network it elects one router as the Designated Router (DR) to cut down the chatter, and before electing it waits a while (the Wait timer, 40 s) to hear who else is there:
r1# show ip ospf interface eth1
  Router ID 1.1.1.1, Network Type BROADCAST, Cost: 10
  Transmit Delay is 1 sec, State Backup, Priority 1
  Designated Router (ID) 2.2.2.2 Interface Address 10.0.12.2/24
  Backup Designated Router (ID) 1.1.1.1, Interface Address 10.0.12.1
  Timer intervals configured, Hello 10s, Dead 40s, Wait 40s, Retransmit 5
  Neighbor Count is 1, Adjacent neighbor count is 1

A link with exactly two routers on it does not need an election. Tell OSPF it is point-to-point and the wait goes away. We tried it on link 13 and the neighbor was Full after 10 seconds (one hello) instead of about 40, with no DR at all ("Full/-"):
r1(config)# interface eth2
r1(config-if)# ip ospf network point-to-point
r1# show ip ospf neighbor
3.3.3.3           1 Full/-          0.125s            39.987s 10.0.13.3       eth2:10.0.13.1
Set it on both ends. Cisco IOS: the same command under the interface. The rest of this page keeps the default (broadcast), as you would find it out of the box.

Step 3: routes appear by themselves

r1# show ip route ospf
O>* 10.0.23.0/24 [110/20] via 10.0.12.2, eth1, weight 1, 00:00:05
  *                       via 10.0.13.3, eth2, weight 1, 00:00:05
O>* 10.3.0.0/24 [110/20] via 10.0.13.3, eth2, weight 1, 00:00:05

No ip route typed anywhere, and r1 knows LAN 3 and link 23. How to read it:
   O          learned from OSPF
   [110/20]   administrative distance 110 (OSPF) / OSPF cost 20
   two "via" lines for 10.0.23.0/24: link 23 is exactly as far through r2 as through r3, so r1 uses BOTH (equal-cost multipath, ECMP)

h1$ ping -c3 -W1 10.3.0.10
64 bytes from 10.3.0.10: seq=0 ttl=62 time=0.186 ms
64 bytes from 10.3.0.10: seq=1 ttl=62 time=0.248 ms
64 bytes from 10.3.0.10: seq=2 ttl=62 time=0.209 ms
3 packets transmitted, 3 packets received, 0% packet loss

h1$ traceroute -n -w1 -q1 10.3.0.10
 1  10.1.0.1  0.008 ms
 2  10.0.13.3  0.014 ms
 3  10.3.0.10  0.004 ms

It takes the short way, straight over link 13, and ttl=62 says two routers this time, not three.
And the return path, the lesson from Routing 1? OSPF built it too: r3 learned LAN 1 the same way.

How OSPF picks a path: cost

Every OSPF interface has a cost (10 here; FRR and Cisco work it out from the interface speed by default). A route's cost is the sum of the costs of the interfaces the traffic LEAVES by:
   r1 to LAN 3 over link 13:   r1 eth2 (10) + r3 LAN side (10)              = 20
   r1 to LAN 3 through r2:     r1 eth1 (10) + r2 eth1 (10) + r3 LAN side (10) = 30
Lowest total wins: 20, straight over link 13.

The map behind it: every router keeps the same link-state database (LSDB). Each router describes its own links (a router LSA), and the DR of each shared link describes the link (a network LSA):
r1# show ip ospf database
                Router Link States (Area 0.0.0.0)
Link ID         ADV Router      Age  Seq#       CkSum  Link count
1.1.1.1        1.1.1.1           12 0x8000000a 0xcdcb 3
2.2.2.2        2.2.2.2           12 0x80000007 0x7d26 2
3.3.3.3        3.3.3.3            8 0x80000008 0x5717 3

                Net Link States (Area 0.0.0.0)
Link ID         ADV Router      Age  Seq#       CkSum
10.0.12.2      2.2.2.2           13 0x80000001 0xb07d
10.0.13.3      3.3.3.3            8 0x80000001 0xaf74
10.0.23.3      3.3.3.3           13 0x80000001 0x63b2

Three routers (r1 with 3 links, r2 with 2, r3 with 3) and three links. Run the same command on r2 or r3 and you get the same table: that is the whole idea of link-state routing.

Step 4: don't talk to the PCs (passive interfaces)

The network commands also put the LAN interfaces into OSPF, so r1 sends OSPF hellos to LAN 1 every 10 seconds. Nothing there answers, and you do not want a stranger's PC answering either:
r1# show ip ospf interface eth0
  Timer intervals configured, Hello 10s, Dead 40s, Wait 40s, Retransmit 5
    Hello due in 7.497s

Make the LAN interfaces passive: still advertised in OSPF, but no hellos and no neighbors on them.
r1(config)# interface eth0
r1(config-if)# ip ospf passive
r3(config)# interface eth1
r3(config-if)# ip ospf passive

r1# show ip ospf interface eth0
  Multicast group memberships: <None>
  Timer intervals configured, Hello 10s, Dead 40s, Wait 40s, Retransmit 5
    No Hellos (Passive interface)

Cisco IOS:  router ospf 1 / passive-interface GigabitEthernet0/0
FRR still accepts "passive-interface eth0" under router ospf, but prints "This command is deprecated, because it is not VRF-aware" and stores it as ip ospf passive on the interface. Make LAN-facing interfaces passive as a habit.

Step 5: pull the cable

Now the part static routes could not do. We sent pings from h1 to h3 every 0.2 seconds and took r1's link 13 interface down (the same as pulling the cable out of r1):
r1$ ip link set eth2 down
31 packets transmitted, 31 packets received, 0% packet loss

Not one ping lost. r1 moved to the long way round at once:
r1# show ip route 10.3.0.0/24
Routing entry for 10.3.0.0/24
  Known via "ospf", distance 110, metric 30, best
  * 10.0.12.2, via eth1, weight 1

h1$ traceroute -n -w1 -q1 10.3.0.10
 1  10.1.0.1  0.008 ms
 2  10.0.12.2  0.003 ms
 3  10.0.23.3  0.004 ms
 4  10.3.0.10  0.004 ms

What surprised us: r3 still listed r1 as a Full neighbor on link 13 for another 30 seconds, yet the replies came back fine. r1 sent its updated map (link 13 gone) to r3 through r2 straight away, and OSPF only uses a link when BOTH ends say it is there. So r3 stopped using link 13 the moment r1's update arrived, without waiting for the neighbor to time out.
r1$ ip link set eth2 up

Step 6: the failure that takes 34 seconds

A pulled cable is the easy case: the router notices. The nasty case is a link that stops passing traffic while both ends still look up: a faulty switch in between, a provider's line, a firewall rule. We faked it by dropping every packet on link 13 at both ends (tc netem: the interfaces stay up):
r1$ tc qdisc add dev eth2 root netem loss 100%
r3$ tc qdisc add dev eth2 root netem loss 100%

Watch the Dead Time column. OSPF only gives up on a neighbor after 40 seconds without a hello:
r1# show ip ospf neighbor
3.3.3.3           1 Full/DR         16.287s           23.711s 10.0.13.3       eth2:10.0.13.1
r1# show ip ospf neighbor
3.3.3.3           1 Full/DR         26.393s           13.605s 10.0.13.3       eth2:10.0.13.1
r1# show ip ospf neighbor
3.3.3.3           1 Full/DR         36.506s            3.492s 10.0.13.3       eth2:10.0.13.1
r1# show ip ospf neighbor
2.2.2.2           1 Full/DR         1m18s             31.267s 10.0.12.2       eth1:10.0.12.1

Until the countdown hit zero, r1 kept sending traffic into the dead link. The pings (every 0.2 s) told the story:
224 packets transmitted, 55 packets received, 75% packet loss
outage: 169 pings lost = 33.8 s
(Our first try, pinging every 0.5 s, lost 31 s.)

Thirty-four seconds of outage on a network with a perfectly good second path. Two fixes follow.

Fix 1: faster hellos

Send hellos every second and give up after 4, on both ends of the link:
r1(config)# interface eth2
r1(config-if)# ip ospf hello-interval 1
r1(config-if)# ip ospf dead-interval 4
(and the same on r3 eth2)

Same silent failure: the outage dropped from 31 s to 3.6 s.
Cisco IOS: the same two commands under the interface. The two ends MUST match, see "Mistake: timers that don't match" below.

Fix 2: BFD (0.8 seconds)

BFD (Bidirectional Forwarding Detection) is a tiny protocol that does one job: check, many times a second, that a link really passes traffic, and tell OSPF the moment it does not. In FRR it is the bfdd program; switch it on per interface, on both ends:
r1(config)# interface eth2
r1(config-if)# ip ospf bfd
r3(config)# interface eth2
r3(config-if)# ip ospf bfd

r1# show bfd peers brief
Session count: 1
SessionId  LocalAddress     PeerAddress      Status
1203416726 10.0.13.1        10.0.13.3        up

The defaults: a packet every 300 ms, and the link is declared dead after 3 missed (900 ms). The same silent failure, pinging every 0.1 s:
63 packets transmitted, 55 packets received, 12% packet loss
outage: 8 pings lost = 0.8 s

From 33.8 seconds to 0.8, with one line per interface. OSPF hellos stay at 10 s; BFD does the fast checking.
Cisco IOS (reference):
   interface GigabitEthernet0/2
    bfd interval 300 min_rx 300 multiplier 3
    ip ospf bfd
When the link came back and OSPF formed the neighbor again, the BFD session came back with it.

Changing the path: cost, and the one-way trap

Make link 13 expensive on r1, so r1 prefers going through r2:
r1(config)# interface eth2
r1(config-if)# ip ospf cost 100

r1# show ip route 10.3.0.0/24
  Known via "ospf", distance 110, metric 30, best
  * 10.0.12.2, via eth1, weight 1

h1$ traceroute -n -w1 -q1 10.3.0.10
 1  10.1.0.1  0.011 ms
 2  10.0.12.2  0.007 ms
 3  10.0.13.3  0.006 ms
 4  10.3.0.10  0.005 ms

Going there: r1, r2, r3. (Hop 3 shows r3's link 13 address because r3 sends its traceroute reply out of link 13, and a Linux router answers from the interface the reply leaves by.)
Now the way back:
h3$ traceroute -n -w1 -q1 10.1.0.10
 1  10.3.0.1  0.007 ms
 2  10.0.12.1  0.005 ms
 3  10.1.0.10  0.005 ms

Back, it goes straight from r3 to r1 over link 13. Cost counts on the interface traffic LEAVES by, and we only changed r1's side. The road there is not the road back, again (Routing 1 said hello). One-way paths work, but they confuse troubleshooting and firewalls. Set it on both ends:
r3(config)# interface eth2
r3(config-if)# ip ospf cost 100

h3$ traceroute -n -w1 -q1 10.1.0.10
 1  10.3.0.1  0.011 ms
 2  10.0.23.2  0.006 ms
 3  10.0.12.1  0.006 ms
 4  10.1.0.10  0.006 ms

Now both directions go through r2. Cisco IOS: ip ospf cost 100 under the interface.
FRR note: vtysh printed "% Configuration applied with notes: No changes found to be committed!" after ip ospf cost 100. The change WAS applied (the routes moved). It is a message from FRR's new management daemon; ignore it.

Static beats OSPF (and the floating static)

Routing 1 said static routes have distance 1, "trust me over any routing protocol". Here is the proof. With OSPF going through r2, add a static route over link 13:
r1(config)# ip route 10.3.0.0/24 10.0.13.3
r1# show ip route 10.3.0.0/24
Routing entry for 10.3.0.0/24
  Known via "static", distance 1, metric 0, best
  * 10.0.13.3, via eth2, weight 1
Routing entry for 10.3.0.0/24
  Known via "ospf", distance 110, metric 30
    10.0.12.2, via eth1, weight 1

The static route wins (distance 1 against 110), and the OSPF route waits in reserve. A forgotten static route is a classic reason why "OSPF ignores my cost change".

Give the static route a distance higher than OSPF's and it becomes a backup instead, used only if OSPF loses the route (a floating static route):
r1(config)# no ip route 10.3.0.0/24 10.0.13.3
r1(config)# ip route 10.3.0.0/24 10.0.13.3 200
r1# show ip route 10.3.0.0/24
Routing entry for 10.3.0.0/24
  Known via "static", distance 200, metric 0
    10.0.13.3, via eth2, weight 1
Routing entry for 10.3.0.0/24
  Known via "ospf", distance 110, metric 30, best
  * 10.0.12.2, via eth1, weight 1

Cisco IOS:  ip route 10.3.0.0 255.255.255.0 10.0.13.3 200

Mistake: timers that don't match

Change the hello interval on ONE end of link 12:
r2(config)# interface eth0
r2(config-if)# ip ospf hello-interval 5
r2# show ip ospf interface eth0
  Timer intervals configured, Hello 5s, Dead 20s, Wait 20s, Retransmit 5
(FRR set the dead interval to 4 x 5 = 20 by itself.)

r1 kept r2 as a neighbor until its own 40-second dead timer ran out, then r2 disappeared and never came back:
r1# show ip ospf neighbor
3.3.3.3           1 Full/DR         1m00s             39.524s 10.0.13.3       eth2:10.0.13.1
r1# show ip ospf interface eth1
  Timer intervals configured, Hello 10s, Dead 40s, Wait 40s, Retransmit 5
  Neighbor Count is 0, Adjacent neighbor count is 0

OSPF neighbors must agree on hello and dead intervals (also area, subnet mask and MTU). Nothing in show ip ospf neighbor says why. Compare show ip ospf interface on both ends, or turn on debugging:
r1(config)# log file /tmp/frr.log
r1# debug ospf packet hello recv detail
r1$ grep mismatch /tmp/frr.log
OSPF: [XQA13-4ED8B][EC 134217741] Packet 2.2.2.2 [Hello:RECV]: RouterDeadInterval mismatch on eth1:10.0.12.1 (expected 40, but received 20).
r1# no debug ospf packet hello recv detail

That one line names the neighbor, the interface and both values. Cisco IOS: debug ip ospf hello reports "Mismatched hello parameters" with both sides' values (then undebug all).
Putting r2 back to 10/40 brought the neighbor straight back to Full.

Save it

r1# show running-config
interface eth0
 ip ospf passive
exit
!
interface eth2
 ip ospf bfd
 ip ospf cost 100
exit
!
router ospf
 ospf router-id 1.1.1.1
 network 10.0.12.0/24 area 0
 network 10.0.13.0/24 area 0
 network 10.1.0.0/24 area 0
exit

r1# write memory
Configuration saved to /etc/frr/zebra.conf
Configuration saved to /etc/frr/ospfd.conf
Configuration saved to /etc/frr/bfdd.conf
...

Each FRR daemon gets its own file. Cisco IOS: copy running-config startup-config.

Build the same lab yourself

Start with the Routing 1 lab (the "Build the same lab yourself" section of Static Routes), then:

1. Switch on ospfd and bfdd. Copy /etc/frr/daemons out of a router, set ospfd=yes and bfdd=yes, and mount it into each router container when you create it:
   docker run ... -v $PWD/daemons:/etc/frr/daemons quay.io/frrouting/frr:10.7.1
   (or edit the file inside each router and restart it; in Docker a restart can shuffle the eth names, so check ip -br addr afterwards)
2. Add link 13:
   docker network create --internal --subnet 10.0.13.0/24 --gateway 10.0.13.254 rt-link13
   docker network connect --ip 10.0.13.1 rt-link13 rt-r1
   docker network connect --ip 10.0.13.3 rt-link13 rt-r3
3. Do NOT add the Routing 1 static routes. Start with empty routers and follow the steps above.

To fake a silent failure: tc qdisc add dev eth2 root netem loss 100% inside both routers, and tc qdisc del dev eth2 root to heal it. To pull the cable: ip link set eth2 down / up.
Docker gotcha we hit: after OSPF had learned a route to 10.0.13.0/24, docker network connect refused to re-attach r1 to link 13 ("conflicts with existing route"). Use ip link set down/up for cable tests, not docker network disconnect.

Cheat sheet: FRR and Cisco IOS

Task                        FRR (vtysh)                            Cisco IOS
start OSPF                  router ospf                            router ospf 1
router ID                   ospf router-id 1.1.1.1                 router-id 1.1.1.1
interfaces in area 0        network 10.0.12.0/24 area 0            network 10.0.12.0 0.0.0.255 area 0
neighbors                   show ip ospf neighbor                  show ip ospf neighbor
interface details/timers    show ip ospf interface eth1            show ip ospf interface g0/1
OSPF routes                 show ip route ospf                     show ip route ospf
the map (LSDB)              show ip ospf database                  show ip ospf database
no hellos on a LAN          ip ospf passive (interface)            passive-interface g0/0 (router ospf)
skip the DR election        ip ospf network point-to-point         ip ospf network point-to-point
path cost                   ip ospf cost 100                       ip ospf cost 100
faster hellos               ip ospf hello-interval 1 / dead-interval 4   same
BFD                         ip ospf bfd                            bfd interval 300 min_rx 300 multiplier 3 + ip ospf bfd
BFD sessions                show bfd peers                         show bfd neighbors
floating static             ip route 10.3.0.0/24 10.0.13.3 200     ip route 10.3.0.0 255.255.255.0 10.0.13.3 200
why no neighbor             debug ospf packet hello recv detail    debug ip ospf hello
save                        write memory                           copy running-config startup-config
(Cisco column: standard IOS syntax, for reference; not run in this lab.)

Common problems

Neighbors stuck in Init for about 40 seconds on Ethernet: normal, that is the DR wait. Use ip ospf network point-to-point on links between two routers.
A neighbor never comes up: hello/dead timers, area, subnet mask or MTU differ between the two ends. Compare show ip ospf interface on both, or debug the hellos.
A route is missing: the interface is not matched by any network statement (check show ip ospf interface), or it is passive on the link that should form a neighbor.
OSPF ignores your cost change: a static route (distance 1) is winning. Look for "Known via static" in show ip route.
Traffic goes one way and comes back another: cost was changed on one end only.
Failover takes 30-40 seconds: the link failed silently and OSPF waited for the dead timer. Use BFD.
Everything vanished after a restart: write memory.

What's next

Routing 3: BGP, the protocol that connects networks run by different people, the internet included. Same lab, new rules: nothing is shared unless you say so.
Missed the start? Routing 1: Static Routes builds this lab and explains the routing table, the return path and default routes.