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!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Alastair Cousins

PC World
Show Comments

Brand Post

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Maryellen Rose George

Brother PT-P750W

It’s useful for office tasks as well as pragmatic labelling of equipment and storage – just don’t get too excited and label everything in sight!

Cathy Giles

Brother MFC-L8900CDW

The Brother MFC-L8900CDW is an absolute stand out. I struggle to fault it.

Luke Hill

MSI GT75 TITAN

I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it.

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?