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.

Join the PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

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

Alastair Cousins

PC World
Show Comments

Most Popular Reviews

Latest News Articles

Resources

PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?