Setting up a quick and dirty gateway on GNU/Linux

It’s occasionally useful to set up your GNU/Linux system to function as an Internet gateway for other machines. In particular, when installing a new operating system on a laptop, the only network in range may be an encrypted WLAN for which the installer does not have drivers. In such a case, connect the laptop (the client) to another machine (the gateway) via an ethernet cable and follow this guide. I will assume that the client and gateway are connected to each other through the eth0 interface, and that the gateway is connected to the Internet through wlan0.

Finally, note that I’m not much of a system administrator. Don’t use a setup this crude for anything but ad-hoc short-term purposes.

Gateway setup

Run this script on the gateway as root. It sets up the actual forwarding and NATing in the kernel. You will need to have the iptable_nat kernel module loaded.

Then statically assign the gateway an IP adress on the eth0 interface:

# ifconfig eth0

Client setup

First, statically assign an IP adress by running the following command on the client.

# ifconfig eth0

You should be able to ping the gateway now. Try it with ping

Now we must add the IP address of the gateway to the kernel routing table.

# route add default gw

Finally, you will probably want to be able to perform DNS lookups on the client, so you should add the IP address of Googles free DNS server to the file /etc/resolv.conf.

# echo nameserver > /etc/resolv.conf

And that’s it.

DHCP server

The above is a pretty crude setup, in particular because you have to manually setup the client. Sometimes, you may not be able to interact with the client (it may have no keyboard, or screen). Yet still, most machines have DHCP clients attached to their network interfaces, so if you run a DHCP server on the gateway, you’ll be able to give it a known IP address. Install the program dhcpd and create an /etc/dhcpd.conf file containing:

subnet netmask {

This will give an IP address in the interval to to anyone making a request. Run the DHCP server as so:

# dhcpd -d -f eth0

This will run the DHCP server in the foreground and send logging output to standard error, so you’ll be able to tell which IP address is given to the client.