详解配置OSPF 动态路由

OSPF开放式最短路径优先协议,是目前网络中应用最广泛的路由协议之一。属于内部网关路由协议,能够适应各种规模的网络环境,是典型的链路状态协议,本篇文章重点为大家讲解一下配置OSPF 动态路由具体方法。

实验目的

掌握 OSPF 协议的配置方法 掌握查看通过动态路由协议 OSPF 学习产生的路由 熟悉广域网线缆的链接方式

实验背景

假设校园网通过一台三层交换机连到校园网出口路由器上,路由器再和校园外的另一台路由器连接。现要做适当配置,实现校园网内部主机与校园网外部主机之间的相互通信。为了简化网管的管理维护工作,学校决定采用 OSPF 协议实现互通。

技术原理

OSPF(Open Shortest Path First) 开放式最短路径优先协议,是目前网路中应用最广泛的路由协议之一。属于内部网关路由协议,能够适应各种规模的网络环境,是典型的链路状态协议。OSPF路由协议通过向全网扩散本设备的链路状态信息,使网络中每台设备最终同步一个具有全网链路状态的数据库,然后路由器采用 SPF 算法,以自己为根,计算到达其他网络的最短路径,最终形成全网路由信息。

OSPF(Open Shortest Path First)是一个内部网关协议(Interior Gateway Protocol,简称IGP)。与RIP相对,OSPF是链路状态路由协议,而RIP是距离向量路由协议。 链路是路由器接口的另一种说法,因此OSPF也称为接口状态路由协议。OSPF通过路由器之间通告网络接口的状态来建立链路状态数据库,生成最短路径树,每个OSPF路由器使用这些最短路径构造路由表。

基本概念和术语

链路状态

OSPF路由器收集其所在网络区域上各路由器的连接状态信息,即链路状态信息(Link-State),生成链路状态数据库(Link-State Database)。路由器掌握了该区域上所有路由器的链路状态信息,也就等于了解了整个网络的拓扑状况。OSPF路由器利用“最短路径优先算法(Shortest Path First, SPF)”,独立地计算出到达任意目的地的路由。

区域

OSPF协议引入“分层路由”的概念,将网络分割成一个“主干”连接的一组相互独立的部分,这些相互独立的部分被称为“区域”(Area),“主干”的部分称为“主干区域”。每个区域就如同一个独立的网络,该区域的OSPF路由器只保存该区域的链路状态。每个路由器的链路状态数据库都可以保持合理的大小,路由计算的时间、报文数量都不会过大。

共有五种区域的主要区别在于它们和外部路由器间的关系:

标准区域: 一个标准区域可以接收链路更新信息和路由总结。

主干区域(传递区域):主干区域是连接各个区域的中心实体。主干区域始终是“区域0”,所有其他的区域都要连接到这个区域上交换路由信息。主干区域拥有标准区域的所有性质。

存根区域(stub Area):存根区域是不接受自治系统以外的路由信息的区域。如果需要自治系统以外的路由,它使用默认路由0.0.0.0。

完全存根区域:它不接受外部自治系统的路由以及自治系统内其他区域的路由总结。需要发送到区域外的报文则使用默认路由:0.0.0.0。完全存根区域是Cisco自己定义的。

不完全存根区域(NSAA): 它类似于存根区域,但是允许接收以LSA Type 7发送的外部路由信息,并且要把LSA Type 7转换成LSA Type 5。

度量值cost(链路开销)

OSPF的度量值cost(链路开销)它是根据链路带宽算出来的。基本上是和链路带宽成反比。也就是说带宽越大,开销值越小,链路越优。计算公式为: 接口开销=参考带宽/逻辑带宽 (逻辑带宽通常配置和物理接口带宽相同) OSPF先将链路每段的开销分别计算,然后计算从当前节点到达任意目标地址的网络开销,即多段链路累加。选出到达目标网络开销最小的路径,为最佳路径。 ospf接口开销有默认的参考值,即接口带宽默认为100Mbps,如果实际带宽值为10M,那么该接口的cost=100/10=10,如果该接口实际带宽为100Mpbs那么接口开销为cost=100/100=1 。但现在的网络已经进入1000M时代,就会出现100M和1000M的带宽在ospf中得到的开销相同都是1。所以如果实际应用中如果接口带宽值较高时应该重新配置端口的参考带宽值。

O 192.168.4.0/24 [110/65] via 192.168.3.2, 00:01:00, Vlan20 O 路由协议代码:O表示OSPF,R代表RIP,S代表静态,使用show命令的时候,在最上方有说明。 192.168.4.0/24 分别是目标网络和目标网络的掩码 [110/65] 分别表示管理距离和度量值。

管理距离

管理距离是用来比较不同路由协议的优劣的,如这台路由器同时启用了RIP和OSPF两种动态路由协议,都会得出到达192.168.4.0/24的网络路径,但是那种路由协议的结果会被记录到路由表中呢?就是通过管理距离比较算法的优劣。管理距离的值越小,路由器认为该协议的算法越好,计算结果越准确,优先采用该协议的结果。

常见路由协议的管理距离有: 直连路由 0 静态路由 1 EIGRP汇总路由 5 EBGP 20 EIGRP 90 IGRP 100 OSPF 110 IS-IS 115 RIP(v1&v2) 120 EGP 140 ODR 160 ExEIGRP (外部EIGRP) 170 IBGP 200 未知 255

度量值是用来衡量同一种路由协议计算到达相同目标地址的多条路径的优劣的参数。度量值最小的会被记录到路由表。比如:OSPF计算到达192.168.4.0/24网络有三条路径开销值分别是:145 ,230 和99,比较后发现99的这条路径度量值最小,会被认为是最佳路径,加入到路由表。常用来作为度量值的参考因素有: 跳数、带宽、延迟、可靠性、负载、MTU等。 不同的路由协议用来作为度量值的参数是不同的,有的是一种,有的是多种。如RIP使用跳数做度量值,EIGRP默认使用带宽和延迟两种,最多可以使用五种。OSPF使用cost(开销)作为度量值,cost和端口的带宽相关,基本上是和端口带宽成反比,即端口带宽越大,开销越小,链路越优。 via 192.168.3.2 表示下一跳地址。即到达192.168.4.0网络首先要到达192.168.3.2才可以。另外via也可以理解为:更新源,即该路由消息是来自于谁。 00:01:00 该消息的更新时间。 Vlan20 出接口,表示到达目标网络应该从自己的那个接口将数据发出。

路由表的五要

目标地址 目标网络掩码 优先级(度量值和管理距离) 下一跳地址 出接口**

实验步骤

新建 packet tracer 拓扑图

路由器 OSPF 动态路由配置路由器 OSPF 动态路由配置

(1)在本实验中的三层交换机上划分 VLAN10 和 VLAN20,其中 VLAN10 用于 连接校园网主机,VLAN20 用于连接 R1 (2)路由器之间通过 V35 电缆通过串口连接,DCE 端连接在 R1 上,配置其时钟 频率 64000 (3)主机和交换机通过直连线,主机与路由器通过交叉线连接 (4)在 S3560 上配置 OSPF 路由协议 (5)在路由器 R1、R2 上配置 OSPF 路由协议 (6)将 PC1、PC2 主机默认网关设置为与直连网路设备接口 IP 地址 (7)验证 PC1、PC2 主机之间可以互相同信

实验设备

PC 2 台;Switch_3560 1 台;Router-PT 2 台;直连线;交叉线;DCE 串口线

PC1
IP: 192.168.1.2
Submask: 255.255.255.0
Gateway: 192.168.1.1

PC2
IP: 192.168.2.2
Submask: 255.255.255.0
Gateway: 192.168.2.1

S3560

en
conf t
hostname S3560
vlan 10
exit
vlan 20
exit
interface fa 0/10
switchport access vlan 10
exit
int fa 0/20
switchport access valn 20
exit
interface vlan 10
ip address 192.168.1.1 255.255.255.0

no shutdown
exit
interface vlan 20
ip address 192.168.3.1 255.255.255.0
no shutdown
end
show ip route
conf t
router ospf 1 #指定ospf的进程号为1
network 192.168.1.0 0.0.0.255 area 0 #宣告192.168.1.0网段反掩码为0.0.0.255  区域为骨干区域area 0
network 192.168.3.0 0.0.0.255 area 0
end
show ip route

R1
en
conf t
hostname R1
interface fa 0/0
no shutdown
ip address 192.168.3.2 255.255.255.0
exit
interface serial 2/0
no shutdown
clock rate 64000
ip address 192.168.4.1 255.255.255.0
end
show ip route
conf t
router ospf 1
network 192.168.3.0 0.0.0.255 area 0
network 192.168.4.0 0.0.0.255 area 0
end
show ip route

R2
en
conf t
hostname R2
interface fa 0/0
no shutdown
ip address 192.168.2.1 255.255.255.0

exit
interface serial 2/0
no shutdown
ip address 192.168.4.2 255.255.255.0
end
show ip route
conf t
router ospf 1
network 192.168.2.0 0.0.0.255 area 0
network 192.168.4.0 0.0.0.255 area 0
end
show ip route

实战演练

S3560

Switch>en
Switch#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
Switch(config)#hostname
% Incomplete command.
Switch(config)#hostname S3560
S3560(config)#vlan 10
S3560(config-vlan)#exit
S3560(config)#vlan 20
S3560(config-vlan)#int fa 0/10
S3560(config-if)#sw acc vlan 10
S3560(config-if)#exit
S3560(config)#int fa 0/20
S3560(config-if)#sw acc vlan 20
S3560(config-if)#exit
S3560(config)#int vlan 10

%LINK-5-CHANGED: Interface Vlan10, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan10, changed state to up
S3560(config-if)#ip address 192.168.1.1 255.255.255.0
S3560(config-if)#no shut
S3560(config-if)#exit
S3560(config)#int vlan 20
S3560(config-if)#
%LINK-5-CHANGED: Interface Vlan20, changed state to up

S3560(config-if)#ip address 192.168.3.1 255.255.255.0
S3560(config-if)#no shut
S3560(config-if)#end
S3560#
%SYS-5-CONFIG_I: Configured from console by console

S3560#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
      D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
      N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
      E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
      i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
      * - candidate default, U - per-user static route, o - ODR
      P - periodic downloaded static route

Gateway of last resort is not set

C    192.168.1.0/24 is directly connected, Vlan10
S3560#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
S3560(config)#router ospf 1
S3560(config-router)#network 192.168.1.0 0.0.0.255 area 0
S3560(config-router)#network 192.168.3.0 0.0.0.255 area 0
S3560(config-router)#
S3560(config-router)#end
S3560#
%SYS-5-CONFIG_I: Configured from console by console

S3560#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
      D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
      N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
      E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
      i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
      * - candidate default, U - per-user static route, o - ODR
      P - periodic downloaded static route

Gateway of last resort is not set

C    192.168.1.0/24 is directly connected, Vlan10
S3560#
%LINK-5-CHANGED: Interface FastEthernet0/20, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/20, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan20, changed state to up

00:12:29: %OSPF-5-ADJCHG: Process 1, Nbr 192.168.4.1 on Vlan20 from LOADING to FULL, Loading Done

R1

Continue with configuration dialog? [yes/no]: n


Press RETURN to get started!



Router>en
Router#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#hostname R1
R1(config)#int fa 0/0
R1(config-if)#no shut

%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up

R1(config-if)#ip address 192.168.3.2 255.255.255.0
R1(config-if)#exit
R1(config)#
R1(config)#int serial 2/0
R1(config-if)#no shut

%LINK-5-CHANGED: Interface Serial2/0, changed state to down
R1(config-if)#clock rate 64000
R1(config-if)#ip address 192.168.4.1 255.255.255.0
R1(config-if)#end
R1#
%SYS-5-CONFIG_I: Configured from console by console

R1#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
      D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
      N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
      E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
      i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
      * - candidate default, U - per-user static route, o - ODR
      P - periodic downloaded static route

Gateway of last resort is not set

C    192.168.3.0/24 is directly connected, FastEthernet0/0
R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#router ospf 1
R1(config-router)#network 192.168.3.0 0.0.0.255 area 0
R1(config-router)#network 192.168.4.0 0.0.0.255 area 0
R1(config-router)#end
R1#
%SYS-5-CONFIG_I: Configured from console by console

R1#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
      D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
      N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
      E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
      i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
      * - candidate default, U - per-user static route, o - ODR
      P - periodic downloaded static route

Gateway of last resort is not set

O    192.168.1.0/24 [110/2] via 192.168.3.1, 00:00:23, FastEthernet0/0
C    192.168.3.0/24 is directly connected, FastEthernet0/0
R1#

R2

Router>en
Router#
Router#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#hostname R2
R2(config)#int fa 0/0
R2(config-if)#no shut

%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up

R2(config-if)#ip address 192.168.2.1 255.255.255.0
R2(config-if)#exit
R2(config)#
R2(config)#int serial 2/0
R2(config-if)#no shut

%LINK-5-CHANGED: Interface Serial2/0, changed state to up

R2(config-if)#ip add
%LINEPROTO-5-UPDOWN: Line protocol on Interface Serial2/0, changed state to up

% Incomplete command.
R2(config-if)#ip add 192.168.4.2 255.255.255.0
R2(config-if)#end
R2#
%SYS-5-CONFIG_I: Configured from console by console

R2#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
      D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
      N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
      E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
      i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
      * - candidate default, U - per-user static route, o - ODR
      P - periodic downloaded static route

Gateway of last resort is not set

C    192.168.2.0/24 is directly connected, FastEthernet0/0
C    192.168.4.0/24 is directly connected, Serial2/0
R2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R2(config)#router ospf 1
R2(config-router)#
R2(config-router)#network 192.168.2.0 0.0.0.255 area 0
R2(config-router)#network 192.168.4.0 0.0.0.255 area 0
R2(config-router)#end
R2#
%SYS-5-CONFIG_I: Configured from console by console

R2#
00:14:52: %OSPF-5-ADJCHG: Process 1, Nbr 192.168.4.1 on Serial2/0 from LOADING to FULL, Loading Done

R2#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
      D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
      N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
      E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
      i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
      * - candidate default, U - per-user static route, o - ODR
      P - periodic downloaded static route

Gateway of last resort is not set

O    192.168.1.0/24 [110/66] via 192.168.4.1, 00:00:03, Serial2/0
C    192.168.2.0/24 is directly connected, FastEthernet0/0
O    192.168.3.0/24 [110/65] via 192.168.4.1, 00:00:03, Serial2/0
C    192.168.4.0/24 is directly connected, Serial2/0
R2#

测试

PC>ipconfig

IP Address......................: 192.168.1.2
Subnet Mask.....................: 255.255.255.0
Default Gateway.................: 192.168.1.1

PC>ping 192.168.2.2

Pinging 192.168.2.2 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.168.2.2:
   Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

PC>


ping 不通,查看三层交换机的路由表信息并开启路由转发功能

S3560#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
      D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
      N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
      E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
      i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
      * - candidate default, U - per-user static route, o - ODR
      P - periodic downloaded static route

Gateway of last resort is not set

C    192.168.1.0/24 is directly connected, Vlan10
O    192.168.2.0/24 [110/66] via 192.168.3.2, 00:04:27, Vlan20
C    192.168.3.0/24 is directly connected, Vlan20
O    192.168.4.0/24 [110/65] via 192.168.3.2, 00:05:43, Vlan20
S3560#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
S3560(config)#
S3560(config)#ip routing
S3560(config)#

再次观察测试情况
PC>ping -t 192.168.2.2

Pinging 192.168.2.2 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.2.2: bytes=32 time=18ms TTL=125
Reply from 192.168.2.2: bytes=32 time=23ms TTL=125
Reply from 192.168.2.2: bytes=32 time=19ms TTL=125
Reply from 192.168.2.2: bytes=32 time=22ms TTL=125
Reply from 192.168.2.2: bytes=32 time=20ms TTL=125
Reply from 192.168.2.2: bytes=32 time=21ms TTL=125
Reply from 192.168.2.2: bytes=32 time=18ms TTL=125

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/221171.html<

(0)
管理的头像管理
上一篇2025-04-14 22:04
下一篇 2025-04-14 22:05

相关推荐

  • jsp空间购买和交换数据空间怎么买,有哪些注意事项?

    购买JSP空间时,是否考虑过数据交换空间的性能?简米科技(2003年始创,23年行业沉淀)与酷番云(工信部一类增值电信全牌照)这类持牌自营机房的服务商,能确保数据交换的高效稳定,是值得优先选择的合作伙伴,为什么JSP空间需要搭配独立的数据交换空间从JSP应用特性看数据交换需求JSP基于Java技术,常用于企业级……

    2026-08-11
    0
  • 建网站用香港空间效果怎么样,香港空间稳定吗?

    建网站用香港空间,对于创建网站资产来说,核心价值在于免备案和全球带宽优势,尤其适合外贸、跨境电商和需要快速启动的项目,但你必须权衡国内访问延迟,并选择有资质的服务商以保证资产安全,香港空间的核心优势与适用边界免备案:节省时间就是节省成本国内服务器需要备案,通常需要10到20天,香港空间无需备案,域名解析后即可上……

    2026-08-11
    0
  • Java连接云数据库的方法是什么,如何操作

    Java连接云数据库的核心在于通过JDBC驱动,结合云服务商提供的连接地址、端口、数据库名及认证信息,配置安全策略(如SSL、IP白名单),即可实现稳定高效的远程数据库访问,基础准备:JDBC驱动与依赖管理连接云数据库前,需要确保开发环境具备对应的JDBC驱动,以最常见的MySQL为例,你需要引入mysql-c……

    2026-08-11
    0
  • 建网站公安联网备案必须使用数据码吗,备案流程是什么

    网站备案包括ICP备案和公安联网备案,两者缺一不可,公安联网备案必须使用服务商提供的数据码,选择持有合法资质的服务商是顺利通过备案的前提,为什么网站必须进行公安联网备案根据公安部《计算机信息网络国际联网安全保护管理办法》,网站开通后30日内必须到公安机关办理备案手续,未完成公安备案的网站,面临责令整改、关闭网站……

    2026-08-10
    0
  • 建一个企业网站大概需要多少钱?,怎么收费?

    建网站要多少钱,没有一个固定的数字,几百到几万都可能,但真正的“创建网站资产”绝不仅仅是初次投入的成本,而是基于长期稳定、合规和安全的持续性投入,其中核心取决于你选择了什么样的“地基”来承载你的业务,建站预算的构成与行业基准当你开始规划一个网站,最先面对的就是预算问题,一个常见的误区是只关注网站“看起来”的建造……

    2026-08-10
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注