-->
Previous Table of Contents Next


Returning to the Burwell network, the following is an example from the laptop linux2, with a SLIP link up and running:


$ /sbin/route

Kernel routing table

Destination    Gateway       Genmask         Flags Metric Ref Use Iface

slip.burwell.c *             255.255.255.255 UH    0      0   0   sl0

127.0.0.0      *             255.0.0.0       U     0      0   100 lo

default        slip.burwell.c  *             UG    0      0   1   sl0

The table entry for the loopback is the same as before, and there are two new entries. The first specifies a route to slip.burwell.com. The other new entry specifies a default route by using slip.burwell.com as a gateway.


NOTE:  Every machine connected to a network must have a default route in its routing table. The default route is used when no other routing table entry matches the destination for a packet.

Adding Static Routes

You add routes to the routing table by running the route program with the add argument. The command-line argument syntax for the route add command is


route add [ -net | -host ] addr [gw gateway ] [metric cost]

[netmask mask] [dev device]

Table 24.5 describes the command-line arguments that the route add command uses.

Table 24.5 Command-Line Arguments Used by route add

Argument Description

-net | -host Forces the specified address to be treated as a network or host address.
addr The destination address for the new route. This can be an IP address, host name, or network name.
gw gateway Specifies that any packets for this address be routed through the specified gateway.
metric cost Sets the metric field in the routing table.
netmask mask Specifies the netmask of the route being added. The route program will guess what this is, so you don’t need to specify it under normal circumstances.
dev device Forces route to associate the new route with the specified network interface device. Again, route usually guesses correctly what device to use for the new route, so you don’t have to use this often.


CAUTION:  
When adding a gateway route to the routing table, you must make sure that the specified gateway is reachable. You usually have to add a static route for the gateway before adding the route by using the gateway.

Looking at Routing Examples

Now for some examples, starting with the loopback interface. After configuring the loopback interface with ifconfig, you need to add a route to it, as in the following:


# route add 127.0.0.1

Nothing else is needed because route compares the address given to it with the addresses for the known interfaces and assigns the loopback interface to the new route. The following example shows how to set the routing for the SLIP link on the Burwell linux2 machine after the SLIP link is established and ifconfig is used to activate the interface:


# route add slip.burwell.com

# route add default gw slip.burwell.com

The first command adds a static route for the host slip.burwell.com; the second one tells the kernel to use slip.burwell.com as a gateway for all packets with unknown destinations.


CAUTION:  
Make sure that any host names you use with the route command are in the /etc/hosts file so that route can find the IP addresses for them; otherwise, route fails.

If you’re subnetting your network by splitting the IP address in the middle of an octet, you’ll have to specify the required netmask when running route. For example, if you have a class C network and have four subnets using the first two bits of the last octet, you need to run route like this:


# route add hostname netmask 255.255.255.192

This ensures that route puts the right netmask in the routing table entry.

For Ethernet and other broadcast network interfaces, you need to add routes that tell the kernel what network can be reached via each configured interface. After using ifconfig to bring up the eth0 network interface on linux1.burwell.com as you did previously, you need to run route to install the route to the network on that interface:


# route add -net 166.82.1.0

That may not look like enough to set the routing table entry correctly, because no interface is indicated; however, route manages to find the interface by comparing the IP address on the command line to the IP address of each network interface. It assigns the route to the interface that matches it. In this case, eth0 has been assigned the address 166.82.1.21 with a netmask of 255.255.255.0. This matches the network address given in the route command, so route installs a route to the network 166.82.1.0 by using interface eth0, as follows:


$ route

Kernel routing table

Destination     Gateway      Genmask         Flags Metric Ref   UseIface

166.82.1.0      *            255.255.255.0   UN    0      0     0 eth0

127.0.0.0       *            255.0.0.0       U     0      0     100 lo

To tell linux1 how to reach the other subnet, you need two more routing table entries to be safe:


# route add gateway.burwell.com

# route add -net 166.82.2.0 gw gateway.burwell.com

This adds a static route to gateway.burwell.com and then adds a network route for 166.82.2.0 by using gateway.burwell.com as the gateway for that network, as shown in the following:


$ route

Kernel routing table

Destination     Gateway         Genmask          Flags Metric Ref UseIface

gateway.burwell *               255.255.255.0    UH    0      0    0 eth0

166.82.1.0      *               255.255.255.0    UN    0      0    0 eth0

166.82.2.0      gateway.burwell 255.255.255.0    UN    0      0    0 eth0

127.0.0.0       *               255.0.0.0        U     0      0    100 lo

This shows the static route you added for gateway.burwell.com and the gatewayed route to the 166.82.2.0 network.

Deleting Routes with the route Command

You delete routes by calling route with the del option and specifying the destination address of the route you want to delete. For example,


# route del -net 166.82.2.0

deletes the network route for network 166.82.2.0.


Previous Table of Contents Next