5.5 Configuring a Default Gateway

5.5 Configuring a Default Gateway

In the previous section, you saw how to manually configure an Ethernet network interface with a particular address and mask. You can connect to networks outside your subnet by adding a default gateway to your kernel's IP routing table with this command:

route add default gw gw-address

The gw-address parameter is the IP address of your default gateway; it must be an address in a locally connected subnet defined by the address and mask settings of your network interface (as described in the previous section). If you have a static IP address, a default route setting usually goes along with an ifconfig command in your boot sequence. For other kinds of connections, other programs usually set the default route.

To view the current default route, run this command:

route -n

The output should look something like this:

Kernel IP routing table
Destination     Gateway        Genmask        Flags Metric Ref   Use Iface  U     0      0       0 eth0        UG    0      0       0 eth0

The ifconfig command that configured the eth0 network interface in this example added the first route line. However, the last line is the default gateway, because it has a netmask of, which allows the host to reach all other hosts on the Internet. The gateway's IP address is, and you can see that you reach that gateway through the eth0 network interface.

If you mistype a gateway address when adding a route, you cannot attach another gateway until you delete the erroneous entry. To remove the current default gateway, run this command:

route del -net default

Figure 5-1 on the next page shows a typical local area network with a gateway, or router. The subnet is with a default gateway of Each IP address represents a network interface, as does the uplink IP on the gateway. If you do not run the gateway, you do not need to worry about its uplink — for example, if you are configuring a host with an IP address of, you need only know that is the local IP address on the gateway.

Click To expand
Figure 5-1: A typical local area network with an Internet link.

If your netmask is, your router is probably at node 1 of the subnet (for example, in This is convention, not necessarily reality.

A properly configured default gateway allows you to connect to the rest of the world. However, you still need to know how to convert common names, or hostnames, to IP addresses.