Previous section   Next section

Recipe 5.4 Using Static Routing

5.4.1 Problem

You want to configure a static route.

5.4.2 Solution

You can configure a static route with the ip route command, as follows:

Router#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#ip route 10.35.15.5 255.255.255.255 Ethernet0
Router(config)#end 
Router#

You can also configure a static route to point to a particular next-hop router:

Router#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#interface Serial0
Router(config-if)#ip address 10.35.6.2 255.255.255.0  
Router(config-if)#exit
Router(config)#ip route 172.16.0.0 255.255.0.0 10.35.6.1 2
Router(config)#end
Router#

If you want to ensure that a route remains in place even if the next-hop IP address becomes unreachable or the interface goes down, you can use the permanent keyword:

Router#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#ip route 10.35.15.5 255.255.255.255 Ethernet0 permanent
Router(config)#ip route 172.16.0.0 255.255.0.0 10.35.6.1 2 permanent
Router(config)#end
Router#

You can also manually configure routing tags using static routes with the tag keyword:

Router#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#ip route 172.16.0.0 255.255.0.0 10.35.6.1 2 tag 36291
Router(config)#end
Router#

5.4.3 Discussion

The first version sends all packets destined to the single host 10.35.15.5 out through the Ethernet0 interface. In this case, the router will need to figure out which device on this segment to forward the packet to, because it must put the MAC address of the next-hop router in the Layer 2 frame header. The standard mechanism for associating IP addresses with MAC addresses is the Address Resolution Protocol (ARP). The router will send out an ARP request broadcast on the Ethernet segment. If the device that owns the packet's destination IP address happens to be on this segment, it will respond with its MAC address. Otherwise, a router configured for proxy ARP will have to respond on its behalf. This is important, because if you do not have proxy ARP configured on the next-hop router, this command will fail. So, for multiple access media such as Ethernet segments, we recommend specifying the IP address of the next-hop router rather than the interface.

Refer to Chapter 22 for more information about enabling and disabling proxy ARP.

You can also specify a point-to-point media such as a serial interface for the route destination:

Router#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#ip route 10.35.15.5 255.255.255.255 Serial0 5
Router(config)#end
Router#

In this case there is no ambiguity. You can reach only one other device through this serial interface, so the proxy ARP issues that we just described do not apply.

The ip route command in the second example affects any packet whose destination address is in the range from 172.16.0.1 to 172.16.255.254. These packets will be forwarded to the next-hop router, 10.35.6.1:

 Router(config)#ip route 172.16.0.0 255.255.0.0 10.35.6.1 2

The last number in this ip route command, 2, is the administrative distance for this route. The router will use this distance value to help it to decide between routes to the same destination prefix from different sources. For example, if you have more than one static route to the same destination, or if the router has learned another route to this destination via RIP, it will compare the administrative distances and use the route with the lowest distance value.

If there is no administrative distance value, as in the first example, the router uses a default value of 1.

The syntax for static routes specifies both an IP address and a netmask. This follows the standard rules for netmasks. However, it is useful to remember that the static route statement controls only how packets should be handled on this router. For example, suppose the range 172.16.0.0/16 includes the networks 172.16.1.0/24, 172.16.2.0/24, 172.16.5.4/30, and 172.16.5.8/30. If all the paths to all of these networks go through the router whose address is 10.35.6.1, then they can all be taken together with the same single route statement:

Router(config)#ip route 172.16.0.0 255.255.0.0 10.35.6.1 2

It is interesting to see what happens when you need to break up a range of addresses. Carrying on with the same example, suppose there is another network, 172.16.3.0/24, that is connected through a different next-hop router, 10.35.7.2. You can configure the router as follows:

Router#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#ip route 172.16.0.0 255.255.0.0 10.35.6.1 2
Router(config)#ip route 172.16.3.0 255.255.255.0 10.35.7.2 2
Router(config)#end
Router#

This may appear to cause a conflict, because 172.16.3.0/24 is contained within the range 172.16.0.0/16. However, the longest match rule that we discussed earlier in this chapter resolves the problem. Also note that the router will use the more specific route even if it has a higher administrative distance value. The distance values are used only when selecting between routes with the same mask length. For example, you could configure two static routes to the same destination:

Router#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#ip route 172.16.3.0 255.255.255.0 10.35.6.1 2
Router(config)#ip route 172.16.3.0 255.255.255.0 10.35.7.3 5
Router(config)#end
Router#

In this case, as long as the router can reach the better next-hop device (10.35.6.1), it will only use this line. The router will install the route with the higher distance only if it doesn't know how to reach the better next-hop device.

Note that this is a cumbersome and unreliable way of achieving automatic rerouting because it works only when the route to the next hop disappears, not when the next hop itself becomes unavailable. So, for example, if these two next-hop routers were connected through different physical interfaces and one of those interfaces went down, the router could switch to the router with the higher distance. But, if both devices were on the same directly connected Ethernet segment, this would not provide a failover. So, while this method is useful for some limited applications, it is generally better to use a dynamic routing protocol such as RIP, EIGRP, or OSPF. These protocols are described in later chapters.

The third example in this recipe uses the permanent keyword:

Router(config)#ip route 10.35.15.5 255.255.255.255 Ethernet0 permanent
Router(config)#ip route 172.16.0.0 255.255.0.0 10.35.6.1 2 permanent

Using the permanent keyword ensures that the static route always remains in the routing table, even if the next-hop interface is down. There is sometimes a danger that the dynamic routing protocol will install a route that you do not want to use, so it may be preferable to drop the packets rather than to use the dynamic route. For example, if you had a private link to another IP network and this link went down, you might not want your routers to try finding a path via the public Internet, even if one were advertised. This method is sort of the opposite of the floating static route given in Recipe 5.5.

The last example in this recipe uses routing tags:

Router(config)#ip route 172.16.0.0 255.255.0.0 10.35.6.1 2 tag 36291

Routing tags are used when redistributing from one routing protocol to another. They provide a convenient way to tell which routes came from what external protocols or networks. This concept is discussed in more detail in Chapter 6, Chapter 7, Chapter 8, and Chapter 9.

5.4.4 See Also

Recipe 5.5; Chapter 6; Chapter 7; Chapter 8; Chapter 9


  Previous section   Next section
Top