OSPF 配置与排错
High Contrast
Dark Mode
Light Mode
Sepia
Forest
1 min read262 words

OSPF 配置与排错

OSPF 邻居不建立、路由不出现在表里、网络变化后收敛慢——这些是最常见的 OSPF 故障。掌握系统性排障思路,比死记命令更重要。


完整配置示例(三路由器拓扑)

拓扑:
PC-A          R1          R2          R3          PC-B
10.0.1.0/24 ──[eth1]──[eth2]──[eth1]──[eth2]──[eth1]── 10.0.3.0/24
10.0.1.1  10.0.12.1  10.0.12.2  10.0.23.1  10.0.23.2  10.0.3.1
# === R1 配置(FRRouting vtysh)===
R1# configure terminal
R1(config)# interface eth1
R1(config-if)# ip address 10.0.1.1/24
R1(config-if)# ip ospf area 0
R1(config-if)# exit
R1(config)# interface eth2
R1(config-if)# ip address 10.0.12.1/30
R1(config-if)# ip ospf area 0
R1(config-if)# exit
R1(config)# router ospf
R1(config-router)# router-id 1.1.1.1
R1(config-router)# passive-interface eth1   # 不向 PC-A 方向发 Hello
R1(config-router)# exit
# === R2 配置 ===
R2# configure terminal
R2(config)# interface eth1
R2(config-if)# ip address 10.0.12.2/30
R2(config-if)# ip ospf area 0
R2(config-if)# exit
R2(config)# interface eth2
R2(config-if)# ip address 10.0.23.1/30
R2(config-if)# ip ospf area 0
R2(config-if)# exit
R2(config)# router ospf
R2(config-router)# router-id 2.2.2.2
R2(config-router)# exit
# === 验证 R1 ===
R1# show ip ospf neighbor
Neighbor ID   Pri State     Dead Time  Address    Interface
2.2.2.2         1 Full/DR   00:00:38   10.0.12.2  eth2
R1# show ip route ospf
O   10.0.23.0/30 [110/2] via 10.0.12.2, eth2
O   10.0.3.0/24  [110/3] via 10.0.12.2, eth2

OSPF Cost 调优

默认 Cost = 参考带宽(100 Mbps)/ 接口带宽
100 Mbps  → Cost = 1
1 Gbps    → Cost = 1   ← 问题:1G 和 100M 的 Cost 相同!
10 Gbps   → Cost = 1
解决:调高参考带宽
R1(config-router)# auto-cost reference-bandwidth 10000   # 10 Gbps
此后:
100 Mbps  → Cost = 100
1 Gbps    → Cost = 10
10 Gbps   → Cost = 1
或者手动设置接口 Cost:
R1(config-if)# ip ospf cost 10
查看 Cost:
R1# show ip ospf interface eth2
Cost: 10

OSPF 排障系统方法

故障:邻居不建立(Stuck in Init 或 不出现)

# 步骤 1:检查接口是否启用 OSPF
R1# show ip ospf interface brief
Interface    PID   Area   IP Addr/Mask   Cost  State  Nbrs F/C
eth2         1     0      10.0.12.1/30   1     DR     0/0    ← 0个邻居
# 步骤 2:检查 Hello 是否发出和收到
R1# debug ip ospf hello
# 应该看到:OSPF Hello 从 eth2 发出,来自 2.2.2.2 的 Hello 被接收
# 步骤 3:检查参数是否匹配
R1# show ip ospf interface eth2
Timer intervals configured, Hello 10, Dead 40
Area: 0.0.0.0
Process ID 1, Router ID 1.1.1.1
# 对比 R2 的参数,必须匹配:
#   Area ID、Hello/Dead 间隔、认证、子网掩码
# 步骤 4:检查防火墙(OSPF 使用 IP 协议号 89,不是 TCP/UDP)
iptables -L INPUT -n | grep -E "(ospf|89)"
# 如果有规则拦截 → 添加放行:
iptables -I INPUT -p 89 -j ACCEPT
iptables -I INPUT -d 224.0.0.5 -j ACCEPT   # 所有 DR 多播地址
iptables -I INPUT -d 224.0.0.6 -j ACCEPT   # DR/BDR 多播地址

故障:邻居 Full 但路由不见了

# 步骤 1:检查 LSDB 是否有该路由的 LSA
R1# show ip ospf database router 3.3.3.3    # 查看 R3 的 LSA
# 如果没有 → LSA 没有传播到 R1
# 步骤 2:检查区域配置(路由是否在正确区域)
R1# show ip ospf database
# 查看 Router LSA 数量是否正确
# 步骤 3:检查是否有 ACL 过滤了 OSPF 路由
R1# show ip protocols    # 查看是否有 distribute-list 过滤路由
# 步骤 4:检查最大路径数
R1# show ip ospf | grep "maximum-paths"
# 如果是 1,ECMP 路由会只显示一条

故障:OSPF 收敛后又振荡(Flapping)

# 查看邻居状态变化日志
R1# show log | grep "OSPF"
# 频繁的 Down/Up → 链路不稳定或 Dead 计时器频繁超时
# 检查 Hello Dead 计时器(Dead = Hello × 4)
R1# show ip ospf interface eth2 | grep Timer
Timer intervals: Hello 10, Dead 40
# 如果链路质量差,可以调大 Dead 间隔:
R1(config-if)# ip ospf dead-interval 120
R1(config-if)# ip ospf hello-interval 30   # 必须同步修改两端

发布默认路由到 OSPF(让其他路由器学到默认路由)

# 场景:R1 连接互联网,让 OSPF 域内所有路由器通过 R1 上网
# R1 本地有默认路由
ip route add default via 203.0.113.1
# 将默认路由重分发到 OSPF(Type 5 LSA)
R1(config-router)# default-information originate
# 强制发布默认路由(即使本地没有默认路由)
R1(config-router)# default-information originate always
# 其他路由器验证:
R2# show ip route | grep "O\*E"
O*E2  0.0.0.0/0 [110/1] via 10.0.12.1, eth1

CCNA 对应考点

考纲位置:Domain 3.4 — Configure and verify single area OSPFv2

常见 OSPF 排错命令(考试和工作都要记): - show ip ospf neighbor — 邻居状态 - show ip ospf interface — 接口参数 - show ip ospf database — LSDB 内容 - show ip route ospf — OSPF 学到的路由 - debug ip ospf adj — 邻接建立调试


下一节BGP 简介:互联网路由——OSPF 管理企业内网,BGP 管理互联网——全球 90,000+ 个自治系统之间靠 BGP 交换路由。