第 3 章静态路由 转发数据包是路由器的最主要功能 路由器转发数据包时需要查找路由表, 管理员可以通过手工的方法在路由器中直接配置路由表, 这就是静态路由 虽然静态路由不适合于在大的网络中使用, 但是由于静态路由简单 路由器负载小 可控性强等原因, 在许多场合中还经常被使用 本章将介绍静态路由的配置, 同时为以后配置动态路由奠定基础 3.1.1 静态路由介绍 3.1 静态路由与默认路由 路由器在转发数据时, 要先在路由表 (routing table) 中查找相应的路由 路由器有这么三种途径建立路由 : (1) 直连网络 : 路由器自动添加和自己直接连接的网络的路由 (2) 静态路由 : 管理员手动输入到路由器的路由 (3) 动态路由 : 由路由协议 (routing protocol) 动态建立的路由静态路由的缺点是不能动态反映网络拓扑, 当网络拓扑发生变化时, 管理员就必须手工改变路由表 ; 然而静态路不会占用路由器太多的 CPU 和 RAM 资源, 也不占用线路的带宽 如果出于安全的考虑想隐藏网络的某些部分或者管理员想控制数据转发路径, 也会使用静态路由 在一个小而简单的网络中, 也常使用静态路由, 因为配置静态路由会更为简捷 配置静态路由的命令为 ip route, 命令的格式如下 : ip route 目的网络掩码 { 网关地址 接口 } 例子 :ip route 192.168.1.0 255.255.255.0 s0/0 例子 :ip route 192.168.1.0 255.255.255.0 12.12.12.2 在写静态路由时, 如果链路是点到点的链路 ( 例如 PPP 封装的链路 ), 采用网关地址和接口都是可以的 ; 然而如果链路是多路访问的链路 ( 例如以太网 ), 则只能采用网关地址, 即不能 :ip route 192.168.1.0 255.255.255.0 f0/0 提示 有的 IOS 版本中, 采用 ip route 192.168.1.0 255.255.255.0 f0/0 时, 路由器也是正常工作的, 然而这是代理 ARP 的功劳, 建议不要采用该形式 在路由器上, 可以使用 show ip route 命令查看路由表 如下 : 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 R 172.16.0.0/16 [120/2] via 10.1.0.2, 00:00:21, Serial0/0
[120/2] via 10.3.0.2, 00:00:06, Serial0/1 10.0.0.0/16 is subnetted, 4 subnets R 10.2.0.0 [120/1] via 10.1.0.2, 00:00:21, Serial0/0 C 10.3.0.0 is directly connected, Serial0/1 C 10.1.0.0 is directly connected, Serial0/0 R 10.4.0.0 [120/1] via 10.3.0.2, 00:00:06, Serial0/1 C 192.168.1.0/24 is directly connected, FastEthernet0/0 在输出中, 首先显示路由条目各种类型的的简写, 如 : C 为直连网络, S 为静态路由 以上面带有下划的路由为例, R 表示这条路由是 RIP 协议学习得到的 ; 10.2.0.0 是目的网络 ; [120/1] 是管理距离 (Administrative Distance,AD)/ 度量值 (Metric); via 10.1.0.2 是指到达目的网络的下一跳路由器的 IP 地址 ; 00:00:21 是指路由器最近一次得知路由到现在的时间 ; Serials 0/0 是指到达下一跳应从哪个端口出去 技术要点 管理距离(AD): 用来表示路由的可信度, 路由器可能从多种途径获得同一路由, 例如 : 一个路由器要获得 10.2.0.0/24 网络的路由, 可以来自 RIP, 也可以是静态路由 不同途径获得的路由可能采取不同的路径到达目的网络, 为了区别它们的可信度, 用管理距离加以表示 表 3-1 是通过各种路由协议获得的路由的默认管理距离 路由表中管理距离值越小说明路由的可靠程度越高, 静态路由的管理距离为 1, 说明手工输入的路由优先级高于其他的路由 表 3-1 路由协议的默认管理距离路由协议管理距离直连接口 0 静态路由 1 外部 BGP 20 内部 EIGRP 90 IGRP 100 OSPF 110 RIP 120 外部 EIGRP 170 内部 BGP 200 技术要点 度量值(Metric): 某一个路由协议判别到目的网络的最佳路径的方法 当一路由器有多条路径到达某一目的网络时, 路由协议必须判断其中的哪一条是最佳的并把它放到路由表中, 路由协议会给每一条路径计算出一个数, 这个数就是度量值, 通常这个值是没有单位的 度量值越小, 这条路径越佳 然而不同的路由协议定义度量值的方法是不一样的, 所以不同的路由协议选择出的最佳距离可能是不一样的 具体请参见路由协议的章节 3.1.2 默认路由介绍 所谓的默认路由, 是指路由器在路由表中如果找不到到达目的网络的具体路由时, 最后会采用的路由 默认路由通常会在存根网络 (Stub network, 即只有一个出口的网络 ) 中使用 如图 3-1, 图中左边的网络到 Internet 上只有一个出口, 因此可以在 R2 上配置默认路由 命令为 :ip route 0.0.0.0 0.0.0.0 { 网关地址 接口 } 例子 :ip route 0.0.0.0 0.0.0.0 s0/0 例子 :ip route 0.0.0.0 0.0.0.0 12.12.12.2
图 3-1 桩网络 (Stub network) 3.1.3 ip classless 图 3-2 ip classless 示例 图 3-2 中, 如果在 R1 上配置了默认路由 :ip route 0.0.0.0 0.0.0.0 s0/0/0, 则 R1 路由器是否会把到达 10.2.2.0/24 网络的数据从 s0/0/0 接口发送出去? 这取决于是否执行了 ip classless 命令 如果执行了 ip classless 命令 ( 实际上这是默认值 ), 则路由器存在默认路由时, 所有在路由表中查不到具体路由的数据包将通过默认路由发送 如果执行了 no ip classless 命令, 当路由器存在一主类网络的某一子网路由时, 路由器将认为自己已经知道该主类网络的全部子网的路由, 这时即使存在默认路由, 到达该主类任一子网的数据包不会通过默认路由发送 图 3-2 中, 执行了 no ip classless 后, 由于 R1 路由器上有 10.0.0.0 的子网 10.1.1.0/24( 这是直连路由 ), 因此 R1 路由器收到到达 10.2.2.0/24 子网的数据包不会使用默认路由进行发送 然而如果数据包是要到达 20.2.2.0/24, 默认路由会被采用, 因为 R1 没有任何 20.0.0.0 子网的路由 1. 实验目的 通过本实验, 读者可以掌握如下技能 : (1) 路由表的概念 (2) ip route 命令的使用 (3) 根据需求正确配置静态路由 2. 实验拓扑 3.2 实验 1: 静态路由 图 3-3 实验 1 实验 2 拓扑图
3. 实验步骤 我们要使得 1.1.1.0/24 2.2.2.0/24 3.3.3.0/24 网络之间能够互相通信 (1) 步骤 1: 在各路由器上配置 IP 地址 保证直连链路的连通性 R1(config)#int loopback0 R1(config-if)#ip address 1.1.1.1 255.255.255.0 R1(config)#int s0/0/0 R1(config-if)#ip address 192.168.12.1 255.255.255.0 R1(config-if)#no shutdown R2(config)#int loopback0 R2(config-if)#ip address 2.2.2.2 255.255.255.0 R2(config)#int s0/0/0 R2(config-if)#clock rate 128000 R2(config-if)#ip address 192.168.12.2 255.255.255.0 R2(config-if)#no shutdown R2(config)#int s0/0/1 R2(config-if)#clock rate 128000 R2(config-if)#ip address 192.168.23.2 255.255.255.0 R2(config-if)#no shutdown R3(config)#int loopback0 R3(config-if)#ip address 3.3.3.3 255.255.255.0 R3(config)#int s0/0/1 R3(config-if)#ip address 192.168.23.3 255.255.255.0 R3(config-if)#no shutdown (2) 步骤 2:R1 上配置静态路由 R1(config)#ip route 2.2.2.0 255.255.255.0 s0/0/0 // 下一跳为接口形式,s0/0/0 是点对点的链路, 注意应该是 R1 上的 s0/0/0 接口 R1(config)#ip route 3.3.3.0 255.255.255.0 192.168.12.2 // 下一跳为 IP 地址形式,192.168.12.2 是 R2 上的 IP 地址 (3) 步骤 3:R2 上配置静态路由 R2(config)#ip route 1.1.1.0 255.255.255.0 s0/0/0 R2(config)#ip route 3.3.3.0 255.255.255.0 s0/0/1 (4) 步骤 4:R3 上配置静态路由 R3(config)#ip route 1.1.1.0 255.255.255.0 s0/0/1 R3(config)#ip route 2.2.2.0 255.255.255.0 s0/0/1 4. 实验调试 (1) 在 R1 R2 R3 上查看路由表 R1#show ip route Codes: C - connected, S - static, 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
i - IS-IS, su - IS-IS summary, 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.12.0/24 is directly connected, Serial0/0/0 1.0.0.0/24 is subnetted, 1 subnets C 1.1.1.0 is directly connected, Loopback0 2.0.0.0/24 is subnetted, 1 subnets S 2.2.2.0 is directly connected, Serial0/0/0 3.0.0.0/24 is subnetted, 1 subnets S 3.3.3.0 [1/0] via 192.168.12.2 R2#show ip route Codes: C - connected, S - static, 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 i - IS-IS, su - IS-IS summary, 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.12.0/24 is directly connected, Serial0/0/0 1.0.0.0/24 is subnetted, 1 subnets S 1.1.1.0 is directly connected, Serial0/0/0 2.0.0.0/24 is subnetted, 1 subnets C 2.2.2.0 is directly connected, Loopback0 3.0.0.0/24 is subnetted, 1 subnets S 3.3.3.0 is directly connected, Serial0/0/1 C 192.168.23.0/24 is directly connected, Serial0/0/1 R3#show ip route Codes: C - connected, S - static, 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 i - IS-IS, su - IS-IS summary, 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 1.0.0.0/24 is subnetted, 1 subnets S 1.1.1.0 is directly connected, Serial0/0/1 2.0.0.0/24 is subnetted, 1 subnets S 2.2.2.0 is directly connected, Serial0/0/1 3.0.0.0/24 is subnetted, 1 subnets
C 3.3.3.0 is directly connected, Loopback0 C 192.168.23.0/24 is directly connected, Serial0/0/1 (2) 从各路由器的环回口 ping 其他路由器的环回口 : R1#ping // 不带任何参数的 ping 命令, 允许我们输入更多的参数 Protocol [ip]: Target IP address: 2.2.2.2 // 目标 IP 地址 Repeat count [5]: // 发送的 ping 次数 Datagram size [100]: //ping 包的大小 Timeout in seconds [2]: // 超时时间 Extended commands [n]: y // 是否进一步扩展命令 Source address or interface: 1.1.1.1 // 源 IP 地址 Type of service [0]: Set DF bit in IP header? [no]: Validate reply data? [no]: Data pattern [0xABCD]: Loose, Strict, Record, Timestamp, Verbose[none]: Sweep range of sizes [n]: Sending 5, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds: Packet sent with a source address of 1.1.1.1!!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 12/14/16 ms // 以上说明从 R1 的 loopback0 可以 ping 通 R2 上的 loopback0 也可以直接使用命令: R1#ping 2.2.2.2 source loopback 0 Sending 5, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds: Packet sent with a source address of 1.1.1.1!!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 12/14/16 ms R2#ping 1.1.1.1 source loopback 0 R2#ping 3.3.3.3 source loopback 0 // 从 R2 的 loopback0 应该可以 ping 通 R1 和 R3 的 lopback0 接口 R3#ping 1.1.1.1 source loopback 0 R3#ping 2.2.2.2 source loopback 0 // 从 R3 的 loopback0 也应该可以 ping 通 R1 和 R2 的 lopback0 接口 提示 虽然从 R1 的 loopback0 可以 ping 通 R3 的 loopback0, 数据需要经过 192.168.23.0/24 网络, 但是在 R1 上我们并没有添加 192.168.23.0/24 的路由 路由器转发数据包完成是根据路由表的, 并且数据是一跳一跳地被转发的, 就像接力赛似的 从 R1 的 loopback0 口 ping R3 的 loopback0 口时,IP 数据包的源 IP 为 1.1.1.1, 目的 IP 为 3.3.3.3 R1 路由器首先查路由表, 数据包被发到了 R2;R2 路由器也查路由表 (3.3.3.0/24 路由 ),
数据包被发到了 R3;R3 知道这是直连路由 R3 响应 R1 的数据包进行类似的过程 (3) 从 R1 上 ping 2.2.2.2 从 R1 上 ping 3.3.3.3 R1#ping 2.2.2.2 Sending 5, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds:!!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 12/14/16 ms // 可以 ping 通 R1#ping 3.3.3.3 Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds:... Success rate is 0 percent (0/5) // 以上无法 ping 通, 原因在于使用 ping 命令时, 如果不指明源接口, 则 R1 路由器使用 s0/0/0 接口的 IP 地址 (192.168.12.1) 作为 IP 数据包的源 IP 地址了 R3 上响应 R1 的数据包时, 数据包是发向 192.168.12.1 的, 然而由于 R3 没有 192.168.12.0/24 的路由, 数据包无法发送 即 : 数据包从 R1 到了 R3 后, 无法返回 R1 1. 实验目的 通过本实验, 读者可以掌握如下技能 : (1) 默认路由的使用场合 (2) 默认路由的配置 2. 实验拓扑 如图 3-3 3. 实验步骤 3.3 实验 2: 默认路由 在实验 1 的基础上进行实验 2 (1) 步骤 1:R1 R3 上删除原有静态路由 R1(config)#no ip route 2.2.2.0 255.255.255.0 Serial0/0/0 // 要删除路由, 在原有命令前面加 no 即可 R1(config)#no ip route 3.3.3.0 255.255.255.0 192.168.12.2 R3(config)#no ip route 1.1.1.0 255.255.255.0 Serial0/0/1 R3(config)#no ip route 2.2.2.0 255.255.255.0 Serial0/0/1 (2) 步骤 2: R1 R3 上配置默认路由 R1(config)#ip route 0.0.0.0 0.0.0.0 s0/0/0 R3(config)#ip route 0.0.0.0 0.0.0.0 s0/0/1 4. 实验调试从各路由器的环回口 ping 其他路由器的环回口 请读者比较两个实验 ping 的结果,
仔细分析原因 3.4 实验 3:ip classless 1. 实验目的 通过本实验, 读者可以掌握如下技能 : (1) ip classless 命令的含义 (2) 配置 ip classless 2. 实验拓扑 3. 实验步骤 图 3-4 实验 3 拓扑图 (1) 步骤 1: 执行 ip classless R1(config)#interface Loopback0 R1(config-if)#ip address 10.1.1.1 255.255.255.0 R1(config)#interface Serial0/0/0 R1(config-if)#no shutdown R1(config-if)#ip address 192.168.12.1 255.255.255.0 R1(config)#ip classless R1(config)#ip route 0.0.0.0 0.0.0.0 Serial0/0/0 // 以上我们配置了默认路由 ; 同时打开 ip classless, 默认就是打开的 R2(config)#interface Loopback0 R2(config-if)#ip address 10.2.2.2 255.255.255.0 R2(config)#interface Serial0/0/0 R1(config-if)#no shutdown R2(config-if)#ip address 192.168.12.2 255.255.255.0 R2(config-if)#clock rate 128000 R1(config)#ip classless R2(config)#ip route 10.1.1.0 255.255.255.0 Serial0/0/0 测试从 R1 ping R2 的 loopback0 接口 R1#ping 10.2.2.2 Sending 5, 100-byte ICMP Echos to 10.2.2.2, timeout is 2 seconds:!!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 12/13/16 ms 可以 ping 通
(2) 步骤 2: 执行 no ip classless R1(config)#no ip cef // 关闭 ip cef, 防止影响我们的测试,CEF R1(config)#no ip classless R1#ping 10.2.2.2 Sending 5, 100-byte ICMP Echos to 10.2.2.2, timeout is 2 seconds:... Success rate is 0 percent (0/5) 可以看到 R1 虽然存在默认路由, 也不能可以 ping 通 R2 的 loopback0 接口 3.5 本章小结 本章介绍了静态路由的配置方法, 命令虽然简单, 但是静态路由的配置却有一定的技巧性 我们也介绍了路由表的查看方法以及路由表中各字段的含义 默认路由常常用于桩网络中, 处于局域网和 Internet 边界的路由器通常会使用它 表 3-2 是本章出现的命令 表 3-2 本章命令汇总 命令 作用 ip route 配置静态路由 show ip route 查看路由表 ip classless/ no ip classless 打开 / 关闭有类路由功能 ping 2.2.2.2 source loopback 0 指定源端口进行 ping 测试