CCNA online course

Configure OSPF for IPv4 Networks

Learn to configure, verify, and troubleshoot single-area and basic multi-area OSPFv2 on Cisco IOS routers, including neighbors, areas, costs, DR elections, and default routes.

Open Shortest Path First (OSPF) is a link-state interior gateway protocol (IGP) used to exchange IPv4 routes within an autonomous system. This lesson focuses on OSPFv2 for Cisco IOS and CCNA-level networks. OSPFv3 is the version commonly used for IPv6.

Before starting, be comfortable with the OSI reference model, IPv4 addressing, subnet masks, Cisco IOS interface configuration, static routes, and basic Ethernet troubleshooting.

How OSPF Works

In a link-state protocol, each router describes its connected links and their properties to other OSPF routers. These descriptions are distributed as link-state advertisements (LSAs). Routers in the same area use the LSAs to build a shared link-state database (LSDB), which is a map of the area's topology.

Each router runs the shortest path first (SPF) algorithm against the LSDB. SPF calculates the lowest-cost path to each destination and installs the best routes in the routing table. OSPF's metric is called cost.

  • OSPF is a link-state IGP used inside an autonomous system.
  • OSPFv2 routes IPv4 networks; OSPFv3 routes IPv6 networks.
  • Routers exchange topology information, not just complete distance measurements.
  • OSPF uses cumulative path cost to select routes.
  • Equal-cost multipath (ECMP) allows multiple paths with the same total cost to be installed and used.

Distance-vector protocols generally learn routes from neighbors by receiving a destination and a distance, then selecting routes using a distance metric and direction. OSPF instead maintains a topology view and independently calculates paths. This distinction is conceptual; both protocol families can use metrics and neighbor exchanges.

OSPF Router IDs

A router ID is a 32-bit value that uniquely identifies an OSPF router. It is written like an IPv4 address, but it does not need to be assigned to a physical interface. Cisco IOS selects it in this order:

PrioritySelection SourceNotes
1Manually configured router IDConfigured under the OSPF process; this takes precedence.
2Highest loopback IPv4 addressSelected from configured loopback interfaces.
3Highest active physical IPv4 addressUsed when no manual ID or loopback address is available.

Loopback interfaces are commonly used because they are logical interfaces and normally remain available even if a physical link fails. Use unique, documented loopback addresses across the OSPF domain.

router ospf 1
 router-id 1.1.1.1

The router ID is normally selected when the OSPF process starts. If you change it after the process is running, reset the OSPF process or reload the router during an approved maintenance window:

clear ip ospf process

Confirm the impact before accepting the reset because existing OSPF adjacencies will be torn down temporarily.

OSPF Areas and Router Roles

An area is a logical OSPF topology boundary. Areas reduce the size of each LSDB, limit SPF calculations, and help large routing domains scale. Area 0, also called the backbone area, is the central area through which inter-area routing is designed to pass.

  • Internal router: All OSPF interfaces belong to the same area.
  • Backbone router: At least one OSPF interface belongs to Area 0.
  • Area Border Router (ABR): Has interfaces in two or more areas and exchanges information between them.
  • Autonomous System Boundary Router (ASBR): Injects routes learned from another routing domain or source, such as static routes or another routing protocol.

A single-area deployment places all OSPF interfaces in Area 0. In a basic multi-area design, every non-backbone area should connect to Area 0 directly through an ABR or through a valid backbone design. An isolated non-backbone area does not provide normal inter-area connectivity.

Single-Area OSPFv2 Lab

Consider three routers in a line. Each router has a loopback network and two-router transit links. All interfaces are in Area 0.

  • R1 router ID: 1.1.1.1; loopback network: 192.168.1.0/24
  • R2 router ID: 2.2.2.2; loopback network: 192.168.2.0/24
  • R3 router ID: 3.3.3.3; loopback network: 192.168.3.0/24
  • R1-R2 transit network: 10.0.12.0/30
  • R2-R3 transit network: 10.0.23.0/30

Configure loopbacks and interfaces

interface Loopback0
 ip address 192.168.1.1 255.255.255.0
!
interface GigabitEthernet0/0
 ip address 10.0.12.1 255.255.255.252
 no shutdown

Use the equivalent addressing for R2 and R3. A loopback interface represents a stable logical network. Transit interfaces connect OSPF routers and must have compatible IPv4 addressing.

Enable OSPF with network statements

router ospf 1
 router-id 1.1.1.1
 network 10.0.12.0 0.0.0.3 area 0
 network 192.168.1.0 0.0.0.255 area 0

On R2, advertise both transit links and its loopback. On R3, advertise the R2-R3 transit link and the R3 loopback. The process number 1 is locally significant. It does not need to match the process number on another router; the area, addressing, and OSPF parameters must match where neighbors form.

Wildcard Masks and OSPF Enablement

A wildcard mask is an inverse mask used by Cisco IOS network statements. A binary 0 means the corresponding bit must match. A binary 1 means the bit can vary.

PurposeExampleMeaning
Match one exact IPv4 address192.168.1.1 0.0.0.0Only that interface address matches.
Match a /24 subnet192.168.1.0 0.0.0.255The first 24 bits must match.
Match a /30 subnet10.0.12.0 0.0.0.3All four addresses in the /30 range can match.

To calculate a wildcard mask, subtract each subnet-mask octet from 255. For 255.255.255.252, the result is 0.0.0.3. Avoid overly broad statements such as network 0.0.0.0 255.255.255.255 area 0 unless you deliberately want every IPv4 interface to participate.

Enable OSPF directly on an interface

On IOS versions that support interface-level OSPF configuration, enable OSPF without a network statement:

interface GigabitEthernet0/0
 ip ospf 1 area 0

This method can make interface intent easier to review. Use either a carefully designed network-statement approach or interface-level commands consistently within a lab or operational standard.

Passive Interfaces

A passive interface advertises its connected prefix into OSPF but does not send OSPF hello packets or form OSPF neighbors. This is appropriate for user LANs and loopbacks, where no OSPF router should be connected.

router ospf 1
 passive-interface default
 no passive-interface GigabitEthernet0/0

This configuration makes all interfaces passive, then enables hello packets on the router-to-router link. Repeat no passive-interface for every legitimate OSPF transit interface. Confirm the result with show ip ospf interface.

Neighbor Discovery and Adjacency

OSPF routers send hello packets to discover and maintain neighbors. After discovery, they negotiate and synchronize link-state information. A working adjacency requires compatible settings, including:

ParameterMust Match or Be CompatibleTypical Symptom When Incorrect
IPv4 subnetInterfaces normally must share the same subnet.No neighbor or one-way communication.
Area IDBoth interfaces must use the same area on the link.Hellows are rejected or no adjacency forms.
Hello and dead intervalsTimers must match on the link.Neighbor fails to form or repeatedly resets.
AuthenticationAuthentication mode and credentials must be compatible.Hello packets are rejected.
Network typeNetwork behavior and neighbor expectations must be compatible.Negotiation or adjacency problems.
OSPF optionsRelevant capability and area options must be compatible.Neighbor may be rejected or remain incomplete.
MTUInterface MTUs should match for database exchange.ExStart or Exchange state.

Neighbor states

StateMeaningTroubleshooting Significance
DownNo recent hello has been received.Check interface state, addressing, OSPF enablement, and Layer 2.
InitA hello was received, but the local router ID was not seen in the neighbor's hello.Often indicates one-way communication or multicast problems.
2-WayBidirectional hello communication is established.Normal final state between DROTHER routers on a broadcast segment.
ExStartRouters negotiate master/slave roles for database exchange.Check MTU and unstable links if stuck here.
ExchangeDatabase description packets are exchanged.Investigate MTU or database negotiation issues if it persists.
LoadingMissing LSAs are requested and received.Check packet loss or database synchronization problems.
FullRequired link-state information is synchronized.Expected on point-to-point links and with DR/BDR adjacencies.

On a point-to-point link, the expected neighbor state is Full. On a broadcast Ethernet segment, two non-DR/non-BDR routers, called DROTHERs, commonly remain in 2-Way with each other while forming Full adjacencies with the DR and BDR.

DR and BDR Elections

On a broadcast multiaccess network, forming a full adjacency between every pair of routers would create unnecessary overhead. The Designated Router (DR) represents the segment, while the Backup Designated Router (BDR) is ready to take over. Other routers are DROTHERs.

Network TypeTypical UseDR/BDR ElectionCommon Neighbor State
BroadcastEthernet multiaccess segmentYesDROTHER-to-DROTHER commonly 2-Way
Point-to-pointDirect router-to-router linkNoFull

Election order is influenced first by the highest OSPF interface priority, then by the highest router ID when priorities tie. Priority 0 prevents an interface from becoming DR or BDR.

interface GigabitEthernet0/1
 ip ospf priority 100

DR and BDR elections are generally non-preemptive. Changing a priority does not normally displace an existing DR. To observe a new election, reset the affected OSPF process or cycle the relevant interfaces during an approved maintenance window.

OSPF Cost and Route Selection

OSPF adds the costs of every outgoing interface along a path. The lowest cumulative cost wins. Cisco IOS calculates a default interface cost conceptually as:

OSPF cost = reference bandwidth / interface bandwidth

The reference bandwidth must be large enough to distinguish modern high-speed links. Configure the same reference bandwidth throughout the OSPF domain:

router ospf 1
 auto-cost reference-bandwidth 10000

The value is expressed in megabits per second. If routers use inconsistent reference bandwidth values, they can calculate different path costs and select unexpected routes.

For a deterministic design, configure an explicit interface cost:

interface GigabitEthernet0/0
 ip ospf cost 10

Use manual cost when the operational preference is not represented by physical bandwidth, such as choosing a primary path over a backup path. Check for manual costs before assuming a route uses automatic calculation.

Advertising Connected Networks and Loopbacks

An interface matched by a network statement, or configured with ip ospf, participates in OSPF unless it is passive. Its connected network can then be represented in the LSDB and advertised to other routers.

Loopbacks are also advertised when enabled in OSPF. On Cisco IOS, a loopback is commonly advertised as a host route by default because the interface is treated as a logical point-to-point interface. If you need the configured loopback subnet to appear as a network, configure the loopback network type as appropriate for the platform and design, or account for the host-route behavior in your addressing plan.

Default Route Propagation

An edge router can advertise a default route so internal routers know where to send traffic for destinations not present in their routing tables. First create or learn a default route, then use default-information originate:

ip route 0.0.0.0 0.0.0.0 203.0.113.1
!
router ospf 1
 default-information originate

Without the always keyword, the router normally advertises the OSPF default only when a default route already exists in its routing table. Use default-information originate always to originate it even when no local default route exists. Conditional origination can be designed with route policies or tracking features, but unconditional origination must be used carefully because it can direct traffic toward an unavailable exit.

OSPF Verification

CommandWhat It VerifiesKey Fields to Review
show ip ospfOSPF process and area informationProcess ID, router ID, areas, reference bandwidth
show ip ospf interface briefParticipating interfacesArea, process, cost, state, and address
show ip ospf interface GigabitEthernet0/0Detailed interface operationArea, network type, timers, cost, priority, DR/BDR, passive status
show ip ospf neighborNeighbor relationshipsNeighbor ID, state, address, interface, uptime
show ip ospf databaseLink-state databaseArea database entries and router/network LSAs
show ip route ospfOSPF-installed routesRoute code, metric, next hop, outgoing interface
show ip protocolsRouting process behaviorNetworks, passive interfaces, router ID, sources
show running-config | section router ospfConfigured OSPF processRouter ID, network statements, passive settings, default route settings

Common Cisco routing-table codes include O for an intra-area OSPF route and O IA for an inter-area OSPF route. Intra-area routes are learned within the local area. Inter-area routes are learned through an ABR from another area.

Troubleshooting Workflow

  1. Validate Layer 1 and Layer 2: inspect cables, switch ports, encapsulation, VLAN membership, and errors.
  2. Check interface state and addressing with show ip interface brief. Both ends of a transit link need compatible IPv4 addresses and masks.
  3. Confirm OSPF is enabled on the intended interfaces with show ip ospf interface brief and show ip protocols.
  4. Compare area IDs, hello and dead timers, authentication, network type, options, and MTU.
  5. Check whether either side is passive.
  6. Inspect show ip ospf neighbor and interpret the current state.
  7. If the neighbor is Full but a route is missing, verify the originating interface, wildcard match, area membership, LSDB, route filtering, and destination prefix.
  8. For an unexpected path, compare interface costs, reference bandwidth, manual cost commands, and possible ECMP paths.
  9. For an unexpected DR or BDR, compare interface priorities and router IDs. Remember that elections are non-preemptive.
SymptomLikely CauseValidation CommandTypical Fix
No neighbor on a direct linkDown interface, wrong subnet, missing OSPF, different area, timer mismatch, passive interface, authentication, or network type mismatchshow ip interface brief; show ip ospf interface; show ip protocolsCorrect the local or remote mismatch and recheck.
Neighbor remains InitOne-way hello communication or multicast delivery problemshow ip ospf neighbor; debug ip ospf helloVerify bidirectional Layer 2 and multicast operation.
Neighbor stuck in ExStart or ExchangeMTU mismatch or unstable database exchangeshow interfaces; show ip ospf interfaceCompare MTUs and OSPF settings; fix the underlying issue.
Expected network is not advertisedWildcard does not match, interface-level OSPF is missing, interface is down, or area is wrongshow ip ospf interface brief; show ip protocols; show ip ospf databaseCorrect interface participation, addressing, area, and state.
Unexpected path selectedInconsistent reference bandwidth, manual cost, or equal-cost pathsshow ip ospf interface; show running-config | include reference-bandwidth|ip ospf costStandardize reference bandwidth and set explicit costs where needed.
Wrong DR or BDRPriority or router ID wins, or a prior non-preemptive election remainsshow ip ospf interface; show ip ospf neighborAdjust priority or ID and deliberately reset the election environment if approved.

Using debug safely

Debug output is processed by the router and can consume CPU or produce a large amount of console output. Use it briefly, preferably during a maintenance window or in a lab. Filter access to the device and stop debugging immediately after collecting evidence:

debug ip ospf hello
debug ip ospf adj
undebug all

Prefer non-disruptive show commands first. Debugging is most useful when the state and configuration outputs do not explain why hellos or adjacency negotiation are failing.

Configuration Safety Checklist

  • Use unique, stable loopback addresses for documented router IDs.
  • Choose a clear OSPF process number and document that it is locally significant.
  • Use passive interfaces by default when the design permits, then enable only router-to-router links.
  • Keep the reference bandwidth consistent on every OSPF router.
  • Use explicit interface costs when path selection must be deterministic.
  • Review the running configuration and verify reachability before saving.
show running-config
copy running-config startup-config

Practical Review Scenarios

Wildcard matching

To match only interface address 10.0.12.1, use network 10.0.12.1 0.0.0.0 area 0. To match the entire 10.0.12.0/30 transit subnet, use network 10.0.12.0 0.0.0.3 area 0. The second form may enable OSPF on any local interface whose address falls in that range.

Passive LAN design

A router has one upstream link and two user-facing LANs. Configure passive-interface default, then use no passive-interface only on the upstream link. The two LAN prefixes remain advertised, but the router does not attempt to discover OSPF neighbors on user ports.

Multiaccess election

Three routers share an Ethernet switch segment. The router with the highest eligible priority becomes DR, and the next highest becomes BDR. A priority of 0 makes a router ineligible. If the election has already occurred, changing priority alone may not change the result because OSPF elections generally do not preempt.

Cost tuning

When two paths exist, inspect each interface's calculated cost and add the costs along the complete path. Standardize the reference bandwidth first. Then apply ip ospf cost to a selected interface if the intended preference does not follow bandwidth. Verify the result with show ip route ospf and, where appropriate, traceroute.

Default route propagation

On an edge router, configure a static default route toward the external network and use default-information originate. Internal routers should then show an OSPF-learned default route. If the edge router has no default route, use always only when that behavior is intentional and safe.