簡介
本文件說明如何設定預設路由或最後選用閘道。
必要條件
需求
本文件沒有特定需求。
採用元件
本文件所述內容不限於特定軟體和硬體版本。所示命令輸出是從採用Cisco IOS®軟體版本15M的Cisco 3900系列路由器中選取的。
本文中的資訊是根據特定實驗室環境內的裝置所建立。文中使用到的所有裝置皆從已清除(預設)的組態來啟動。如果您的網路運作中,請確保您瞭解任何指令可能造成的影響。
慣例
如需文件慣例的詳細資訊,請參閱思科技術提示慣例。
背景資訊
預設路由用於將資料包定向到路由表中未明確列出的網路。在不需要學習所有更具體的網路(如末節網路)或由於系統資源(如記憶體和有限資源)有限而不可行的拓撲中,預設路由是非常寶貴的。
將使用下一個IP命令,並作更詳細的說明:
-
ip default-gateway
-
ip default-network
-
ip route 0.0.0.0 0.0.0.0
使用命令ip default-gateway
ip default-gateway 指令與其他兩個指令不同,因為只有在Cisco路由器上停用ip路由時才能使用它。例如,如果路由器是IP世界中的主機,則可以使用此命令為其定義預設網關。您也可以在低端Cisco路由器處於啟動模式時,使用此命令將Cisco IOS®軟體映像通過TFTP傳送到路由器。在啟動模式下,路由器未啟用ip routing。以下示例將IP地址172.16.15.4上的路由器定義為預設路由:
ip default-gateway 172.16.15.4
使用命令ip default-network
與ip default-gateway 指令不同,您可以在思科路由器上啟用ip routing時使用ip default-network。設定ip default-network時,路由器會將通往該網路的路由視為路由器上的最後選用網關。對於使用ip default-network配置的每個網路,如果路由器具有通往該網路的路由,則該路由會標籤為候選預設路由。此網路圖顯示從路由器R1獲取的路由表:
R1#show ip route
Codes: L - local, 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, H - NHRP, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
Gateway of last resort is not set
10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks
C 10.44.192.0/24 is directly connected, GigabitEthernet0/0
L 10.44.192.1/32 is directly connected, GigabitEthernet0/0
C 10.108.99.0/24 is directly connected, GigabitEthernet0/1
L 10.108.99.1/32 is directly connected, GigabitEthernet0/1
S 192.168.1.0/24 [1/0] via 10.44.192.2
請注意,通過10.44.192.2到達192.168.1.0的靜態路由且未設定最後選用網關。如果配置ip default-network 192.168.1.0,路由表將更改如下:
R1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#ip default-network 192.168.1.0
R1(config)#end
R1#
R1#show ip route
Codes: L - local, 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, H - NHRP, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
Gateway of last resort is 10.44.192.2 to network 192.168.1.0
S* 0.0.0.0/0 [1/0] via 10.44.192.2
10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks
C 10.44.192.0/24 is directly connected, GigabitEthernet0/0
L 10.44.192.1/32 is directly connected, GigabitEthernet0/0
C 10.108.99.0/24 is directly connected, GigabitEthernet0/1
L 10.108.99.1/32 is directly connected, GigabitEthernet0/1
S* 192.168.1.0/24 [1/0] via 10.44.192.2
R1#
R1#show ip protocols
*** IP Routing is NSF aware ***
Routing Protocol is "application"
Sending updates every 0 seconds
Invalid after 0 seconds, hold down 0, flushed after 0
Outgoing update filter list for all interfaces is not set
Incoming update filter list for all interfaces is not set
Maximum path: 32
Routing for Networks:
Routing Information Sources:
Gateway Distance Last Update
Distance: (default is 4)
R1#
最後選用閘道現在設定為10.44.192.2。此結果獨立於任何路由協定,如show ip protocols命令輸出所示,未配置路由協定。您可以使用ip default-network的另一個例項的配置,新增另一個候選預設路由:
R1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#ip route 172.17.24.0 255.255.255.0 10.108.99.2
R1(config)#ip default-network 172.17.24.0
R1(config)#end
R1#
R1#show ip route
Codes: L - local, 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, H - NHRP, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
Gateway of last resort is 10.44.192.2 to network 192.168.1.0
S* 0.0.0.0/0 [1/0] via 10.44.192.2
10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks
C 10.44.192.0/24 is directly connected, GigabitEthernet0/0
L 10.44.192.1/32 is directly connected, GigabitEthernet0/0
C 10.108.99.0/24 is directly connected, GigabitEthernet0/1
L 10.108.99.1/32 is directly connected, GigabitEthernet0/1
172.17.0.0/16 is variably subnetted, 2 subnets, 2 masks
S 172.17.0.0/16 [1/0] via 172.17.24.0
S 172.17.24.0/24 [1/0] via 10.108.99.2
S* 192.168.1.0/24 [1/0] via 10.44.192.2
註:輸入ip default-network命令後,您可以觀察到網路未標籤為預設網路。標籤預設網絡部分說明了原因。
標籤預設網路
ip default-network命令是有類的,這表示如果路由器具有通往此命令所指示的子網的路由,它會將路由安裝到主網路。此時,兩個網路均未標籤為預設路由。必須再次運行ip default-network命令,這次是使用主網路,以便將候選路由標籤為預設路由。
R1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#ip default-network 172.17.0.0
R1(config)#end
R1#sh
*Jul 15 22:32:42.829: %SYS-5-CONFIG_I: Configured from console by conso
R1#show ip route
Codes: L - local, 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, H - NHRP, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
Gateway of last resort is 172.17.24.0 to network 172.17.0.0
S* 0.0.0.0/0 [1/0] via 172.17.24.0
10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks
C 10.44.192.0/24 is directly connected, GigabitEthernet0/0
L 10.44.192.1/32 is directly connected, GigabitEthernet0/0
C 10.108.99.0/24 is directly connected, GigabitEthernet0/1
L 10.108.99.1/32 is directly connected, GigabitEthernet0/1
* 172.17.0.0/16 is variably subnetted, 2 subnets, 2 masks
S* 172.17.0.0/16 [1/0] via 172.17.24.0
S 172.17.24.0/24 [1/0] via 10.108.99.2
S* 192.168.1.0/24 [1/0] via 10.44.192.2
如果原始靜態路由已配置到主網路,則無需執行上述步驟。
路由器中仍然未配置IP協定。如果沒有任何動態協定,您可以將路由器配置為根據路由表是否包含通往0.0.0.0/0以外網路的路由,從一系列候選預設路由中進行選擇。ip default-network 命令可讓您在選擇最後選用網關時配置穩健性。您可以讓路由器根據路由表資訊選擇通往特定網路的預設路由,而不是使用通往特定下一跳的靜態路由。
如果失去通往特定網路的路由,路由器會選擇另一個候選預設路由。在這種情況下,您可以從配置中刪除丟失的路由,如下面的輸出所示:
R1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#no ip route 172.17.24.0 255.255.255.0 10.108.99.2
R1(config)#end
*Jul 15 22:52:59.047: %SYS-5-CONFIG_I: Configured from console by console
刪除通往網路的靜態路由後,路由表如下所示:
R1#show ip route
Codes: L - local, 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, H - NHRP, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
Gateway of last resort is 10.44.192.2 to network 192.168.1.0
S* 0.0.0.0/0 [1/0] via 10.44.192.2
10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks
C 10.44.192.0/24 is directly connected, GigabitEthernet0/0
L 10.44.192.1/32 is directly connected, GigabitEthernet0/0
C 10.108.99.0/24 is directly connected, GigabitEthernet0/1
L 10.108.99.1/32 is directly connected, GigabitEthernet0/1
S* 192.168.1.0/24 [1/0] via 10.44.192.2
R1#
使用不同的路由協定
根據所使用的路由協定,通過ip default-network命令選擇的最後選用網關的傳播方式不同。對於EIGRP,在EIGRP中配置預設路由的方法有不同的首選。使用ip default-network命令通告的預設路由不會通過開放最短路徑優先(OSPF)或中間系統到中間系統(IS-IS)傳播。有關OSPF預設路由行為的詳細資訊,請參閱OSPF如何生成預設路由?。
使用命令ip route 0.0.0.0 0.0.0.0
使用ip route 0.0.0.0 0.0.0.0命令配置的靜態路由是設定路由器上最後選用網關的另一種方法。與ip default-network命令一樣,到0.0.0.0的靜態路由的使用不依賴於任何路由協定。但是,必須在路由器上啟用ip routing。
注意:EIGRP將路由傳播到網路0.0.0.0,但必須將靜態路由重新分發到路由協定中。
在早期版本的RIP中,通過ip route 0.0.0.0 0.0.0建立的預設路由由RIP路由器自動通告。在Cisco IOS軟體版本12及更高版本中,如果路由不是通過RIP獲知的,RIP不會通告預設路由。可能需要將路由重分配到RIP中。
OSPF和IS-IS不會傳播使用ip route 0.0.0 0.0.0.0命令配置的預設路由。此外,無法通過redistribute命令將此預設路由重分佈到OSPF或IS-IS中。使用default-information originate命令生成到IS-IS或OSPF路由域的預設路由。有關OSPF預設路由行為的詳細資訊,請參閱OSPF如何生成預設路由?。下一輸出是有關如何使用ip route 0.0.0.0 0.0.0.0命令配置最後選用網關的示例:
R1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#ip route 0.0.0.0 0.0.0.0 10.108.99.2
R1(config)#end
R1#
R1#show ip route
Codes: L - local, 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, H - NHRP, l - LISP
a - application route
+ - replicated route, % - next hop override, p - overrides from PfR
Gateway of last resort is 10.108.99.2 to network 0.0.0.0
S* 0.0.0.0/0 [1/0] via 10.108.99.2
10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks
C 10.44.192.0/24 is directly connected, GigabitEthernet0/0
L 10.44.192.1/32 is directly connected, GigabitEthernet0/0
C 10.108.99.0/24 is directly connected, GigabitEthernet0/1
L 10.108.99.1/32 is directly connected, GigabitEthernet0/1
D 192.168.1.0/24 [90/130816] via 10.44.192.2, 00:20:24, GigabitEthernet0/0
R1#show ip route 0.0.0.0 0.0.0.0
Routing entry for 0.0.0.0/0, supernet
Known via "static", distance 1, metric 0, candidate default path
Routing Descriptor Blocks:
* 10.108.99.2
Route metric is 0, traffic share count is 1
註:如果通過ip default-network 命令將多個網路配置為候選預設路由,則會選擇具有最低管理距離的網路作為最後選用網關的網路。如果所有網路具有相同的管理距離,則選擇路由表中列出的第一個網路作為最後選用網關的網路。如果同時使用ip default-network和ip route 0.0.0 0.0.0.0.0 命令來配置候選預設網路,則ip route 0.0.0.0 0.0.0.0命令優先執行,並被選為最後選用網關。如果使用多個ip route 0.0.0.0 0.0.0.0命令配置預設路由,則流量會在多個路由上進行負載均衡。
摘要
在Cisco路由器上停用ip routing時,請使用ip default-gateway命令。使用ip default-network或ip route 0.0.0.0 0.0.0.0命令在已啟用ip路由的Cisco路由器上設定最後選用網關。路由協定傳播預設路由資訊的方式因協定而異。
相關資訊