路由器静态路由和动态路由的配置。
(1) 理解路由表 (2) 掌握静态路由和动态路由的配置 (3) 理解 RIP 和 OSPF 协议的工作过程
(1) 在小规模环境里,静态路由是最佳的选择 (2) 静态路由开销小,但不灵活,使用于相对稳定的网络
(1) DCR 路由器 3 台 (2) CR-V35FC 1 条 (3) CR-V35MT 1条
静态路由与 RIP 协议配置的实验拓扑:
OSPF 协议配置的拓扑结构:
配置路由器的 IP 地址:
Router-A | Router-B | Router-C | |||
---|---|---|---|---|---|
F0/1 | 192.168.1.1 | F0/1 | 192.168.2.1 | F0/0 | 192.168.2.2 |
F0/0 | 192.168.0.1 | F0/0 | 192.168.1.2 | E0/1 | 192.168.3.1 |
首先改一下名字:
同种设备之间互联使用的是交叉线。
首先配置 PC1 的 IP 地址、子网掩码、默认网关:
验证 PC1:
配置路由器 A 的 IP 地址:
RouterA#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
RouterA(config)#int f0/0
RouterA(config-if)#ip address 192.168.0.1 255.255.255.0
RouterA(config-if)#no shutdown
RouterA(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
RouterA(config-if)#exit
RouterA(config)#int f0/1
RouterA(config-if)#ip address 192.168.1.1 255.255.255.0
RouterA(config-if)#no shutdown
RouterA(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up
RouterA(config-if)#
不要忘记 no shutdown
。
然后验证一下:
配置路由器B:
RouterB>en
RouterB#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
RouterB(config)#int f0/0
RouterB(config-if)#ip address 192.168.1.2 255.255.255.0
RouterB(config-if)#no shutdown
RouterB(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
RouterB(config-if)#exit
RouterB(config)#int f0/1
RouterB(config-if)#ip address 192.168.2.1 255.255.255.0
RouterB(config-if)#no shutdown
RouterB(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up
RouterB(config-if)#exit
检查一下配置:
配置路由器 C :
RouterC>en
RouterC#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
RouterC(config)#int f0/0
RouterC(config-if)#ip address 192.168.2.2 255.255.255.0
RouterC(config-if)#no shutdown
RouterC(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
RouterC(config-if)#exit
RouterC(config)#int f0/1
RouterC(config-if)#ip address 192.168.3.1 255.255.255.0
RouterC(config-if)#no shutdown
RouterC(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up
RouterC(config-if)#exit
RouterC(config)#exit
检查一下配置:
配置到这里可以发现配置过的都已经通了:
还有 PC2 的 IP 地址没有配置:
然后检查一下结果:
我们用 PC1 去 ping 路由器 A:
结果是可以 ping
通的。
ping 路由器B 不通。 ping 另一个接口也不通,显示目标主机不可达。
路由器 A 去 ping 路由器B,结果是 ping 不通的:
再去 ping 路由器C:
也是 ping 不通的。 但是都配置好了,为什么 ping 不通? 因为路由表没有设置,可以采用静态路由或者动态路由的方式设置,动态路由一般采用 RIP 协议。这里我们先来看静态路由的设置方法。
show ip route
只有两条记录而且是直连的。 第一列表示的路由的源:
查看 B 也是直连的。
C也是,都是直连的话一旦遇到非直连的情况,他们就不知道怎么走了。 所以到目前为止就只有直连的机器之间可以互相通信,我们可以通过设置静态路由的方式使得其它的设备之间也可以通信。
在 A 上 ping C ping 不通:
两个接口都不通。
使用 ip route
命令关键字,第一个参数是目的网络地址,第二个参数是目的网络的子网掩码,第三个参数是下一跳的 IP
地址。
路由器 A 要想到达 192.168.2.0
这个网络,那么必须经过路由器 B 的转发,中间的那个网络是 192.168.1.0
,路由器 A 与之连接的是 f0/1
接口,它的 ip
地址是 192.168.1.1
,而路由器的 f0/0
接口是 192.168.1.2
所以路由器 A 的下一跳应该是 路由器 B 的 f0/0
接口。
所以路由器要想到达 192.168.3
这个网络也必须经过路由器B和C 的转发。
在路由器 A 中做如下配置:
RouterA(config)#ip route 192.168.2.0 255.255.255.0 192.168.1.2
RouterA(config)#ip route 192.168.3.0 255.255.255.0 192.168.1.2
再次输入 show ip route
指令看一下:
发现多出来两条静态路由。
其中 [1/0]
代表静态路由的优先级最高。
1-255
,值越小优先级越高;然后按照同样的方法,我们在路由器 B 上面也配置一下静态路由:
RouterB(config)#ip route 192.168.0.0 255.255.255.0 192.168.1.1
RouterB(config)#ip route 192.168.3.0 255.255.255.0 192.168.2.2
检查一下配置的结果:
发现多出来两条静态路由。
同样的方法再路由器 B 上也设置一下静态路由,同时看一下结果:
再用 PC1 去 ping
路由器B是通的,ping A 也是通的:
****
**** 用 PC 1 去ping 路由器 C:
发现都可以 ping
通了,这就说明静态路由生效了。
下面配置 RIP 协议:
还是按照这张表配置:
首先配置 PC1 的 IP 地址、子网掩码、默认网关:
验证 PC1:
配置路由器 A 的 IP 地址:
RouterA#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
RouterA(config)#int f0/0
RouterA(config-if)#ip address 192.168.0.1 255.255.255.0
RouterA(config-if)#no shutdown
RouterA(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
RouterA(config-if)#exit
RouterA(config)#int f0/1
RouterA(config-if)#ip address 192.168.1.1 255.255.255.0
RouterA(config-if)#no shutdown
RouterA(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up
RouterA(config-if)#
不要忘记 no shutdown
。
然后验证一下:
这个时候发现已经通了一部分了:
配置路由器B:
RouterB>en
RouterB#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
RouterB(config)#int f0/0
RouterB(config-if)#ip address 192.168.1.2 255.255.255.0
RouterB(config-if)#no shutdown
RouterB(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
RouterB(config-if)#exit
RouterB(config)#int f0/1
RouterB(config-if)#ip address 192.168.2.1 255.255.255.0
RouterB(config-if)#no shutdown
RouterB(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up
RouterB(config-if)#exit
检查一下配置:
配置路由器 C :
RouterC>en
RouterC#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
RouterC(config)#int f0/0
RouterC(config-if)#ip address 192.168.2.2 255.255.255.0
RouterC(config-if)#no shutdown
RouterC(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
RouterC(config-if)#exit
RouterC(config)#int f0/1
RouterC(config-if)#ip address 192.168.3.1 255.255.255.0
RouterC(config-if)#no shutdown
RouterC(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up
RouterC(config-if)#exit
RouterC(config)#exit
检查一下配置:
配置到这里可以发现配置过的都已经通了:
还有 PC2 的 IP 地址没有配置:
然后检查一下结果:
我们用 PC1 去 ping 路由器 A:
结果是可以 ping
通的。
ping
路由器B 不通。
ping
另一个接口也不通,显示目标主机不可达。
查看 ROUTER-A 的路由表:
查看 ROUTER-B 的路由表:
查看 ROUTER-C 的路由表:
发现现在是 ping
不通的。
route rip
network 192.168.0.0 # 向外通报本路由器的直连路由
netwprk 192.168.1.0
配置完 B 的 RIP 之后:
发现他学习到了一个新的路由,因为 路由器 A 和路由器 B 都已经开启了 RIP 协议,这样他们就可以交换路由信息了。
这个时候再去看 A ,发现也已经有了新的路由记录:
路由器 A:
路由器 B:
RIP 的三要素是:
通过路由器 B 我们可以明显的看到变化,从 2 到 3 再到 4;
原因非常简单,按照固定的时间交换,我配置完RouterC之后立马就生效了;
因为要满足三要素,固定时间交换,收敛需要一定的时间;
刚才不同现在通了;
在路由器A去ping路由器B;
去 ping
其他几个网络都是通的。
下面配置 OSPF 协议。
首先它的拓扑结构是这样的:
也就是用路由器 A 的 f0/1
接口和路由器 B 的 f0/1
接口连接起来,同时设定 ip
地址为指定的 ip
。
f0/1
接口与loopback0
接口输入以下命令配置:
Router>en
Router#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#int f0/1
Router(config-if)#ip address 192.168.1.1 255.255.255.0
Router(config-if)#no shutdown
Router(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up
Router(config-if)#int loopback0
Router(config-if)#
%LINK-5-CHANGED: Interface Loopback0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback0, changed state to up
Router(config-if)#ip address 10.10.10.1 255.255.255.0
Router(config-if)#end
Router#
%SYS-5-CONFIG_I: Configured from console by console
Router#en
Router#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#hostname RouterA
RouterA(config)#end
RouterA#
%SYS-5-CONFIG_I: Configured from console by console
RouterA#
f0/1
的接口之后记得转为 up
状态;loopback0
接口之后不需要说动转化状态,他已经自动转为 up
了。
记得改名字为 RouterA
:
然后验证一下:
配置成功,现在去配置 路由器B 的 ip
;
f0/1
接口与loopback0
接口同样的操作:
Router>en
Router#config
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#hostname RouterB
RouterB(config)#int f0/1
RouterB(config-if)#ip address 192.168.1.2 255.255.255.0
RouterB(config-if)#no shutdown
RouterB(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up
RouterB(config-if)#exit
RouterB(config)#int loopback0
RouterB(config-if)#
%LINK-5-CHANGED: Interface Loopback0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback0, changed state to up
RouterB(config-if)#ip address 10.10.11.1 255.255.255.0
RouterB(config-if)#end
show run
验证一下:
这个时候发现线路已经通了。
此时相当于是两个路由器连接了三个网络,但是路由器的路由表中只有与之直连的网络。
验证一下,看一下路由表:
路由器 A 只有两个直连路由,来看路由器 B:
也只有两个直连路由。
我们通过 OSPF 协议来交换路由表中的路由:
首先是路由器 A:
route ospf 2 # 2 是进程的标识,可随意取
network 10.10.10.0 255.255.255.0 area 0 # 向外通报自己的路由,必须跟掩码,因为他是有类路由协议
network 192.168.1.0 255.255.255.0 area 0
配置完之后去 show
一下,这个时候还是没有变,因为其他路由器并没有和他交换路由信息,目前只有他自己:
这个时候我们去修改 B 的配置信息:
route ospf 2 # 2 是进程的标识,可随意取
network 10.10.10.0 255.255.255.0 area 0 # 向外通报自己的路由,必须跟掩码,因为他是有类路由协议
network 192.168.1.0 255.255.255.0 area 0
这个时候我眼疾手快,立马执行了 show ip route
,但是发现还是只有两个直连路由信息,然后突然蹦出来一条消息:
00:40:14: %OSPF-5-ADJCHG: Process 3, Nbr 10.10.10.1 on FastEthernet0/1 from LOADING to FULL, Loading Done
这应该是状态变化的提示信息,于是我再次看一下路由表:
发现已经学习到了一条新的路由。
前面已经看了路由器 B 的路由表成功了,现在再来看一下 A 的信息:
发现 A 这边也会先提示一下,然后 show
的时候就有了一个 O
。
110
,而 RIP
实验中是 120
,这说明 OSPF
的优先级高于 RIP
;验证一下路由器 A :
验证一下路由器 B:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有