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 newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Alastair Cousins

PC World
Show Comments

Cool Tech

SanDisk MicroSDXC™ for Nintendo® Switch™

Learn more >

Breitling Superocean Heritage Chronographe 44

Learn more >

Toys for Boys

Family Friendly

Panasonic 4K UHD Blu-Ray Player and Full HD Recorder with Netflix - UBT1GL-K

Learn more >

Stocking Stuffer

Razer DeathAdder Expert Ergonomic Gaming Mouse

Learn more >

Christmas Gift Guide

Click for more ›

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Walid Mikhael

Brother QL-820NWB Professional Label Printer

It’s easy to set up, it’s compact and quiet when printing and to top if off, the print quality is excellent. This is hands down the best printer I’ve used for printing labels.

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?