簡介
本文件說明如何設定預設路由或最後選用閘道。
必要條件
需求
本文件沒有特定需求。
採用元件
本文件所述內容不限於特定軟體和硬體版本。所示命令輸出是從採用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路由器處於引導模式時使用此命令,以透過TFTP將Cisco IOS®軟體映象傳輸到路由器。在啟動模式下,路由器未啟用ip routing。以下示例將IP地址172.16.15.4上的路由器定義為預設路由:
ip default-gateway 172.16.15.4
使用 ip default-network 命令
不同於ip default-gateway命令,可以在Cisco路由器上啟用IP路由時使用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.0建立的預設路由由RIP路由器自動通告。在 Cisco IOS 12 及更新版本的軟體中,如果系統不是透過 RIP 得知預設路由,RIP 就不會通告該路由。可能需要將路由重分配到RIP中。
OSPF 和 IS-IS 不會傳播使用 ip route 0.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路由器上設定最後選用網關。路由協定傳播預設路由資訊的方式因協定而異。
相關資訊