路由器是干啥的?c++
链接不一样地址段的网络。屏蔽不一样地址段的网络广播。shell
路由器有多个接口,至少得有2个吧,一边一个局域网。微信
查询路由表里的路由条目show ip route
网络
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.0.0/24 is directly connected, FastEthernet1/0
C:表明直连的意思。就是说我使用接口1/0,能够链接192.168.0.0/24网络上的任意主机。负载均衡
不退出全局模式,在全局模式下执行特权模式下的命令:在原来特权模式下的命令的前面加do学习
R2(config)#do show ip interface brief Interface IP-Address OK? Method Status Protocol FastEthernet0/0 192.168.0.2 YES manual up up FastEthernet1/0 unassigned YES unset administratively down down R2(config)#
测试2个点是否联通:ping测试
R1#ping 192.168.0.2 Type escape sequence to abort. Sending 5, 100-byte ICMP Echos to 192.168.0.2, timeout is 2 seconds: .!!!! Success rate is 80 percent (4/5), round-trip min/avg/max = 60/61/64 ms R1#ping 192.168.0.2
执行结果里的【.!!!!】的含义:!表明通了;.表明不通。只要有一个!,就说明通了。至于为何有第一个点,是由于ARP的缘由。3d
假设路由器有2个接口,接口f0/0的ip地址是:192.168.0.1/24,那么就不能把另外一个接口f1/0的ip地址配置成和接口f0/0是同一个网段的ip地址了,会报出下面的错误:【% 192.168.0.0 overlaps with FastEthernet0/0】。code
路由器就是为链接不一样地址段网络而生的,2个接口设置成了相同的地址段,就变成交换机(swith)了。那就不如直接用交换机了。交换机比路由器便宜多了。blog
R2(config)#do show ip interface brief Interface IP-Address OK? Method Status Protocol FastEthernet0/0 192.168.0.2 YES manual up up FastEthernet1/0 unassigned YES unset administratively down down R2(config)#interface f1/0 R2(config-if)#ip addres 192.168.0.3 255.255.255.0 % 192.168.0.0 overlaps with FastEthernet0/0
ping使用的协议是ICMP。ICMP协议是网络层协议。
从一个路由器A的一个接口f0/0,ping另外一个路由器B的一个非直链接口f1/0的地址的场景,这2个接口处于不一样的网路段:
ping要使用ICMP协议,ICMP协议的报文里要求有源IP地址和目的IP地址,因为没法知道(路由器A里的路由表里没有路由器B里f1/0网段的条目)从哪一个接口出去,就没法知道源IP地址,因此ICMP协议的包就没法作成,固然就没法发送,因此ping不通。
ping,必须能去并且还能回来才能ping通。
方法1,不指定出接口,也就是没有指定源IP,须要路由器去递归查询一次,才能得到出接口的源ip:ip route 192.168.1.0 255.255.255.0 192.168.0.2
192.168.1.0 255.255.255.0:是目标网络
192.168.0.2:是下一跳。
为了可以访问192.168.1.0 255.255.255.0网络,必须借助192.168.0.2,做为跳板。
R1(config)#ip route 192.168.1.0 255.255.255.0 192.168.0.2 R1(config)#do show ip route Gateway of last resort is not set C 192.168.0.0/24 is directly connected, FastEthernet1/0 S 192.168.1.0/24 [1/0] via 192.168.0.2
S:表明静态路由的意思。
方法2,不指定下一跳,可是指定了出接口。没下一跳,路由寻址慢:ip route 192.168.1.0 255.255.255.0 f1/0
R1(config)#ip route 192.168.1.0 255.255.255.0 f1/0 R1#show ip route C 192.168.0.0/24 is directly connected, FastEthernet1/0 S 192.168.1.0/24 is directly connected, FastEthernet1/0
方法3,既指定出接口又指定下一跳:ip route 192.168.1.0 255.255.255.0 f1/0 192.168.0.2
R1(config)#ip route 192.168.1.0 255.255.255.0 f1/0 192.168.0.2 R1(config)#do show ip route C 192.168.0.0/24 is directly connected, FastEthernet1/0 S 192.168.1.0/24 [1/0] via 192.168.0.2, FastEthernet1/0
这种方式最好
好比公司,公司里面的主机都是私有ip,公司只有一个公网ip,因此须要把路由器的下一跳指定成这个公网ip。
S 0.0.0.0/0 [1/0] via 公网ip, FastEthernet1/0
目的地是相同的,有不少通路均可以到达目的地,为了不某几条路由过于拥堵,就使用负载均衡路由条目,把请求平均分配到每条路由。
S 0.0.0.0/0 [1/0] via 192.168.0.2,192.168.1.2,192.168.2.2 ...
当有1个以上路由条目均可以到达目的地时,选用那条路由呢?
选用掩码最长的。
例如:目的地是192.168.2.1,路由条目有2条,以下所示。这2条均可以到达,但选用的是S 192.168.2.1 /32。由于它的掩码的长度是32,另外一个是24.
S 192.168.2.1 /32 via ... S 192.168.2.0 /24 via ...
例如:访问12.2.0.0时,12.1.1.0/24和12.1.1.0/24都不匹配,只能匹配12.0.0.0/8
12.1.1.1/32 12.1.1.0/24 12.0.0.0/8