路由器是幹啥的?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