6.2. Setting a Default Gateway

You're a bit confused on the concepts of gateways and default gateways. When do you need them? What are they for? How do you configure them?

Gateways forward traffic between different networks, like different subnets, or your local network and the Internet. Another way to think of them is next hop routers. The default gateway contains the default route out of your network. Any host that is allowed access outside of the local network needs a default gateway.

Suppose your network is set up like this:

You'll need to configure two gateways: from your individual LAN hosts to your router, and then from your router to your ISP. Figure 6-1 illustrates this network configuration.

There are several different ways of configuring gateways on your LAN hosts. One way is with route:

	# route add default gw 10.10.0.25

Another way is with iproute2:

	# ip route add default via 10.10.0.25

If your PC has more than one network interface, you can specify a single one:

	# route add default gw 10.10.0.25 eth2
	# ip route add default via 10.10.0.25 dev eth2

But, these will not survive a reboot. Debian users have /etc/network/interfaces for permanent network configurations. For hosts with static IP addresses, add a gateway line to your interface stanzas:

	gateway 10.10.0.25

Fedora users have individual configuration files for each interface in /etc/sysconfig/network-scripts, like ifcfg-eth0:

	gateway 10.10.0.25

Your router then needs a gateway 208.201.239.1 statement in the configuration for its WAN interface to get Internet access.

Use these commands to remove gateways:

	# route del default
	# route del default gw 10.10.0.25
	# ip route del default
	# ip route del default via 10.10.0.25

ip will not let you set more than one default gateway, which route will let you do. There can be only one.

Gateways cannot have addresses outside of their own networks. The example used in this recipe demonstrates this—the WAN interface, 208.201.239.36, is on the same network as the ISP, 208.201.239.1. The LAN gateway interface is on the LAN network.

How do you decide which route to make your default gateway? By the number of routes it serves. Your Internet gateway leads you to hundreds of thousands of routes, while you're going to have just a few local routes.

Using route or ip is great for testing because you can set up and tear down routes as fast as you can type.

Computers do not need routes or default gateways to access other hosts in their own subnet. You can test this easily by deleting your default gateway and running some ping tests.

Any hosts that need access outside their own subnet must have default gateways. A computer may have many routes, but it can have only one default gateway. This keeps your routing tables manageable because then you don't need routes for every possible destination.

TCP/IP routing can be thought of as a series of hops. You'll see the term next hop a lot. All it means is any router only needs to know the next router to forward packets to. It doesn't have to know how to get all the way to the final destination.

The word gateway encompasses a number of meanings. It's the entrance to a network, and it's a translator between different protocols or codecs. In the olden days, you would have needed a gateway between incompatible networking protocols like Token Ring, IPX/SPX, and Ethernet. TCP/IP and Ethernet are pretty much it these days, and most computers support multiple protocols. Voice over IP often requires transcoding of various VoIP protocols, so we have specialized media gateways to do this.

  • Chapter 4 to learn how to configure DHCP and DNS using dnsmasq

  • man 8 ip

  • man 8 route