





一.拓扑图
二。分别给路由器和PC配置地址
第一步:分别给路由器配置IP
R0:
R1:
R4:
R3:
第二步:添加静态路由使路由器实现互通
R0:
R1:
R4:
R3:
注:
· show ip route 查看路由器路由表的内容。
·例如:R0(config)#ip route 192.168.2.0 255.255.255.0 fa0/1 注意不要漏掉 fa0/0
·哪个路由器和哪个路由器没有直接相连就配置静态路由就好(用自己话说的,可能不太标准。)第三步:给PC配置IP
第四步:验证
发现PC可以ping通任何一个路由器的地址
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 补充一下,有的时候是只有四台路由器并没有PC机的话,只需要把上边的实验稍作修改即可。
(1)把有关PC的ip地址(192.18.0.10)去掉即可。在添加静态路由的时候也不用添加。
(2)可以在路由器上ping。
(3)测试效果(我是在R0上ping的,可以在任意一台路由器上ping,发现是都可以ping通的。)
1.配置静态路由
路由器可以将不同网段之间的网络连接到一起,当路由器接收到数据包后要查看数据包中的目标IP,再检查自己的路由表,如果路由表中有和目标IP相匹配的路由条目,路由器才能将数据包按照该路由条目所指定的端口转发出去,实现不同网络之间的通信,那么路由器中的路由表是如何实现的呢?配置了路由器接口IP并为UP状态路由表中自动生成直连路由,对于非直连的路由,需通过静态路由管理员手工添加或通过配置动态路自动学习。
配置静态路由
配置浮动静态路由
配置缺省路由
网络环境及IP地址规划
图-1
本例中的配置练习采用思科模拟器 —— Cisco Packet Tracer 6.0,Route采用2621路由器实现。
实现此案例需要按照如下步骤进行。
R1上配置接口IP
> R1(config)#interface fastEthernet 0/0 R1(config-if)#ip address 192.168.1.254 255.255.255.0 > R1(config-if)#no shutdown > R1(config-if)#exit > R1(config)#interface fastEthernet 0/1 > R1(config-if)#ip address 192.168.2.1 255.255.255.0 > R1(config-if)#no shutdown
R2上配置接口IP
> R2(config)#interface fastEthernet 0/1 R2(config-if)#ip address192.168.2.2 255.255.255.0 > R2(config-if)#no shutdown > R2config-if)#exit > R2(config)#interface fastEthernet 0/0 R2(config-if)#ip addres 192.168.3.254 255.255.255.0 > R2(config-if)#no shutdown
R1上添加静态路由
> R1(config)#ip route 192.168.3.0 255.255.255.0 192.168.2.2
R1上查看路由表
> 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.1.0/24 is directly connected, FastEthernet0/0 > C 192.168.2.0/24 is directly connected, FastEthernet0/1 > S 192.168.3.0/24 [1/0] via 192.168.2.2 //S表示静态路由
R2上添加静态路由
> R2(config)#ip route 192.168.1.0 255.255.255.0 192.168.2.1
R2上查看路由条目
> 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 S 192.168.1.0/24 [1/0] via 192.168.2.1 //S表示静态路由 C 192.168.2.0/24 is directly connected, FastEthernet0/1 C 192.168.3.0/24 is directly connected, FastEthernet0/0
配置PC1的IP地址为192.168.1.1,网关为192.168.1.254
配置PC2的IP地址为192.168.3.1,网关为192.168.3.254测试网络连通性,PC1 ping 192.168.3.1
> PC>ping 192.168.3.1 Pinging 192.168.3.1 with 32 bytes of data: Reply from 192.168.3.1: bytes=32 time=1ms TTL=126 Reply from 192.168.3.1:bytes=32 time=11ms TTL=126 Reply from 192.168.3.1: bytes=32 time=10msTTL=126 Reply from 192.168.3.1: bytes=32 time=11ms TTL=126 > Ping statistics for 192.168.3.1: > Packets: Sent = 4, Received = 4, Lost = 0 (0% loss) > Approximate round trip times in milli-seconds: > Minimum = 1ms, Maximum = 11ms, Average = 8ms
配置浮动静态路由
1)在以上静态路由实验的基础上,先分别进入R1与R2的特权模式输入write命令保存配置信息,然后分别进入R1与R2的物理配置界面,点击开关按钮关闭路由器,添加NM-1FE-TX模块并再次点击开关按钮,2)添加模块后将R1的F1/0接口连接到R2的F1/0接口修改拓扑
3 配置R1的F1/0接口IP
> R1(config)#interface fastEthernet 1/0 R1(config-if)#ip address 192.168.4.1 255.255.255.0 > R1(config-if)#no shutdown
4 配置R2的F1/0接口IP
> R2(config)#interface fastEthernet 1/0 R2(config-if)#ip address 192.168.4.2 255.255.255.0 > R2(config-if)#no shutdown
5 R1上添加静态浮动路由
> R1(config)#ip route 192.168.3.0 255.255.255.0 192.168.4.2 50
6 R2上添加静态浮动路由
> R2(config)#ip route 192.168.1.0 255.255.255.0 192.168.4.1 50
7 R1上查看路由表
> 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.1.0/24 is directly connected, FastEthernet0/0 C > 192.168.2.0/24 is directly connected, FastEthernet0/1 S > 192.168.3.0/24 [1/0] via 192.168.2.2 C > 192.168.4.0/24 is directly connected, FastEthernet1/0
> R1#show ip rou > 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.1.0/24 is directly connected, FastEthernet0/0 C > 192.168.2.0/24 is directly connected, FastEthernet0/1 S > 192.168.3.0/24 [1/0] via 192.168.2.2 //只有下一跳为192.168.2.2的静态路由 C > 192.168.4.0/24 is directly connected, FastEthernet1/0
8 禁用F/01接口
> R1(config)#interface fastEthernet 0/1 > R1(config-if)#shutdown
9 R1上查看路由表
> C 192.168.1.0/24 is directly connected, FastEthernet0/0 > S 192.168.3.0/24 [50/0] via 192.168.4.2//下一跳接口为192.168.4.2的路由生效 > C 192.168.4.0/24 is directly connected, FastEthernet1/0
10 测试网络连通性,PC1 ping 192.168.3.1
> PC>ping 192.168.3.1 > Pinging 192.168.3.1 with 32 bytes of data: > Reply from 192.168.3.1: bytes=32 time=0ms TTL=126 > Reply from 192.168.3.1: bytes=32 time=10ms TTL=126 > Reply from 192.168.3.1: bytes=32 time=11ms TTL=126 > Reply from 192.168.3.1: bytes=32 time=1ms TTL=126 > Ping statistics for 192.168.3.1: > Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), > Approximate round trip times in milli-seconds: > Minimum = 0ms, Maximum = 11ms, Average = 5ms
配置缺省路由
1 网络环境及IP地址规划,如图-4所示
2 R1上配置接口IP
> Router(config)#interface fastEthernet 0/0 > R1(config-if)#ip address192.168.1.254 255.255.255.0 > R1(config-if)#no shutdown > R1(config-if)#exit > R1(config)#interface fastEthernet 0/1 > R1(config-if)#ip address 192.168.2.1 255.255.255.0 > R1(config-if)#no shutdown
3 R2上配置接口IP
> R2(config)#interface f0/1 > R2(config-if)#ip address 192.168.2.2 255.255.255.0 > R2(config-if)#no shutdown > R2(config-if)#exit > R2(config)#interface fastEthernet 0/0 > R2(config-if)#ip addres 192.168.3.1 255.255.255.0 > R2(config-if)#no shutdown
4 R3上配置接口IP
R3(config)#interface fastEthernet 0/1 R3(config-if)#ip address 192.168.3.2 255.255.255.0 R3(config-if)#no shutdown R3(config-if)#exit R3(config)#interface fastEthernet 0/0 R3(config-if)#ip address 192.168.4.254 255.255.255.0 R3(config-if)#no shutdown
5 R1、R2、R3上分别添加静态路由
R1(config)#ip route 192.168.3.0 255.255.255.0 192.168.2.2 R1(config)#ip route 192.168.4.0 255.255.255.0 192.168.2.2 R2(config)#ip route 192.168.1.0 255.255.255.0 192.168.2.1 R2(config)#ip route 192.168.4.0 255.255.255.0 192.168.3.2 R3(config)#ip route 192.168.1.0 255.255.255.0 192.168.3.1 R3(config)#ip route 192.168.2.0 255.255.255.0 192.168.3.1
6 R1上查看路由表
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.1.0/24 is directly connected, FastEthernet0/0 C 192.168.2.0/24 is directly connected, FastEthernet0/1 S 192.168.3.0/24 [1/0] via 192.168.2.2 //静态路由 S 192.168.4.0/24 [1/0] via 192.168.2.2 //静态路由
7 R2上查看路由表
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 S 192.168.1.0/24 [1/0] via 192.168.2.1 //静态路由 C 192.168.2.0/24 is directly connected, FastEthernet0/1 C 192.168.3.0/24 is directly connected, FastEthernet0/0 S 192.168.4.0/24 [1/0] via 192.168.3.2 //静态路由
8 R3上查看路由表
R3#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 S 192.168.1.0/24 [1/0] via 192.168.3.1 //静态路由 S 192.168.2.0/24 [1/0] via 192.168.3.1 //静态路由 C 192.168.3.0/24 is directly connected, FastEthernet0/1 C 192.168.4.0/24 is directly connected, FastEthernet0/0
9 配置PC的IP地址
这个配置不用命令只需要自己点开电脑软件里的pc机直接配置就好
10 测试网络连通性,PC1 ping 192.168.2.2、192.168.3.1、192.168.3.2、192.168.4.1
PC>ping ping 192.168.2.2、192.168.3.1、192.168.3.2、192.168.4.1 以此类推ping下去如果都是以下命令就都通了Reply from 192.168.2.2: bytes=32 time=0ms TTL=254
11 R1、R3取消静态路由
R1(config)#no ip route 192.168.3.0 255.255.255.0 192.168.2.2 R1(config)#no ip route 192.168.4.0 255.255.255.0 192.168.2.2 R3(config)#no ip route 192.168.1.0 255.255.255.0 192.168.3.1 R3(config)#no ip route 192.168.2.0 255.255.255.0 192.168.3.1
12 R1、R3添加默认路由
R1(config)#ip route 0.0.0.0 0.0.0.0 192.168.2.2 R3(config)#ip route 0.0.0.0 0.0.0.0 192.168.3.1
13 最后分别查看一下R1的路由表和R3的路由表、在测试一下网络连通性没有问题就可以了
最近公司好多员工自己私接路由,由于员工配置的路由全是192.168.1.0段的最终导致了部分人无法连接打印机同时还产生了ARP风暴造成了大面积人通过无线无法连接打印机,思考了一下这是一个典型的基于串联双路由的模式配置,所以要想实现了不同网段访问共享打印机,只能在路由上设置静态路由节点来实现不同网段之间的互通,接下来引用了红黑联盟图片以及自己的个人理解来说明一下。
静态路由设置实例解析随着宽带接入的普及,很多家庭和小企业都组建了局域网来共享宽带接入。而且随着局域网规模的扩大,很多地方都涉及到2台或以上路由器的应用。当一个局域网内存在2台以上的路由器时,由于其下主机互访的需求,往往需要设置路由。由于网络规模较小且不经常变动,所以静态路由是最合适的选择。本文作为一篇初级入门类文章,会以几个简单实例讲解静态路由,并在最后讲解一点关于路由汇总(归纳)的知识。由于这类家庭和小型办公局域网所采用的一般都是中低档宽带路由器,所以这篇文章就以最简单的宽带路由器为例。(其实无论在什么档次的路由器上,除了配置方式和命令不同,其配置静态路由的原理是不会有差别的。)常见的1WAN口、4LAN口宽带路由器可以看作是一个最简单的双以太口路由器+一个4口小交换机,其WAN口接外网,LAN口接内网以做区分。路由就是把信息从源传输到目的地的行为。形象一点来说,信息包好比是一个要去某地点的人,路由就是这个人选择路径的过程。而路由表就像一张地图,标记着各种路线,信息包就依靠路由表中的路线指引来到达目的地,路由条目就好像是路标。在大多数宽带路由器中,未配置静态路由的情况下,内部就存在一条默认路由,这条路由将LAN口下所有目的地不在自己局域网之内的信息包转发到WAN口的网关去。宽带路由器只需要进行简单的WAN口参数的配置,内网的主机就能访问外网,就是这条路由在起作用。本文将分两个部分,第一部分讲解静态路由的设置应用,第二部分讲解关于路由归纳的方法和作用。下面就以地瓜这个网络初学者遇到的几个典型应用为例,让高手大虾来说明一下什么情况需要设置静态路由,静态路由条目的组成,以及静态路由的具体作用。例一:最简单的串连式双路由器型环境这种情况多出现于中小企业在原有的路由器共享Internet的网络中,由于扩展的需要,再接入一台路由器以连接另一个新加入的网段。而家庭中也很可能出现这种情况,如用一台宽带路由器共享宽带后,又加入了一台无线路由器满足无线客户端的接入。地瓜:公司里原有一个局域网LAN 1,靠一台路由器共享Internet,现在又在其中添加了一台路由器,下挂另一个网段LAN 2的主机。经过简单设置后,发现所有主机共享Internet没有问题,但是LAN 1的主机无法与LAN 2的主机通信,而LAN 2的主机却能Ping通LAN 1下的主机。这是怎么回事?大虾:这是因为路由器隔绝广播,划分了广播域,此时LAN 1和LAN 2的主机位于两个不同的网段中,中间被新加入的路由器隔离了。所以此时LAN 1下的主机不能“看”到LAN 1里的主机,只能将信息包先发送到默认网关,而此时的网关没有设置到LAN 2的路由,无法做有效的转发。这种情况下,必须要设置静态路由条目。此种网络环境的拓扑示意如下:(注:图中省略了可能存在的交换层设备)如图一所示,LAN 1为192.168.0.0这个标准C类网段,路由器R1为原有路由器,它的WAN口接入宽带,LAN口(IP为192.168.0.1)挂着192.168.0.0网段(子网掩码255.255.255.0的C类网)主机和路由器R2(新添加)的WAN口(IP为192.168.0.100)。R2的LAN口(IP为192.168.1.1)下挂着新加入的LAN 2这个192.168.1.0的C类不同网段的主机。 www.jb51.net如果按照共享Internet的方式简单设置,此时应将192.168.0.0的主机网关都指向R1的LAN口(192.168.0.1),192.168.1.0网段的主机网关指向R2的LAN口(192.168.1.1),那么只要R2的WAN口网关指向192.168.0.1,192.168.1.0的主机就都能访问192.168.0.0网段的主机并能通过宽带连接上网。这是因为前面所说的宽带路由器中一条默认路由在起作用,它将所有非本网段的目的IP包都发到WAN口的网关(即路由器R1),再由R1来决定信息包应该转发到它自己连的内网还是发到外网去。但是192.168.0.0网段的主机网关肯定要指向192.168.0.1,而R1这时并不知道192.168.1.0这个LAN 2的正确位置,那么此时只能上网以及本网段内的互访,不能访问到192.168.1.0网段的主机。这时就需要在R1上指定一条静态路由,使目的IP为192.168.1.0网段的信息包能转发到路由器R2去。一条静态路由条目一般由3部分组成:1.目的IP地址或者叫信宿网络、子网;2.子网掩码;3.网关或叫下一跳。例一中R1上设定的静态路由条目就应该为:目的IP地址192.168.1.0(代表1.x这个网段),子网掩码255.255.255.0(因为是C类网段),下一跳192.168.0.100。如图2,此图为TP-LINK R410中的静态路由表配置项,保存后即可生效。如果是Cisco的路由器,则在全局配置模式下键入命令:Router(config)# ip route 192.168.1.0 255.255.255.0 192.168.0.100。注意:其中的网关IP必须是与WAN或LAN口属于同一个网段。那条默认路由写出来就是:目的IP为 0.0.0.0,子网掩码0.0.0.0,下一跳为WAN口上的默认网关,有时我们也称它为“8个0的默认路由”。另外,如果目的IP是一个具体的主机IP(如192.168.1.2),那么路由条目应为:目的IP 192.168.1.2,子网掩码255.255.255.255,下一跳或网关192.168.0.100。使用此种连接方式,还可以方便的使用路由器内置的访问控制列表来设置LAN 2下主机的访问权限,这对企业用户而言还是很方便的。宽带路由器中的“防火墙设置”其实就是一个简化的访问控制列表,即ACL- Access Control Lists。如:希望局域网LAN 2中IP地址为192.168.1.7的计算机不能收发邮件,IP地址为192.168.1.8的计算机不能访问企业内部位于LAN 1的ERP服务器(假设其IP为192.168.0.10),对局域网中的其它计算机则不做任何限制,这时您需要指定如下的数据包过滤表,如图:图3例二:两台平级并连的路由器,下挂子网中主机需要互相通信的环境这种情况,两台平行并连的路由器上层应该还有一个总的出口网关,而这个网关有可能因某种原因不便设置路由,而此时网络中存在3个不同的网段。地瓜:我家是小区共享型的宽带接入,我自己用一台宽带路由器构建了一个家庭局域网以共享Internet,正好邻居也跟我一样用宽带路由器构建了另一个家庭局域网。而我们各自局域网内的主机之间却不能互相通信,根本ping不通,这是怎么回事?大虾:这种情况下整个小区其实就是一个大的局域网,主机不能互通的原因,其实跟例一中LAN 1不能ping通LAN 2的原因一样,都是因为上层的默认网关不知道目的IP所属网段的正确位置,无法做有效转发所致。这种环境的典型示意图如下:图中内网网关就是小区的网关,R1和R3分别为两户的宽带路由器,它们之间一般通过楼层的接入交换机和小区的骨干交换机连接在一起,此图省略了这一部分。图4的这种情况,只要在网关设备上按例一的方式添加两条路由就能实现两个子网中主机的互访,而且其 10.0.0.0这个A类网段中存在的主机也都能通过这两条路由访问到R1和R3下的内网机。但是如果是小区的网关设备,那肯定是不会让用户随便配置路由条目的,而且你应该也不想小区内的所有用户都能直接访问到你的内网主机。这时,我们可以在R1和R3上各添加一条路由指向对方来实现R1和R3下主机直接互访的效果。在R1上:目的IP地址172.16.0.0,子网掩码255.255.0.0(B类网段),下一跳10.1.1.3。在R3上:目的IP地址192.168.0.0,子网掩码255.255.255.0(C类网段),下一跳10.1.1.2。注:有些新型小区中使用了P-VLAN技术,这种网络的情况比较复杂,这样上面简单的静态路由设置有可能无法达到目的。例三:既串且并,网络中有多级路由设备的环境。这种情况可以说是例一和例二两种应用的整合和延伸,看似复杂其实简单。地瓜:如果像例二中那样的环境中,我家里的局域网再添置一个路由器,下挂另一个网段以做扩展,那要怎么设置呢?大虾:你说的这种网络结构,确实就是将例一和例二合在一起了。这时一共有4个网段并存,我们的设置是要让两户家庭局域网下的3个子网内主机能够互通,而此时小区的网关当然还是不能去设置的。其拓扑示意图如下:可以看到图5就是将图1和图4整合在一起了。既然拓扑图是例一、例二的结合,那将例一、例二中的路由条目加在一起是不是就可以了呢?当然也不是这么简单,如果只是配置了前两例的路由条目,R3下的主机是无法直接访问到R2下的192.168.1.0这个子网的。所以在R3上还要加一条到192.168.1.0这个子网的路由。静态路由条目配置如下:R1:目的IP地址192.168.1.0,子网掩码255.255.255.0,下一跳192.168.0.100。目的IP地址172.16.0.0,子网掩码255.255.0.0,下一跳10.1.1.3。R3:目的IP地址192.168.0.0,子网掩码255.255.255.0,下一跳10.1.1.2。目的IP地址192.168.1.0,子网掩码255.255.255.0,下一跳10.1.1.2。地瓜:为何R3中第二条路由的下一跳不是直接指向R2,而是也指向R1呢?大虾:就知道你会问这个,这个问题要从路由器间通信的原理来讲解。路由器是通过ARP解析协议来获得下一跳路由器的MAC地址,而ARP基于广播,在一般情况下路由器是不会转发广播,也就是广播包无法过路由。所以对于路由器R3来讲,R1和R3才是同等级的,它只能看到R1,不能看到R2,这就是为何在例一的注意中提到:“其中的网关IP必须是与WAN或LAN口属于同一个网段”的原因。而文中所说的静态路由条目组成的第3部分:网关又叫下一跳,而不叫下两跳、下三跳也是这个意思。总之,在一般情况下,下一跳路由的IP地址肯定要跟这个路由器的某个接口是在同一个网段的。本篇文章下面的部分将讲解关于路由汇总(或叫路由归纳)的知识。上面例三中R3上的静态路由条目,其实可以写成一条:目的IP地址192.168.0.0,子网掩码255.255.0.0(不再是C类子网的掩码),下一跳 10.1.1.2。这时192.168.0.0,掩码255.255.0.0这个网段不能称为C类或B类的子网了,由于它超过了本身C类网段的范围,所以可以称它是一个超网。这个网段包含了192.168.0.0~192.168.255.0所有的子网。也就是说,这条静态路由会使所有目的IP在这个范围内的信息包,都发给10.1.1.2的路由器R1。将多条子路由条目汇总成一条都包含其内的总路由条目,这就是路由汇总或叫路由归纳。路由器在检查计算路由时是比较消耗资源的,路由条目越多,路由表越长,则这个过程耗时越多,所以通过路由汇总减少路由表的长度,对提高路由器工作效率是很有帮助的。虽然在举例中的这种只有几个路由器的小网络中起到的作用有限,但是如果是几十、几百甚至上千、上万个路由器的大型网络中,路由归纳起到的作用就非常明显了,可以说不使用路由归纳是不可想象的。例四:可能细心的读者已经发现,例三最后的那条归纳路由虽然包含了R1下所有的两个子网(192.168.0.0和192.168.1.0),但是也包含了R1下实际上并不存在的一些子网(192.168.2.0~192.168.255.0)。如果在整个局域网中别的路由器下还存在这些子网(如图6,R4下存在192.168.2.0子网),那么路由就会出错了,所以这条汇总路由是一条不精确的汇总。我们都知道IPv4的地址是由4段8位的二进制数组成,一部分是网络位,一部分是主机位。其对应的子网掩码网络位部分就是全1的二进制数,而主机位就是全0的二进制数。每个信息包在过路由器时会检查其目的IP,和路由表中路由条目的子网掩码做“与”运算,并与路由条目中目的IP进行比对,相同的就按照这条路由规则转发,不相同的就再检查比对下一条。可以看出我们做的汇总路由的操作,就是将多条路由条目中目的IP相同的网络位提取出来写成一条。而例三中的汇总路由之所以不精确就是因为相同部分未能全部提出来。如例三中,R3上的第一条:目的IP为192.168.0.0;第二条:目的IP为192.168.1.0。我们只提取了前面的两段192.168,而后面的第三段网络位中还是有相同的部分的。192.168.0.0中第三段写成二进制数为00000000(8位0),182.168.1.0中第三段写成二进制数为00000001(7位0,1位1),那么它们的前7位是相同的,在对应的子网掩码位置上就应该是11111110(7位1,1位0),合成十进制为254。所以这条汇总路由应该写成:目的IP为192.168.0.0,子网掩码255.255.254.0,下一跳10.1.1.2。这样,这条汇总路由只包含192.168.0.0和192.168.1.0两个子网,是一条精确的汇总路由。如图6中,R3下172.16.0.0的主机发送到192.168.2.0网段的信息包,其第三段网络位写成二进制为00000010(前6位0),就不包含在这条精确的汇总路由内了。这时我们在R3上静态路由条目应该为:1.目的IP地址192.168.0.0,子网掩码255.255.254.0,下一跳10.1.1.2。2.目的IP地址192.168.2.0,子网掩码255.255.255.0,下一跳10.1.1.4。我们在进行路由汇总时应该尽量使用精确的汇总条目,本着能汇总的条目就汇总,不能精确汇总的条目就不汇总的原则。这样在网络以后的扩展和变动时能更有条理的增改路由表,减少出错的几率。总结:静态路由因为其设置简单明了,在不常变动的网络中稳定性好,排错也相对容易,所以在中小企业甚至一些大型的园区网中也都使用静态路由,它在实际应用中是很常见的,属于网络工作人员必会的基础知识。如文中所述,静态路由的设置原理是比较简单的,但可以说它是学习各种路由协议的基础,属于学习路由知识时必学的部分。另外,在越复杂越大的网络中,汇总路由的效果就越显著,而能不能进行有效的路由汇总、汇总的效率如何,都跟网络结构中IP地址网段的分布有密切关系。IP地址的部署越连续而有条理,则路由汇总越容易也越有效,所以我们在部署网络时应该重视体系化编址。(注:在子网环境中,当网络地址是以2的指数形式的连续区块时,路由归纳是最有效的。)最后也说几句希望大家多多关注我的个人博客,共同进步在linux的海洋中翱翔!!
配置静态路由
路由器可以将不同网段之间的网络连接到一起,当路由器接收到数据包后要查看数据包中的目标IP,再检查自己的路由表,如果路由表中有和目标IP相匹配的路由条目,路由器才能将数据包按照该路由条目所指定的端口转发出去,实现不同网络之间的通信,那么路由器中的路由表是如何实现的呢?配置了路由器接口IP并为UP状态路由表中自动生成直连路由,对于非直连的路由,需通过静态路由管理员手工添加或通过配置动态路自动学习。
配置静态路由
配置浮动静态路由
配置缺省路由
网络环境及IP地址规划,如图-1所示。
图-1
本例中的配置练习采用思科模拟器 —— Cisco Packet Tracer 6.0,Route采用2621路由器实现。
步骤
实现此案例需要按照如下步骤进行。步骤一:配置静态路由
1)R1上配置接口IP
R1(config)#interface fastEthernet 0/0 R1(config-if)#ip address 192.168.1.254 255.255.255.0 R1(config-if)#no shutdown R1(config-if)#exit R1(config)#interface fastEthernet 0/1 R1(config-if)#ip address 192.168.2.1 255.255.255.0 R1(config-if)#no shutdown
2)R2上配置接口IP
R2(config)#interface fastEthernet 0/1 R2(config-if)#ip address 192.168.2.2 255.255.255.0 R2(config-if)#no shutdown R2config-if)#exit R2(config)#interface fastEthernet 0/0 R2(config-if)#ip address 192.168.3.254 255.255.255.0 R2(config-if)#no shutdown
3)R1上添加静态路由
R1(config)#ip route 192.168.3.0 255.255.255.0 192.168.2.2
4)R1上查看路由表
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.1.0/24 is directly connected, FastEthernet0/0 C 192.168.2.0/24 is directly connected, FastEthernet0/1 S 192.168.3.0/24 [1/0] via 192.168.2.2 //S表示静态路由
5)R2上添加静态路由
R2(config)#ip route 192.168.1.0 255.255.255.0 192.168.2.1
6)R2上查看路由条目
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 S 192.168.1.0/24 [1/0] via 192.168.2.1 //S表示静态路由 C 192.168.2.0/24 is directly connected, FastEthernet0/1 C 192.168.3.0/24 is directly connected, FastEthernet0/0
7)配置PC1的IP地址为192.168.1.1,网关为192.168.1.254
8)配置PC2的IP地址为192.168.3.1,网关为192.168.3.254
9)测试网络连通性,PC1 ping 192.168.3.1PC>ping 192.168.3.1 Pinging 192.168.3.1 with 32 bytes of data: Reply from 192.168.3.1: bytes=32 time=1ms TTL=126 Reply from 192.168.3.1: bytes=32 time=11ms TTL=126 Reply from 192.168.3.1: bytes=32 time=10ms TTL=126 Reply from 192.168.3.1: bytes=32 time=11ms TTL=126 Ping statistics for 192.168.3.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 1ms, Maximum = 11ms, Average = 8ms
步骤二:配置浮动静态路由
1)在以上静态路由实验的基础上,先分别进入R1与R2的特权模式输入write命令保存配置信息,然后分别进入R1与R2的物理配置界面,点击开关按钮关闭路由器,添加NM-1FE-TX模块并再次点击开关按钮,如下图-2所示。
图-2
2)添加模块后将R1的F1/0接口连接到R2的F1/0接口修改拓扑如下图-3所示:
图-3
3)配置R1的F1/0接口IPR1(config)#interface fastEthernet 1/0 R1(config-if)#ip address 192.168.4.1 255.255.255.0 R1(config-if)#no shutdown
4)配置R2的F1/0接口IP
R2(config)#interface fastEthernet 1/0 R2(config-if)#ip address 192.168.4.2 255.255.255.0 R2(config-if)#no shutdown
5)R1上添加静态浮动路由
R1(config)#ip route 192.168.3.0 255.255.255.0 192.168.4.2 50 //管理距离50
6)R2上添加静态浮动路由
R2(config)#ip route 192.168.1.0 255.255.255.0 192.168.4.1 50 //管理距离50
7)R1上查看路由表
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.1.0/24 is directly connected, FastEthernet0/0 C 192.168.2.0/24 is directly connected, FastEthernet0/1 S 192.168.3.0/24 [1/0] via 192.168.2.2 C 192.168.4.0/24 is directly connected, FastEthernet1/0 R1#show ip rou 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.1.0/24 is directly connected, FastEthernet0/0 C 192.168.2.0/24 is directly connected, FastEthernet0/1 S 192.168.3.0/24 [1/0] via 192.168.2.2 //只有下一跳为192.168.2.2的静态路由 C 192.168.4.0/24 is directly connected, FastEthernet1/0
8)禁用F/01接口
R1(config)#interface fastEthernet 0/1 R1(config-if)#shutdown
9)R1上查看路由表
C 192.168.1.0/24 is directly connected, FastEthernet0/0 S 192.168.3.0/24 [50/0] via 192.168.4.2//下一跳接口为192.168.4.2的路由生效 C 192.168.4.0/24 is directly connected, FastEthernet1/0
10)测试网络连通性,PC1 ping 192.168.3.1
PC>ping 192.168.3.1 Pinging 192.168.3.1 with 32 bytes of data: Reply from 192.168.3.1: bytes=32 time=0ms TTL=126 Reply from 192.168.3.1: bytes=32 time=10ms TTL=126 Reply from 192.168.3.1: bytes=32 time=11ms TTL=126 Reply from 192.168.3.1: bytes=32 time=1ms TTL=126 Ping statistics for 192.168.3.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 11ms, Average = 5ms
步骤三:配置缺省路由
1)网络环境及IP地址规划,如图-4所示
图-4
2)R1上配置接口IPRouter(config)#interface fastEthernet 0/0 R1(config-if)#ip address 192.168.1.254 255.255.255.0 R1(config-if)#no shutdown R1(config-if)#exit R1(config)#interface fastEthernet 0/1 R1(config-if)#ip address 192.168.2.1 255.255.255.0 R1(config-if)#no shutdown
3)R2上配置接口IP
R2(config)#interface f0/1 R2(config-if)#ip address 192.168.2.2 255.255.255.0 R2(config-if)#no shutdown R2(config-if)#exit R2(config)#interface fastEthernet 0/0 R2(config-if)#ip address 192.168.3.1 255.255.255.0 R2(config-if)#no shutdown 4)R3上配置接口IP R3(config)#interface fastEthernet 0/1 R3(config-if)#ip address 192.168.3.2 255.255.255.0 R3(config-if)#no shutdown R3(config-if)#exit R3(config)#interface fastEthernet 0/0 R3(config-if)#ip address 192.168.4.254 255.255.255.0 R3(config-if)#no shutdown
5)R1、R2、R3上分别添加静态路由
R1(config)#ip route 192.168.3.0 255.255.255.0 192.168.2.2 R1(config)#ip route 192.168.4.0 255.255.255.0 192.168.2.2 R2(config)#ip route 192.168.1.0 255.255.255.0 192.168.2.1 R2(config)#ip route 192.168.4.0 255.255.255.0 192.168.3.2 R3(config)#ip route 192.168.1.0 255.255.255.0 192.168.3.1 R3(config)#ip route 192.168.2.0 255.255.255.0 192.168.3.1
6)R1上查看路由表
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.1.0/24 is directly connected, FastEthernet0/0 C 192.168.2.0/24 is directly connected, FastEthernet0/1 S 192.168.3.0/24 [1/0] via 192.168.2.2 //静态路由 S 192.168.4.0/24 [1/0] via 192.168.2.2 //静态路由
7)R2上查看路由表
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 S 192.168.1.0/24 [1/0] via 192.168.2.1 //静态路由 C 192.168.2.0/24 is directly connected, FastEthernet0/1 C 192.168.3.0/24 is directly connected, FastEthernet0/0 S 192.168.4.0/24 [1/0] via 192.168.3.2 //静态路由
8)R3上查看路由表
R3#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 S 192.168.1.0/24 [1/0] via 192.168.3.1 //静态路由 S 192.168.2.0/24 [1/0] via 192.168.3.1 //静态路由 C 192.168.3.0/24 is directly connected, FastEthernet0/1 C 192.168.4.0/24 is directly connected, FastEthernet0/0
9)按图-4配置PC的IP地址
10)测试网络连通性,PC1 ping 192.168.2.2、192.168.3.1、192.168.3.2、192.168.4.1PC>ping 192.168.2.2 //ping 192.168.2.2 Pinging 192.168.2.2 with 32 bytes of data: Reply from 192.168.2.2: bytes=32 time=0ms TTL=254 Reply from 192.168.2.2: bytes=32 time=0ms TTL=254 Reply from 192.168.2.2: bytes=32 time=0ms TTL=254 Reply from 192.168.2.2: bytes=32 time=0ms TTL=254 Ping statistics for 192.168.2.2: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms PC>ping 192.168.3.1 //ping 192.168.3.1 Pinging 192.168.3.1 with 32 bytes of data: Reply from 192.168.3.1: bytes=32 time=0ms TTL=254 Reply from 192.168.3.1: bytes=32 time=3ms TTL=254 Reply from 192.168.3.1: bytes=32 time=0ms TTL=254 Reply from 192.168.3.1: bytes=32 time=0ms TTL=254 Ping statistics for 192.168.3.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 3ms, Average = 0ms PC>ping 192.168.3.2 //ping 192.168.3.2 Pinging 192.168.3.2 with 32 bytes of data: Reply from 192.168.3.2: bytes=32 time=0ms TTL=253 Reply from 192.168.3.2: bytes=32 time=12ms TTL=253 Reply from 192.168.3.2: bytes=32 time=0ms TTL=253 Reply from 192.168.3.2: bytes=32 time=12ms TTL=253 Ping statistics for 192.168.3.2: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 12ms, Average = 6ms PC>ping 192.168.4.1 //ping 192.168.4.1 Pinging 192.168.4.1 with 32 bytes of data: Reply from 192.168.4.1: bytes=32 time=0ms TTL=125 Reply from 192.168.4.1: bytes=32 time=10ms TTL=125 Reply from 192.168.4.1: bytes=32 time=0ms TTL=125 Reply from 192.168.4.1: bytes=32 time=22ms TTL=125 Ping statistics for 192.168.4.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 22ms, Average = 8ms
11)R1、R3取消静态路由
R1(config)#no ip route 192.168.3.0 255.255.255.0 192.168.2.2 R1(config)#no ip route 192.168.4.0 255.255.255.0 192.168.2.2 R3(config)#no ip route 192.168.1.0 255.255.255.0 192.168.3.1 R3(config)#no ip route 192.168.2.0 255.255.255.0 192.168.3.1 12)R1、R3添加默认路由 R1(config)#ip route 0.0.0.0 0.0.0.0 192.168.2.2 R3(config)#ip route 0.0.0.0 0.0.0.0 192.168.3.1
13)R1上查看路由表
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 192.168.2.2 to network 0.0.0.0 C 192.168.1.0/24 is directly connected, FastEthernet0/0 C 192.168.2.0/24 is directly connected, FastEthernet0/1 S* 0.0.0.0/0 [1/0] via 192.168.2.2 //默认路由
14)R3上查看路由表
R3#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 192.168.3.1 to network 0.0.0.0 C 192.168.3.0/24 is directly connected, FastEthernet0/1 C 192.168.4.0/24 is directly connected, FastEthernet0/0 S* 0.0.0.0/0 [1/0] via 192.168.3.1 //默认路由
15)测试网络连通性,PC1 ping 192.168.4.1
PC>ping 192.168.4.1 Pinging 192.168.4.1 with 32 bytes of data: Reply from 192.168.4.1: bytes=32 time=1ms TTL=125 Reply from 192.168.4.1: bytes=32 time=0ms TTL=125 Reply from 192.168.4.1: bytes=32 time=14ms TTL=125 Reply from 192.168.4.1: bytes=32 time=14ms TTL=125 Ping statistics for 192.168.4.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 14ms, Average = 7ms