简介
本文档介绍如何配置默认路由或最后选用网关。
先决条件
要求
本文档没有任何特定的要求。
使用的组件
本文档不限于特定的软件和硬件版本。图中所示的命令输出取自安装了Cisco IOS®软件版本15M的Cisco 3900系列路由器。
本文档中的信息都是基于特定实验室环境中的设备编写的。本文档中使用的所有设备最初均采用原始(默认)配置。如果您的网络处于活动状态,请确保您了解所有命令的潜在影响。
规则
有关文档规则的详细信息,请参阅Cisco技术提示规则。
背景信息
默认路由用于指引发往路由表中未明确地列出的网络的数据包到达目标。默认路由在不需要学习所有更具体的网络(如末节网络)的拓扑中非常宝贵,或者由于系统资源(如内存和有限资源)有限而不可行。
使用下面的IP命令并对其进行更详细的说明:
-
ip default-gateway
-
ip default-network
-
ip route 0.0.0.0 0.0.0.0
使用命令ip default-gateway
ip default-gatewaycommand与另外两个命令不同,因为只有在Cisco路由器上禁用ip routing时才能使用它。例如,如果路由器是 IP 环境中的主机,可以使用此命令为其定义默认网关。当您的低端Cisco路由器处于引导模式时,您也可以使用此命令将Cisco IOS®软件映像通过TFTP传输到路由器。在启动模式下,路由器没有启用ip。此示例将 IP 地址 172.16.15.4 上的路由器定义为默认路由:
ip default-gateway 172.16.15.4
使用命令ip default-network
与ip default-gatewaycommand不同,在Cisco路由器上启用ip routing时,您可以使用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命令输出所示,其中未配置路由协议。您可以使用另一个实例ofip 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-networkcommand后,您可以观察到网络未标记为默认网络。Flag a Default Network部分解释了原因。
标记默认网络
ip default-networkcommand是有类的,这意味着如果路由器具有此命令指示的子网路由,则会将路由安装到主网络。此时,两个网络均未标记为默认路由。必须再次运行ip default-networkcommand,这次要使用主网络,以便将候选路由标记为默认路由。
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-networkcommand允许您在选择最后选用网关时配置稳健性。您可以让路由器根据路由表信息选择通往特定网络的默认路由,而不是使用通往特定下一跳的静态路由。
如果到特定网络的路由丢失,则路由器将选择另一个候选默认路由。在这种情况下,您可以从配置中删除丢失的路由,如下面的输出所示:
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-networkcommand选择的最后选用网关的传播方式会根据所使用的路由协议而有所不同。对于EIGRP,在EIGRP中配置默认路由有不同的首选方法。使用ip default-networkcommand通告的默认路由不通过开放最短路径优先(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-networkcommand一样,使用到0.0.0.0的静态路由不依赖于任何路由协议。但是,必须在路由器上启用ip路由。
注意: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命令配置的默认路由。此外,此默认路由不能通过theredistributtecommand重分发到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-networkcommand将多个网络配置为候选默认路由,则具有最小管理距离的网络将被选为最后选用网关的网络。如果所有网络具有相同的管理距离,则选择路由表中列出的第一个网络作为最后选用网关的网络。如果同时使用ip default-networkandip route 0.0.0 0.0.0.0.0命令配置候选默认网络,则ip route 0.0.0.0 0.0.0.0.0命令将优先于最后选用网关,并被选中。如果使用multipleip route 0.0.0.0 0.0.0.0命令配置默认路由,流量将在多个路由上实现负载均衡。
摘要
当Cisco路由器上禁用了ip routing时,请使用ip default-gatewaycommand。使用ip default-networkorip route 0.0.0 0.0.0.0.0命令在启用了IP路由的Cisco路由器上设置最后选择网关。路由协议传播默认路由信息的方式因各个协议而异。
相关信息