在路由器中配置靜態路由及默認路由

靜態路由介紹:
轉發數據包是路由器的最主要功能。路由器轉發數據包時須要查找路由表,管理員能夠經過手工的方法在路由器中直接配置路由表,這就是靜態路由。雖然靜態路由不適合於在大的網絡中使用,可是因爲靜態路由簡單、路由器負載小、可控性強等緣由,在許多場合中還常常被使用。
路由器在轉發數據時,要先在路由表(routing table)中查找相應的路由。路由器有這麼三種途徑創建路由:
1) 直連網絡:路由器自動添加和本身直接鏈接的網絡的路由
2) 靜態路由:管理員手動輸入到路由器的路由
3) 動態路由:由路由協議(routing protocol)動態創建的路由
靜態路由的缺點是不能動態反映網絡拓撲,當網絡拓撲發生變化時,管理員就必須手工改變路由表;然而靜態路不會佔用路由器太多的CPU RAM 資源,也不佔用線路的帶寬。若是出於安全的考慮想隱藏網絡的某些部分或者管理員想控制數據轉發路徑,也會使用靜態路由。在一個小而簡單的網絡中,也常使用靜態路由,由於配置靜態路由會更爲簡捷。
配置靜態路由的命令爲「ip route」,命令的格式以下:
ip route 目的網絡掩碼 { 網關地址 | 接口 }
默認路由的介紹:
所謂的默認路由,是指路由器在路由表中若是找不到到達目的網絡的具體路由時,最後會採用的路由。默認路由一般會在存根網絡(Stub network,即只有一個出口的網絡)中使用。命令爲:
ip route 0.0.0 .0 0.0.0.0 { 網關地址 | 接口 }
下面經過一個實驗,來了解靜態路由及默認路由的配置:
拓撲圖以下:
操做步驟:
靜態路由的配置
咱們要使得 1.1.1 .0/242.2.2.0/243.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 f0/0
R1(config-if)#ip address 192.168.1.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 f0/0
R2(config-if)#ip address 192.168.1.2 255.255.255.0
R2(config-if)#no shutdown
R2(config)#int f0/1
R2(config-if)#ip address 192.168.2.1 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 f0/1
R3(config-if)#ip address 192.168.2.2 255.255.255.0
R3(config-if)#no shutdown
2) 步驟2R1 上配置靜態路由
R1(config)#ip route 2.2.2 .0 255.255. 255.0 f 0/0
// 下一跳爲接口形式,f0/0 是點對點的鏈路,注意應該是R1 上的f0/0 接口
R1(config)#ip route 3.3.3 .0 255.255.255.0 192.168.1.2
// 下一跳爲IP 地址形式,192.168.1.2 R2 上的IP 地址
3) 步驟3R2 上配置靜態路由
R2(config)#ip route 1.1.1 .0 255.255. 255.0 f 0/0
R2(config)#ip route 3.3.3 .0 255.255. 255.0 f 0/1
4) 步驟4R3 上配置靜態路由
R3(config)#ip route 1.1.1 .0 255.255. 255.0 f 0/1
R3(config)#ip route 2.2.2 .0 255.255. 255.0 f 0/1
(5) 分別在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
 
     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, Ethernet0/0
     3.0.0 .0/24 is subnetted, 1 subnets
S       3.3.3 .0 [1/0] via 192.168.1.2
C    192.168.1.0/24 is directly connected, Ethernet0/0
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
     1.0.0 .0/24 is subnetted, 1 subnets
S       1.1.1 .0 is directly connected, Ethernet0/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, Ethernet0/1
C    192.168.1.0/24 is directly connected, Ethernet0/0
C    192.168.2.0/24 is directly connected, Ethernet0/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, Ethernet0/1
     2.0.0 .0/24 is subnetted, 1 subnets
S       2.2.2 .0 is directly connected, Ethernet0/1
     3.0.0 .0/24 is subnetted, 1 subnets
C       3.3.3 .0 is directly connected, Loopback0
C    192.168.2.0/24 is directly connected, Ethernet0/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]:
Type escape sequence to abort.
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
Type escape sequence to abort.
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 loopback0 接口。
R3#ping 1.1.1 .1 source loopback 0
R3#ping 2.2.2 .2 source loopback 0
// R3 loopback0 也應該能夠ping R1 R2 loopback0 接口。
 
【提示】雖然從R1 loopback0 能夠ping R3 loopback0,數據須要通過
192.168.2.0/24 網絡,可是在R1 上咱們並無添加192.168.2.0/24 的路由。路由器轉發數據包完成是根據路由表的,而且數據是一跳一跳地被轉發的,就像接力賽似的。從R1loopback0 ping R3 loopback0 口時,IP 數據包的源IP 1.1.1 .1,目的IP 3.3.3.3R1 路由器首先查路由表,數據包被髮到了R2R2 路由器也查路由表(3.3.3.0/24 路由),數據包被髮到了R3R3 知道這是直連路由。R3 響應R1 的數據包進行相似的過程。
3) 從R1 ping 2.2.2 .2、從R1 ping 3.3.3.3
R1#ping 2.2.2 .2
Type escape sequence to abort.
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
Type escape sequence to abort.
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 路由器使用f0/0接口的IP 地址(192.168.1.1)做爲IP 數據包的源IP 地址了。R3 上響應R1 的數據包時,數據包是發向192.168.1.1 的,然而因爲R3 沒有192.168.1.0/24 的路由,數據包沒法發送。即:數據包從R1 到了R3 後,沒法返回R1
默認路由的配置:
1) 步驟1R1R3 上刪除原有靜態路由
R1(config)#no ip route 2.2.2 .0 255.255. 255.0 f 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 f 0/1
R3(config)#no ip route 2.2.2 .0 255.255. 255.0 f 0/1
2) 步驟2 R1R3 上配置默認路由
R1(config)#ip route 0.0.0 .0 0.0. 0.0 f 0/0
R3(config)#ip route 0.0.0 .0 0.0. 0.0 f 0/1
驗證: 從各路由器的環回口ping 其餘路由器的環回口,這樣,靜態路由和默認路由就作好了。
相關文章
相關標籤/搜索