Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book


When two or more local area networks are connected, they use a gateway. A gateway is a machine that acts as the connection between the two networks, routing data between the two based on the IP address of the destination machine. You have to make some changes to the network configuration files whenever your local machine is going to use a gateway, as well as if your machine is going to act as a gateway.

To use the services of another machine as a gateway, you have to tell the routing tables about the gateway and the networks that it connects to. The simplest use of a gateway is one used to connect to the rest of the world, such as the Internet. This is configured with the route command like this:

route add default gw net_gate

net_gate is the name of the machine on your local area network that acts as the gateway. The gateway machine follows the keyword gw in the route command. The use of the word default in the command indicates that the kernel's routing table should assume that all networks can be reached through that gateway.

If you want to configure a gateway to another local area network, the name of that network should be in the /etc/networks file. For example, if you have a gateway machine called gate_serv that leads from your own local area network to a neighboring network called big_corp (and an entry exists in the /etc/networks file for big_corp with its network IP address), you could configure the routing tables on your local machine to use gate_serv to access big_corp machines with this command:

route add big_corp gw gate_serv

An entry should be made on the remote network's routing table to reflect your network's address; otherwise, you would only be able to send data and not receive it.

If you want to set up your local machine to act as a gateway itself, you need to configure the two network connections that your machine is joining. This usually requires two network boards, PPP connections, or SLIP connections in some combination. Assume your machine is going to act as a simple gateway between two networks called small_net and big_net, and you have two Ethernet cards installed in your machine. You configure both Ethernet interfaces separately with their respective network IP addresses (for example, your machine might have an IP address on big_net of, whereas on small_net it might have the IP address

You should add the two network addresses to your /etc/hosts file to simplify network name resolution. For the networks and IP addresses mentioned, you will have the following two entries in the /etc/hosts file:        merlin.big_net.com merlin-iface1        merlin.small_net.com merlin-iface2

This example shows the fully qualified domain names in the /etc/hosts file (this example assumes the machine has the name merlin on both networks, which is perfectly legal). You can also add shorter forms of the name, as well (such as merlin, merlin.big_net, and so on). Finally, the interface names have been included for convenience (so merlin-iface1 is the first interface on merlin, and merlin-iface2 is the second).

You then use the ifconfig commands to set up the connections between the interface and the names used in the /etc/hosts file:

ifconfig eth0 merlin-iface1
ifconfig eth1 merlin-iface2

These commands assume that the Ethernet device /dev/eth0 is for the interface to big_net and /dev/eth1 is for small_net.

Finally, the kernel routing table must be updated to reflect the two network names. The commands for this example are shown here:

route add big_net
route add small_net

When these steps are completed, you can use your machine as a gateway between the two networks. Other machines on either network can also use your machine as a gateway between the two networks.

  • + Share This
  • 🔖 Save To Your Account