Sharing an Internet connection using Linux

Do you have more than one computer but only a single Internet connection? With Internet Connection Sharing it is possible to use one Internet connection on all of your computers at once, seamlessly.

Internet Connection Sharing under Linux is known as either IP Masquerading or NAT (Network Address Translation). Setting this up is not very difficult, but it does require that you already have a working LAN set up. There is one other complexity: the 2.2.x and 2.4.x series of kernels use different tools to set up Internet Connection Sharing. In this column we will go through both methods.

The first step is to discover which series of kernel your system is running.

To do this, in a shell type:

$ uname -r

This code returns;

2.4.17

From this I can tell that my kernel is 2.4.17, and thus part of the 2.4.x series of kernels.

2.2.X SERIES KERNELS

The 2.2.x series of kernels use a tool called IP Chains to set up Internet Connection Sharing. The latest version of this tool is included on this month's cover CD. If you compiled your own kernel, you will need to enable some options; if you use a kernel provided by your distribution, you don't need to worry about this. The options to be enabled are:

CONFIG_FIREWALL

CONFIG_IP_FIREWALL

Once the kernel is configured correctly, create the following script in a text editor:

#!/bin/sh

# set up IP Masquerading

/sbin/ipchains -P forward DENY

/sbin/ipchains -A forward -j MASQ -s 192.168.1.0/24 -d 0.0.0.0/0
# enable forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward

and save this script as:

/etc/rc.d/rc.ipmasq

Note in this script that the local network is assumed to have the IP range of 192.168.1.0 - 192.168.1.255. Change this address to suit the IPs used by the computers on your LAN. The address should end in 0/24. Finally, make the script executable:

$ chmod 700 /etc/rc.d/rc.ipmasq

You can now run this script to start Internet Connection Sharing immediately, or when you reboot it should run automatically.

2.4.X. SERIES KERNELS

The 2.4.x series of kernels use a tool named IP Tables to set up Internet Connection Sharing. The latest version of IP Tables is included on the cover CD, as is the source code for the latest 2.4.x series kernel. If you compiled your own kernel, you will need to enable some options; if you use a kernel provided by your distribution, you don't need to worry about this. The options are:

CONFIG_NETFILTER

CONFIG_IP_NF_CONNTRACK

CONFIG_IP_NF_IPTABLES

CONFIG_IP_NF_NAT

CONFIG_IP_NF_TARGET_MASQUERADE

Once the kernel is configured correctly, create the following script in a text editor:

#!/bin/sh

# flush any old rules

/sbin/iptables -F -t nat

# turn on NAT

/sbin/iptables -A POSTROUTING -t nat -o ppp0 -j MASQUERADE#

enable forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward

Save this script as:

/etc/rc.d/rc.ipmasq

Note that in this script it is assumed that a modem Internet connection is being shared. If you want to share another type of connection such as cable Internet, change "ppp0" to the device in your system that represents this connection, such as "eth0". This device can be found by typing:

$ /sbin/ifconfig -a

Finally, make the script executable:

$ chmod 700 /etc/rc.d/rc.ipmasq

TESTING YOUR SHARED CONNECTION

By now you should have Internet Connection Sharing working on your gateway. Each computer that you want to access the Internet will need to be configured to access this gateway. Under Windows, the gateway can be set under Network in the Control Panel. You simply need to enter the gateway computer's IP address, and the IP addresses of your ISP's DNS servers. If you have another Linux computer on your LAN, the gateway can be set on this by typing as root:

$ route add default gw <gateway ip>

The Internet should now be accessible from any computer on your LAN without a hitch.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Alastair Cousins

PC World

Comments

Comments are now closed.

Most Popular Reviews

Follow Us

Best Deals on GoodGearGuide

Shopping.com

Latest News Articles

Resources

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Latest Jobs

Shopping.com

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?