Networking / Beginners

Using the route Command

Using the route command displays or modifies the computer's routing table. For a typical computer that has a single network interface and is connected to a local area network (LAN) that has a router, the routing table is pretty simple and isn't often the source of network problems. Still, if you're having trouble accessing other computers or other networks, you can use the route command to make sure that a bad entry in the computer's routing table isn't the culprit.

For a computer with more than one interface and that's configured to work as a router, the routing table is often a major source of trouble. Setting up the routing table properly is a key part of configuring a router to work.

Displaying the routing table

To display the routing table (both IPv4 and IPv6) in Windows, use the route print command. In Unix/Linux, you can just use route without any command line switches. The output displayed by the Windows and Unix/Linux commands are similar. Here's an example from a typical Windows client computer:

C:\>route print
==================================================================
Interface List
 8 ...00 12 3f a7 17 ba ...... Intel(R) PRO/100 VE Network Connection
 1 ........................... Software Loopback Interface 1
 9 ...02 00 54 55 4e 01 ...... Teredo Tunneling Pseudo-Interface
10 ...00 00 00 00 00 00 00 e0 isatap.{D0F85930-01E2-402F-B0FC-
31DFF887F06F}
==================================================================
IPv4 Route Table
==================================================================
Active Routes:
Network  	 Netmask 	Gateway       Interface     Metric
Destination
0.0.0.0 	 0.0.0.0 	 192.168.1.1  192.168.1.110   276
127.0.0.0 	 255.0.0.0 	 On-link      127.0.0.1       306
127.0.0.1 	 255.255.255.255 On-link      127.0.0.1       306
127.255.255.255  255.255.255.255 On-link      127.0.0.1       306
192.168.1.0 	 255.255.255.0 	 On-link      192.168.1.110   276
192.168.1.110 	 255.255.255.255 On-link      192.168.1.110   276
192.168.1.255 	 255.255.255.255 On-link      192.168.1.110   276
224.0.0.0 	 240.0.0.0 	 On-link      127.0.0.1       306
224.0.0.0 	 240.0.0.0 	 On-link      192.168.1.110   276
255.255.255.255  255.255.255.255 On-link      127.0.0.1 306
255.255.255.255  255.255.255.255 On-link      192.168.1.110   276
=================================================================
Persistent Routes:
Network Address     Netmask 	Gateway Address   Metric
	0.0.0.0     0.0.0.0 	192.168.1.1 	  Default
=================================================================
IPv6 Route Table
=================================================================
Active Routes:
If Metric Network Destination 	Gateway
9 	18 ::/0 		On-link
1	306 ::1/128 		On-link
9 	18 2001::/32 		On-link
9 	266 2001:0:4136:e38c:2c6c:670:3f57:fe91/128
				On-link
8 	276 fe80::/64 		On-link
9 	266 fe80::/64 		On-link
10 	281 fe80::5efe:192.168.1.110/128
				On-link
8 	276 fe80::cca:9067:9427:a911/128
				On-link
9 	266 fe80::2c6c:670:3f57:fe91/128
				On-link
1 	306 ff00::/8 		On-link
9 	266 ff00::/8 		On-link
8 	276 ff00::/8 		On-link
=================================================================
Persistent Routes:
  None
C:\>

For each entry in the routing table, five items of information are listed:

  • The destination IP address Actually, this is the address of the destination subnet, and must be interpreted in the context of the subnet mask.
  • The subnet mask that must be applied to the destination address to determine the destination subnet
  • The IP address of the gateway to which traffic intended for the destination subnet will be sent
  • The IP address of the interface through which the traffic will be sent to the destination subnet
  • The metric, which indicates the number of hops required to reach destinations via the gateway

Each packet that's processed by the computer is evaluated against the rules in the routing table. If the packet's destination address matches the destination subnet for the rule, the packet is sent to the specified gateway via the specified network interface. If not, the next rule is applied.

The computer on which I ran the route command in this example is on a private 192.168.1.0 subnet. The computer's IP address is 192.168.1.100, and the default gateway is a router at 192.168.1.1.

Here's how the rules shown in this example are used. Notice that you have to read the entries from the bottom up:

  • The first rule is for packets sent to 255.255.255.255, with subnet mask 255.255.255.255. This special IP address is for broadcast packets. The rule specifies that these broadcast packets should be delivered to the local network interface (192.168.1.100).
  • The next rule is for packets sent to 192.168.1.255, again with subnet mask 255.255.255.255. These are also broadcast packets and are sent to the local network interface.
  • The next rule is for packets sent to 192.168.1.100, again with subnet mask 255.255.255.255. This is for packets that the computer is sending to itself via its own IP address. This rule specifies that these packets will be sent to the local loopback interface on 127.0.0.1.
  • The next rule is for packets sent to 192.168.1.0, with subnet mask 255.255.255.0. These are packets intended for the local subnet. They're sent to the subnet via the local interface at 192.169.1.100.
  • The next rule is for packets sent to the loopback address (127.0.0.1, subnet mask 255.0.0.0). These packets are sent straight through to the loopback interface, 127.0.0.1.
  • The last rule is for everything else. All IP addresses will match the destination IP address 0.0.0.0 with subnet mask 0.0.0.0 and will be sent to the default gateway router at 192.168.1.1 via the computer's network interface at 192.168.1.100.
Tip: One major difference between the Windows version of route and the Unix/ Linux version is the order in which they list the routing table. The Windows route command lists the table starting with the most general entry and works toward the most specific. The Unix/Linux version is the other way around: It starts with the most specific and works toward the more general. The Unix/Linux order makes more sense - the Windows route command displays the routing list upside down.

Modifying the routing table

Besides displaying the routing table, the route command also lets you modify it by adding, deleting, or changing entries.

Warning You shouldn't do this unless you know what you're doing. If you mess up the routing table, your computer may not be able to communicate with anyone.

The syntax for the route command for adding, deleting, or changing a route entry is

route [-p] command dest [mask subnet] gateway [-if interface]

The following list describes each of the route command's parameters:

  • -p: Makes the entry persistent. If you omit -p, the entry will be deleted the next time you reboot. (Use this only with add commands.)
  • command: Add, delete, or change.
  • dest: The IP address of the destination subnet.
  • mask subnet: The subnet mask. If you omit the subnet mask, the default is 255.255.255.255, meaning that the entry will apply only to a single host rather than a subnet. You usually want to include the mask.
  • gateway: The IP address of the gateway to which packets will be sent.
  • -if interface: The IP address of the interface through which packets will be sent. If your computer has only one network interface, you can omit this.

Suppose that your network has a second router that serves as a link to another private subnet, 192.168.2.0 (subnet mask 255.255.255.0). The interface on the local side of this router is at 192.168.1.200. To add a static route entry that sends packets intended for the 192.168.2.0 subnet to this router, use a command like this:

C:\>route -p add 192.168.2.0 mask 255.255.255.0 192.168.1.200

Now, suppose that you later change the IP address of the router to 192.168.1.222. You can update this route with the following command:

C:\>route change 192.168.2.0 mask 255.255.255.0 192.168.1.222

If you omit the mask from a route change command, the command changes the mask to 255.255.255.255!

Finally, suppose that you realize that setting up a second router on this network wasn't such a good idea after all, so you want to just delete the entry. The following command will do the trick:

C:\>route delete 192.168.2.0
[Previous] [Contents] [Next]