October 4, 2004

CLI Magic: Route

Continuing the theme from last week, when we looked at the ifconfig command, this week it's all about route. Not route in the sense a recent president has used the word for dealing with evil-doers, but the Linux command for displaying, creating, and modifying network routes. So wipe your shoes and get out of that GUI for a bit. With an understanding of both the ifconfig and route commands, you can work real networking magic.If you're new to Linux, you might be thinking to yourself, "Well, that's great. But what the heck are network routes?" Relax, you're in the right place. And the answer is easy. A network route is the path packets sent from your computer follows to get someplace else. Note that there are often more than one route. It all depends upon the destination, and the contents of your routing table.

The Routing Table

A route begins with a network interface. From there, it may proceed to the gateway, which delivers it to a new network with its own routing table, and from there to wherever it needs to go to reach its destination.

A routing table is used to keep the rules for what to do with each outgoing packet. To get a picture of what routes are configured on your machine, enter su mode and type the route command from the CLI.

On my system, it looks like this:


Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     *               255.255.255.0   U     0      0        0 eth0
link-local      *               255.255.0.0     U     0      0        0 eth0
loopback        *               255.0.0.0       U     0      0        0 lo
default         192.168.0.1     0.0.0.0         UG    0      0        0 eth0

The Destination column is compared with the destination IP address in the TCP/IP packet to determine how to route a particular packet. If it's necessary for the packet to leave your local network -- the LAN -- in order to get where it's going, a Gateway address will be shown. That's where the packet will be sent first on its trip. On a home LAN, there is usually only one Gateway: your router. If no Gateway is required to complete the routing, an asterisk will be shown instead of an address.

The Genmask indicates which of the four (assuming IPV4) octets of the destination IP address are actually needed to do the routing. On the first row in the table, where the destination is shown as 192.168.0.0 -- that's a private LAN address by the way, not addressable from the Internet -- the Genmask is shown as 255.255.255.0. That means only the 4th quad of the address is needed for addressability. In other words, if I was trying to send a packet to my laptop in the living room, whose IP address is 192.168.0.11, only the .11 would be needed to get it there.

The Flags column show the status of the route. Common flags used include U, H, and G. U indicates the route is up. H indicates the destination is a host as opposed to a network, and G means a Gateway is used.

The Metric column shows the number (or a guess at the number) of gateways that a packet will have to transit in order to reach the destination. Note that there isn't any way of knowing that for the default rule, because the address could be anywhere on the Internet. I'm really not sure the information in this column is useful or needed.

Let's ignore the Ref and Use columns and move along to the last column: the iface, which shows the network interface used in the route. Most often, home users will have only one interface to worry about, a NIC or a modem. But if your box is acting as a standalone firewall, or if you have a laptop with a NIC, a modem, and a wireless card, you may have multiple possibilities for the interface.

In my routing table above, we can see that any packets destined for an address on my LAN's subnet (192.168.0.0) are controlled by the first route. That route has no Gateway, and a Genmask which indicates only the last octet (the zero in the 255.255.255.0) is needed for the address,

Looking at my routing table, we can see that all traffic destined for elsewhere on the LAN (any address beginning with 192.168.0) has no gateway specified, and is sent on eth0. By definition, no gateway is needed because gateways join networks and the destination is on our LAN network.

Ignoring the link-local and loopback entries for now, we can also see that there is a default route which is also sent on eth0. But unlike the LAN traffic, it has a Gateway specified. It goes directly to 192.168.0.1, which just happens to be my router.

Rather than provide examples here of how to use the route command to add, delete or modify routes for networks or for specific machines, I'm simply going to refer you to the man pages for route. It contains a number of examples of commonly used functionality.

So what's the point?

Why come all this way and not give specific examples? Good question. A couple of careful readers have written to inform me that both ifconfig and route are getting a little long in the tooth. The new game in town is the ip command. As one guy (thanks, Stephen!) put it: "The true linux types use the iproute2 commands. ip replaces ifconfig, route and ss replaces netstat."

With our columns on ifconfig and route, we've laid a good foundation for understanding home networking tools. In a future piece, we'll look at how to manipulate things with the new generation of tools.

Click Here!