5.11 Configuring Routes

5.11 Configuring Routes

Routing is the act of transferring packets from one host or subnet to another. Let's say that you have two LAN subnets, and, and a Linux router machine with two Ethernet cards, one connected to each subnet. The router has two IP addresses: for eth0 and for eth1. Figure 5-4 shows the two networks; the router's routing table looks like this (obtained by running route -n):

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface   U     0      0        0 eth0   U     0      0        0 eth1
Click To expand
Figure 5-4: Two subnets joined with a router.

Furthermore, let's say that the hosts on each subnet have the router as their default gateway ( for and for Therefore, if wanted to send a packet to anything outside of, it would pass the packet to Now let's say that you want to send a packet from to The packet goes to (the router) via its eth0 interface, and now you want it to go back out through the router's eth1 interface. To make the Linux kernel perform this basic routing function, the only thing you need to do is enable IP forwarding on the router with the following command:

echo 1 > /proc/sys/net/ipv4/ip_forward

This is easy enough, but what if you have another subnet,, connected to the host at on that host's second network interface, as shown in Figure 5-5? After configuring's second Ethernet interface to, you now need to figure out how everything else in and can talk to Let's start with the router that connects and

Click To expand
Figure 5-5: Three subnets.

You can tell the router that handles with this command:

route add -net netmask gw

The routing table on the router now looks like this:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface   UG    0      0        0 eth0   U     0      0        0 eth0   U     0      0        0 eth1

As an added bonus, recall that all traffic from initially goes to the router, because is the default router on that subnet. Therefore, anything on can now talk to, and vice versa (as long as you set the default route for the hosts on to But what about

Technically, this also works now, because the packets go to (eth0 on the router), then back out the same network interface to This is inefficient and a bit slower, of course, because the packets to must go across the same wire twice, with the router handling the packet between the transmissions. If you want to "fix" this, you must run a route command similar to the one above for each host on

Say that the router has a connection to the Internet, and that this is the router's default gateway. Theoretically, there's no problem in sending packets out of your network to the rest of the Internet. Unfortunately, if your IP addresses are in private networks (as in this section) you run into the same problem described in Section 5.10.2 — you will never get anything back. Again, you need to run NAT (see Section 5.14) or do some other trick to get everything within the network talking to the outside world.