admin 管理员组

文章数量: 887021


2024年2月19日发(作者:scalar是显示器中的什么)

ip default-network、ip route、ip default-gateway的区别

在cisco的16448文档中详细地讲解了这三条命令的使用条件和区别。

一、使用条件

ip default-gateway在禁用了ip routing的情况下使用,该路由器被当做默认网关。

ip default-network和ip route 0.0.0.0 0.0.0.0 则可以在启用路由选择协议下使用。

二、区别

ip default-network 与 ip route 0.0.0.0 0.0.0.0 的最大区别在于前者可以被路由选择协议宣告出去。

但是由于ip default-network这条命令是有类的,如果宣告的网络不是主类网,将不会被标记成*号。

ip default-network产生的默认路由是可以被EIGRP和RIP宣告的,但不被OSPF宣告,至于OSPF怎么产生默认路由,上一边有介绍。值得注意的是,RIP宣告ip default-network的路由条目是,使用0.0.0.0,可以debug ip rip看到。

ip route 0.0.0.0 0.0.0.0 也可以被EIRGP传播,但是这条静态路由必须重分发到EIGRP协议中;在12.0T以后的IOS版本中,RIP将不宣告默认路由,除非是通过RIP学习到得,或者重分发到RIP中。

同样,在OSPF中也不能传播该命令产生的默认路由,(default-information

originate命令)

三、负载均衡

如果用ip default-network配置了去往同一目的的多条默认路由,路由表会选择管理距离最小,如果管理距离都一样则选择最先配置的那条。

同时使用ip default-network和ip route 0.0.0.0 0.0.0.0双方设定默认路由时,如果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 default-gateway,ip default-network和ip

route 默认路由的区别

关于这几条命令的区别,网上已经有很多讨论了,也的确是很容易混淆的。下面是整理的一些总结,都一一举例做了比较,应该会有一个全新的认识,以后实际中遇到了也不混淆犯错了。

指定默认路由(last resort gateway)的指令供有3种,可以分成两类:

1、ip default-gateway

当路由器上的ip routing无效时,使用它指定默认路由,用于RXBoot模式(no

ip routing)下安装IOS等。

或者关闭ip routing 让路由器当主机用,此时需要启用ip default-gateway配置默认网关。

2、ip default-network和ip route 0.0.0.0 0.0.0.0

两者都用于ip routing有效的路由器上,区别主要在于路由协议是否传播这条路由信息。比如:IGRP无法识别0.0.0.0,因此传播默认路由时必须用ip

default-network。

当用ip default-network指令设定多条默认路由时,administrative distance最短的成为最终的默认路由;如果有复数条路由distance值相等,那么在路由表(show ip route)中靠上的成为默认路由。

同时使用ip default-network和ip route 0.0.0.0 0.0.0.0双方设定默认路由时,如果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 routing 举例:

mycisco(config)#no ip routing

mycisco(config)#ip default-gateway 192.168.0.1

mycisco(config)#end

mycisco#ping

Type escape sequence to abort.

Sending 5, 100-byte ICMP Echoes to 61.152.167.75, timeout is 2 seconds:

!!!!!

Success rate is 100 percent (5/5), round-trip min/avg/max = 60/60/60 ms

mycisco#show ip route

Default gateway is 192.168.0.1

Host Gateway Last Use Total Uses Interface

ICMP redirect cache is empty

mycisco#

ip route例子:

ip route 0.0.0.0 0.0.0.0 192.168.0.1

mycisco#ping

Type escape sequence to abort.

Sending 5, 100-byte ICMP Echoes to 61.152.167.75, timeout is 2 seconds:

!!!!!

Success rate is 100 percent (5/5), round-trip min/avg/max = 56/56/60 ms

mycisco#show ip route

Codes: C – connected, S – static, I – IGRP, R – RIP, M – mobile, B

– BGP

D – EIGRP, EX – EIGRP external, O – OSPF, IA – OSPF inter area

E1 – OSPF external type 1, E2 – OSPF external type 2, E – EGP

i – IS-IS, L1 – IS-IS level-1, L2 – IS-IS level-2, * – candidate default

U – per-user static route

Gateway of last resort is 192.168.0.1 to network 0.0.0.0

C 1.0.0.0/8 is directly connected, Loopback0

C 192.168.0.0/24 is directly connected, Ethernet0

S* 0.0.0.0/0 [1/0] via 192.168.0.1

mycisco#

ip default-network 必须是在所到网络已经存在路由的情况下,否则执行无效.

mycisco(config)#ip route 61.0.0.0 255.0.0.0 192.168.0.1

mycisco#show ip route

Codes: C – connected, S – static, I – IGRP, R – RIP, M – mobile, B

– BGP

D – EIGRP, EX – EIGRP external, O – OSPF, IA – OSPF inter area

E1 – OSPF external type 1, E2 – OSPF external type 2, E – EGP

i – IS-IS, L1 – IS-IS level-1, L2 – IS-IS level-2, * – candidate default

U – per-user static route

Gateway of last resort is not set

C 1.0.0.0/8 is directly connected, Loopback0

S 61.0.0.0/8 [1/0] via 192.168.0.1

C 192.168.0.0/24 is directly connected, Ethernet0

接着我们执行:

mycisco(config)#ip default-network 61.0.0.0

再看路由表:

mycisco#show ip route

Codes: C – connected, S – static, I – IGRP, R – RIP, M – mobile, B

– BGP

D – EIGRP, EX – EIGRP external, O – OSPF, IA – OSPF inter area

E1 – OSPF external type 1, E2 – OSPF external type 2, E – EGP

i – IS-IS, L1 – IS-IS level-1, L2 – IS-IS level-2, * – candidate default

U – per-user static route

Gateway of last resort is 192.168.0.1 to network 61.0.0.0

C 1.0.0.0/8 is directly connected, Loopback0

S* 61.0.0.0/8 [1/0] via 192.168.0.1

C 192.168.0.0/24 is directly connected, Ethernet0

ip default-gateway

ip default-gateway command differs from the other two commands in that it should only be used

when ip routing is disabled on the Cisco router.

For instance, if the router is a host in the IP world, you can use this command to define a default

gateway for it. You might also use this command when your low end Cisco router is in boot mode

in order to TFTP a Cisco IOS® Software image to the router. In boot mode, the router does

not have ip routing enabled.

ip default-network

Unlike the ip default-gateway command, you can use ip default-network when ip routing is

enabled on the Cisco router. When you configure ip default-network the router considers routes

to that network for installation as the gateway of last resort on the router.

For every network configured with ip default-network, if a router has a route to that network,

that route is flagged as a candidate default route. Look at the following routing table taken from a

Cisco router:

2513#show ip route

Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP

D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP

i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, * - candidate default

Gateway of last resort is not set

161.44.0.0 255.255.255.0 is subnetted, 1 subnets

C 161.44.192.0 is directly connected, Ethernet0

S 198.10.1.0 [1/0] via 161.44.192.2

131.108.0.0 255.255.255.0 is subnetted, 1 subnets

C 131.108.99.0 is directly connected, TokenRing0

Note the static route to 198.10.1.0 via 161.44.192.2 and that the gateway of last resort is not set.

If you configure ip default-network 198.10.1.0, the routing table changes to the following:

2513#show ip route

Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP

D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP

i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, * - candidate default

Gateway of last resort is 161.44.192.2 to network 198.10.1.0

161.44.0.0 255.255.255.0 is subnetted, 1 subnets

C 161.44.192.0 is directly connected, Ethernet0

S 161.44.0.0 255.255.0.0 [1/0] via 161.44.192.0

S* 198.10.1.0 [1/0] via 161.44.192.2

131.108.0.0 255.255.255.0 is subnetted, 1 subnets

C 131.108.99.0 is directly connected, TokenRing0

2513#show ip protocols

2513#

You can see the gateway of last resort has now been set as 161.44.192.2. This result is

independent of any routing protocol, as shown by the show ip protocols command at the bottom

of the output.

You can add another candidate default route simply by configuring another instance of ip

default-network:

2513#config terminal

Enter configuration commands, one per line. End with CNTL/Z.

2513(config)#ip route 171.70.24.0 255.255.255.0 131.108.99.2

2513(config)#ip default-network 171.70.24.0

2513(config)#^Z

2513#show ip route

Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP

D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP

i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, * - candidate default

Gateway of last resort is 161.44.192.2 to network 198.10.1.0

161.44.0.0 255.255.255.0 is subnetted, 1 subnets

C 161.44.192.0 is directly connected, Ethernet0

S 161.44.0.0 255.255.0.0 [1/0] via 161.44.192.0

S* 198.10.1.0 [1/0] via 161.44.192.2

171.70.0.0 is variably subnetted, 2 subnets, 2 masks

S 171.70.0.0 255.255.0.0 [1/0] via 171.70.24.0

S 171.70.24.0 255.255.255.0 [1/0] via 131.108.99.2

131.108.0.0 255.255.255.0 is subnetted, 1 subnets

C 131.108.99.0 is directly connected, TokenRing0

Notice that after entering the above command, the network was not flagged as a default network.

The following section explains why.

Flagging a Default Network

Note: The ip default-network command is classful, which means if the router has a route to the

subnet indicated by this command, it installs the route to the major net. At this point neither

network has been flagged as the default network. The ip default-network command must be

issued again, using the major net, in order to flag the candidate default route.

2513#config terminal

Enter configuration commands, one per line. End with CNTL/Z.

2513(config)#ip default-network 171.70.0.0

2513(config)#^Z

2513#show ip route

Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP

D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP

i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, * - candidate default

Gateway of last resort is 161.44.192.2 to network 198.10.1.0

161.44.0.0 255.255.255.0 is subnetted, 1 subnets

C 161.44.192.0 is directly connected, Ethernet0

S 161.44.0.0 255.255.0.0 [1/0] via 161.44.192.0

S* 198.10.1.0 [1/0] via 161.44.192.2

171.70.0.0 is variably subnetted, 2 subnets, 2 masks

S* 171.70.0.0 255.255.0.0 [1/0] via 171.70.24.0

S 171.70.24.0 255.255.255.0 [1/0] via 131.108.99.2

131.108.0.0 255.255.255.0 is subnetted, 1 subnets

C 131.108.99.0 is directly connected, TokenRing0

If the original static route had been to the major network, the extra step of configuring the

default network twice would not have been necessary.

There are still no IP protocols running here. Without any dynamic protocols running, you can

configure your router to choose from a number of candidate default routes based on whether

the routing table has routes to networks other than 0.0.0.0/0. This command allows you to

configure robustness into the selection of a gateway of last resort. Rather than configuring static

routes to specific next-hops, you can have the router choose a default route to a particular

network by checking in the routing table.

If you lose the route to a particular network, the router selects the second candidate default, as

specified above. You can remove the lost route by removing the static route in the configuration

as follows:

2513#config terminal

Enter configuration commands, one per line. End with CNTL/Z.

2513(config)#no ip route 198.10.1.0 255.255.255.0 161.44.192.2

2513(config)#^Z

2513#

%SYS-5-CONFIG_I: Configured from console by console

After removing the static route to the original default network, the routing table looks like this:

2513#show ip route

Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP

D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP

i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, * - candidate default

Gateway of last resort is 171.70.24.0 to network 171.70.0.0

161.44.0.0 255.255.255.0 is subnetted, 1 subnets

C 161.44.192.0 is directly connected, Ethernet0

S 161.44.0.0 255.255.0.0 [1/0] via 161.44.192.0

* 171.70.0.0 is variably subnetted, 2 subnets, 2 masks

S* 171.70.0.0 255.255.0.0 [1/0] via 171.70.24.0

S 171.70.24.0 255.255.255.0 [1/0] via 131.108.99.2

131.108.0.0 255.255.255.0 is subnetted, 1 subnets

C 131.108.99.0 is directly connected, TokenRing0

2513#

Using Different Routing Protocols

Gateways of last resort selected using the ip default-network command are propagated

differently depending on which routing protocol is propagating the default route. For IGRP and

EIGRP to propagate the route, the network specified by the ip default-network command must be

known to IGRP or EIGRP. This means the network must be an IGRP- or EIGRP-derived network in

the routing table, or the static route used to generate the route to the network must be

redistributed into IGRP or EIGRP.

RIP advertises a route to 0.0.0.0. For example, note that the gateway of last resort on the router

below was learned using the combination of the ip route and ip default-network commands. If

you enable RIP on this router, RIP advertises a route to 0.0.0.0 (although not to the Token Ring

network because of split-horizon):

2513(config)#router rip

2513(config-router)#network 161.44.0.0

2513(config-router)#network 131.108.0.0

2513(config-router)#^Z

2513#

2513#

2513#

%SYS-5-CONFIG_I: Configured from console by console

2513#debug ip rip

RIP protocol debugging is on

2513#

RIP: sending update to 255.255.255.255 via Ethernet0 (161.44.192.1)

default 0.0.0.0, metric 1

network 131.108.0.0, metric 1

RIP: sending update to 255.255.255.255 via TokenRing0 (131.108.99.1)

network 161.44.0.0, metric 1

2513#

Note: In IOS release 12.0T and later, RIP does not advertise the default router if the route is not

learned via RIP. Therefore, it may be necessary to redistribute the route into RIP, or use the

default-information originate command.

OSPF, like RIP, advertises a route for 0.0.0.0 0.0.0.0. However, with OSPF, the router originating

the default route must be configured with the default-information originate command. For more

detailed information, see How Does OSPF Generate Default Routes?.

ip route 0.0.0.0 0.0.0.0

Creating a static route to network 0.0.0.0 0.0.0.0 is another way to set the gateway of last resort

on a router. As with the ip default-network command, using the static route to 0.0.0.0 is not

dependent on any routing protocols. However, ip routing must be enabled on the router.

Note: IGRP does not understand a route to 0.0.0.0, therefore it cannot propagate default routes

created using the ip route 0.0.0.0 0.0.0.0 command. Use the ip default-network command to

have IGRP propagate a default route.

EIGRP propagates a route to network 0.0.0.0, but the static route must be redistributed into

EIGRP. RIP and OSPF behave as described when using the ip default-network command. Keep in

mind that the note above still applies.

Look at an example of configuring a gateway of last resort using the ip route 0.0.0.0 0.0.0.0

command:

router-3#conf terminal

Enter configuration commands, one per line. End with CNTL/Z.

router-3(config)#ip route 0.0.0.0 0.0.0.0 170.170.3.4

router-3(config)#^Z

router-3#

router-3#show ip route

Codes: C - connected, S - static, I - IGRP, 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, E - EGP

i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, * - candidate default

U - per-user static route, o - ODR

Gateway of last resort is 170.170.3.4 to network 0.0.0.0

170.170.0.0/24 is subnetted, 2 subnets

C 170.170.2.0 is directly connected, Serial0

C 170.170.3.0 is directly connected, Ethernet0

S* 0.0.0.0/0 [1/0] via 170.170.3.4

router-3#

router-3#

Note: If you configure multiple networks as candidate default routes using the ip default-network

command, the network that has the lowest administrative distance is chosen as the network for

the gateway of last resort. If all the networks have the same administrative distance then the

network listed first in the routing table (show ip route lists the routing table) is chosen as the

network for the gateway of last resort. If you use both the ip default-network and ip route 0.0.0.0

0.0.0.0 commands to configure candidate default networks, and the network used by the ip

default-network command is known statically, the network defined with the ip default-network

command takes precedence and is chosen for the gateway of last resort. Otherwise if the

network used by the ip default-network command is derived by a routing protocol, the ip route

0.0.0.0 0.0.0.0 command, which will have a lower administrative distance, takes precedence and

is chosen for the gateway of last resort. Lastly, if you use multiple ip route 0.0.0.0 0.0.0.0

commands to configure a default route, traffic is load-balanced over the multiple routes.

Summary

To summarize, you can use the ip default-gateway command when ip routing is disabled on a

Cisco router. Use the ip default-network and ip route 0.0.0.0 0.0.0.0 commands to set the

gateway of last resort on Cisco routers that have ip routing enabled. The way in which routing

protocols propagate the default route information varies for each protocol.


本文标签: 路由 默认 选择 使用 协议