ip ospf cost [value]
Manually sets the OSPF cost (metric) on an interface, overriding the default cost derived from bandwidth, to influence path selection in OSPF routing.
Definition: ip ospf cost [value] is a Cisco IOS interface config command. Manually sets the OSPF cost (metric) on an interface, overriding the default cost derived from bandwidth, to influence path selection in OSPF routing.
Overview
The `ip ospf cost [value]` interface configuration command manually sets the OSPF cost (metric) on a specific interface, overriding the default cost that OSPF derives from the interface bandwidth (cost = reference-bandwidth / bandwidth). This command is critical for influencing OSPF path selection in multi-area or multi-path networks where you need to steer traffic away from high-latency or low-bandwidth links without altering the physical bandwidth or reference bandwidth. It is often used in traffic engineering, load balancing, or to enforce administrative policies.
The command is applied in interface configuration mode and immediately affects the OSPF link-state database (LSDB) and subsequent SPF calculations. It does not require a router ospf process restart; changes take effect after the next OSPF hello or LSA update. The cost value is an integer from 1 to 65535, with lower values indicating more preferred paths.
Common mistakes include setting the cost too low (causing suboptimal routing) or forgetting that the cost is per-interface and must be consistent across both ends of a link for symmetric routing. This command is preferred over adjusting the reference bandwidth when only specific interfaces need cost modification, as it avoids global impact. In troubleshooting, verifying the cost with `show ip ospf interface` helps identify misconfigurations that cause unexpected routing behavior.
The command is available in all IOS privilege levels that allow interface configuration (typically privilege level 15). It is saved in the running configuration and can be written to startup-config.
ip ospf cost [value]When to Use This Command
- Forcing traffic to prefer a higher-bandwidth link by setting a lower cost on that interface.
- Making a backup link less preferred by assigning a higher cost to it.
- Equalizing costs across unequal-bandwidth links for load balancing.
- Adjusting OSPF metric to steer traffic away from a congested link.
Parameters
| Parameter | Syntax | Description |
|---|---|---|
| value | <1-65535> | The OSPF cost metric for the interface. Valid values range from 1 to 65535. Lower values make the interface more preferred for OSPF path selection. Common mistake: setting a value that is too low (e.g., 1) may attract all traffic to that link, potentially causing congestion. Ensure the cost is consistent with the network design and other interface costs. |
Command Examples
Setting OSPF cost on a GigabitEthernet interface
Router(config-if)# ip ospf cost 10This command sets the OSPF cost to 10 on the current interface. No output is generated; the change is applied immediately.
Verifying the configured OSPF cost
Router# show ip ospf interface gigabitEthernet 0/0GigabitEthernet0/0 is up, line protocol is up
Internet Address 192.168.1.1/24, Area 0
Process ID 1, Router ID 1.1.1.1, Network Type BROADCAST, Cost: 10
Transmit Delay is 1 sec, State DR, Priority 1
Designated Router (ID) 1.1.1.1, Interface address 192.168.1.1
Backup Designated router (ID) 2.2.2.2, Interface address 192.168.1.2
Timer intervals configured, Hello 10, Dead 40, Wait 40, Retransmit 5
oob-resync timeout 40
Hello due in 00:00:03
Supports Link-local Signaling (LLS)
Cisco NSF helper support enabled
IETF NSF helper support enabled
Index 1/1, flood queue length 0
Next 0x0(0)/0x0(0)
Last flood scan length is 1, maximum is 25
Last flood scan time is 0 msec, maximum is 4 msec
Neighbor Count is 1, Adjacent neighbor count is 1
Adjacent with neighbor 2.2.2.2 (Backup Designated Router)
Suppress hello for 0 neighbor(s)The output shows OSPF interface details. The 'Cost: 10' field confirms the configured cost. Other fields: 'Internet Address' is the interface IP, 'Area' is the OSPF area, 'Process ID' and 'Router ID' identify the OSPF process, 'Network Type' is broadcast, 'State' is DR (Designated Router), and 'Neighbor Count' shows adjacent neighbors.
Understanding the Output
The 'show ip ospf interface' command displays OSPF parameters for an interface. The 'Cost' field shows the OSPF metric; lower values indicate more preferred paths. Default cost is 10^8 / bandwidth (bps).
A manually set cost overrides this. Other key fields: 'State' indicates the router's role (DR, BDR, DROTHER); 'Neighbor Count' shows discovered neighbors; 'Adjacent neighbor count' shows fully adjacent neighbors. High cost values (e.g., >100) may indicate slower links or misconfiguration.
Watch for mismatched costs on links that should be equal.
Configuration Scenarios
Configure OSPF cost on a high-bandwidth link to prefer it over a backup link
In a dual-homed branch office, you have a primary 1 Gbps link and a backup 100 Mbps link. By default, OSPF cost is 1 on the 1 Gbps link (reference 100 Mbps) and 1 on the 100 Mbps link (since reference/bandwidth = 100/100 = 1). To ensure the primary link is always preferred, you can manually set a lower cost on the primary link.
Topology
R1(Gi0/0)---10.0.12.0/30---(Gi0/0)R2 (primary 1Gbps)
R1(Gi0/1)---10.0.13.0/30---(Gi0/1)R2 (backup 100Mbps)Steps
- 1.Step 1: Enter global configuration mode: configure terminal
- 2.Step 2: Enter interface configuration for the primary link: interface GigabitEthernet0/0
- 3.Step 3: Set OSPF cost to 10 (lower than default 1? Actually default is 1, so set to 1 to keep it low, but for demonstration set to 5): ip ospf cost 5
- 4.Step 4: Exit and configure the backup interface: interface GigabitEthernet0/1
- 5.Step 5: Set OSPF cost to 100 (higher than primary): ip ospf cost 100
- 6.Step 6: Exit and verify: end
! Full IOS config block Router(config)# interface GigabitEthernet0/0 Router(config-if)# ip ospf cost 5 Router(config-if)# interface GigabitEthernet0/1 Router(config-if)# ip ospf cost 100 Router(config-if)# end
Verify: Use 'show ip ospf interface GigabitEthernet0/0' and 'show ip ospf interface GigabitEthernet0/1' to see the cost. Expected output shows 'Cost: 5' for Gi0/0 and 'Cost: 100' for Gi0/1. Also use 'show ip route ospf' to verify that the route via Gi0/0 is preferred.
Watch out: If OSPF network type is broadcast or non-broadcast, the cost must be consistent on both ends of the link to avoid asymmetric routing. Also, changing cost does not affect existing adjacencies until the next hello or LSA update.
Override OSPF cost on a slow link to discourage transit traffic
You have a serial link (T1, 1.544 Mbps) connecting two remote sites. The default OSPF cost is 64 (reference 100 Mbps / 1.544 Mbps ≈ 64). You want to discourage OSPF from using this link for transit traffic, preferring a parallel higher-bandwidth link. You can increase the cost on the serial link.
Topology
R3(Se0/0/0)---10.0.34.0/30---(Se0/0/0)R4 (T1 link)
R3(Gi0/0)---10.0.35.0/30---(Gi0/0)R4 (100 Mbps link)Steps
- 1.Step 1: Enter global configuration mode: configure terminal
- 2.Step 2: Enter interface configuration for the serial link: interface Serial0/0/0
- 3.Step 3: Set OSPF cost to a high value, e.g., 1000: ip ospf cost 1000
- 4.Step 4: Exit and verify: end
! Full IOS config block Router(config)# interface Serial0/0/0 Router(config-if)# ip ospf cost 1000 Router(config-if)# end
Verify: Use 'show ip ospf interface Serial0/0/0' to confirm cost is 1000. Use 'show ip route ospf' to see that routes via the serial link have higher metric and are not preferred if equal-cost paths exist.
Watch out: If the serial link is the only path to a network, increasing the cost may cause suboptimal routing but not loss of connectivity. Ensure that the cost is not set so high that it exceeds the maximum path cost (OSPF metric is 16-bit, max 65535). Also, remember that cost is a per-interface setting; the neighbor's cost on the same link does not need to match, but mismatched costs can cause asymmetric routing.
Troubleshooting with This Command
When troubleshooting OSPF path selection issues, the `ip ospf cost` command is often a suspect. Healthy output from `show ip ospf interface` shows the cost as expected based on configuration or default calculation. Problem indicators include unexpected cost values (e.g., default cost when manual cost was intended, or cost that does not reflect bandwidth changes).
Focus on the 'Cost' field in the output. Common symptoms: traffic taking a suboptimal path (e.g., using a slow link instead of a fast one) often due to misconfigured cost on the fast link (cost too high) or on the slow link (cost too low). Another symptom is OSPF not preferring a redundant link because the cost is equal or higher than the primary.
To diagnose, follow this flow: 1) Identify the interface in question with `show ip ospf interface brief` to see all OSPF-enabled interfaces and their costs. 2) Check the cost on each interface with `show ip ospf interface [interface]`. 3) Compare with expected values based on bandwidth or manual configuration. 4) If cost is unexpected, verify the interface configuration with `show running-config interface [interface]`. 5) Check if the cost was overridden by `ip ospf cost` or if the reference bandwidth was changed globally with `auto-cost reference-bandwidth`. 6) Use `show ip ospf database` to see the link-state metrics advertised by neighbors; if the cost differs from what you configured, the neighbor may be using a different cost. 7) For path selection, use `show ip route ospf` to see the metric of routes. Correlate with `traceroute` to confirm the actual path. Debug commands like `debug ip ospf spf` can show SPF calculations and why a particular path was chosen.
Remember that OSPF cost is additive along the path; a single high-cost interface can make the entire path less preferred. Also, note that the cost is only one factor; OSPF also considers area type (stub, NSSA) and route type (intra-area, inter-area, external).
CCNA Exam Tips
CCNA exam tip: OSPF cost is inversely proportional to bandwidth; default cost = 10^8 / bandwidth. Manually setting cost overrides this.
CCNA exam tip: The 'ip ospf cost' command is configured in interface configuration mode, not OSPF router mode.
CCNA exam tip: OSPF uses cost as the metric; the lowest cost path to a destination is preferred.
CCNA exam tip: To verify the cost, use 'show ip ospf interface' and look for the 'Cost' field.
Common Mistakes
Mistake: Setting cost on the wrong interface, causing unintended path selection.
Mistake: Forgetting that cost is a 16-bit value (1-65535); values outside this range are rejected.
Mistake: Assuming cost is the only factor; OSPF also considers path type (intra-area, inter-area, external).
ip ospf cost [value] vs show ip ospf interface
Both commands are OSPF interface-related but serve different purposes: one configures the OSPF cost to influence path selection, while the other displays OSPF interface parameters for verification. They are often used together when tuning OSPF metrics and checking the result.
| Aspect | ip ospf cost [value] | show ip ospf interface |
|---|---|---|
| Scope | Individual interface | All OSPF interfaces or specified one |
| Configuration mode | Interface Configuration mode | Privileged EXEC mode |
| Function | Sets OSPF metric (cost) | Displays OSPF parameters and status |
| Persistence | Saved in running-config | Not persistent (show output) |
| Impact | Changes routing behavior | No impact, only observation |
| Typical use | Tuning path selection | Verifying cost, state, neighbors |
Use ip ospf cost [value] when you need to manually override the default OSPF metric on an interface to influence path selection.
Use show ip ospf interface when you need to verify OSPF interface parameters, such as cost, state, and neighbor details.
Platform Notes
In IOS-XE (e.g., Catalyst 9000 switches), the `ip ospf cost` command syntax is identical to classic IOS. However, on some IOS-XE platforms, the interface cost may also be influenced by the `bandwidth` command if no manual cost is set. On NX-OS (e.g., Nexus switches), the equivalent command is `ip ospf cost <value>` under interface configuration mode, but note that NX-OS uses a different OSPF implementation (OSPFv2 and OSPFv3).
The syntax is the same, but the output of `show ip ospf interface` differs slightly (e.g., cost displayed as 'Cost: 10'). On ASA firewalls, OSPF is supported in routed mode; the command is `ospf cost <value>` under interface configuration mode (not `ip ospf cost`). For IOS-XR (e.g., ASR 9000), the command is `ospf cost <value>` under the interface configuration within the OSPF process (router ospf, then interface).
There is no `ip ospf cost` at the interface level. Behavior differences: In IOS 12.x and earlier, the default reference bandwidth was 100 Mbps; in 15.x and later, it remains 100 Mbps unless changed. The `ip ospf cost` command overrides the auto-cost regardless of version.
In IOS-XE 16.x, the command works identically. Always verify with the specific platform documentation, as some platforms may have limitations on cost values (e.g., some older switches only support up to 65535).
Related Commands
Practice for the CCNA 200-301
Test your knowledge with practice questions covering all CCNA 200-301 exam domains.
Practice CCNA 200-301 Questions