APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Internet Sharing using a linux box

© February 2005 amarjyoti

Author: amarjyoti
Date: Tue Feb 22 10:54:48 2005 Internet Sharing using a linux box

The computer world was similar to all of us having an apple each till some time back. And then the wise men from the industry made networks. Ideas started flowing all over and soon came in the internet. The internet has been the best thing that has happened to the computer world so far. It has created a platform where we can share our ideas.

Since the Internet is a large network composed of smaller networks, it made sense to break the address space into smaller chunks. Network classes enable us to break down this address space. In IPv4 the various classes of networks are -

Class A networks have an address range  between to and support 16 million hosts on each of 127
Class B networks have an address range  between to and support 65,000 hosts on each of 16,000
Class C networks have an address range between to and support 254 hosts on each  of 2 million
Class D networks have an address range between to and are reserved for multicast groups.
Class E  networks have an address range between to and are reserved for the future

Such a system makes things simple and networks are manageable. However, they cannot communicate with each other. The scenario is very similar to how communities grew around the world. With time people from different communities needed to communicate with each other, however language was the barrier. The solution they found was people who understand both the languages i.e. translators. So if you need to talk to a beautiful French girl, all you need to do is get somebody who understands French and can translate English to French and vice versa. Routers /gateways do the same in computer networks. Different networks communicate with each other using routers.

A router allows hosts that are not on the same logical network, like an IP subnet, to communicate with each other. The router receives packets (chunks of data) on an interface and routes them to where they need to go based on a routing table; the table allows the router to have knowledge of where a given logical network is located.

Most offices and homes have small class C private networks. These networks need to communicate with the servers in the internet. The only logical way for them is to use a gateway/router. Linux has routing functionality in the kernel itself which makes it an ideal choice for as routing box.

One simple way of sharing the internet connection using linux is using the IP forwarding feature of the kernel and network address translation (NAT). For NATing one can use either ipchains or iptables. It is assumed that the private network is in the range.

Let us take the example of a simple network. The server is used to connect to the internet. It's IP address is There are 4 workstations They are connected to the server via the switch. All the workstations share the internet through

The first step is to enable ipforwarding in the kernel of the server (

$ vi /etc/sysctl.conf
Change the line
net.ipv4.ip_forward = 0
net.ipv4.ip_forward = 1
> This would enable ip forwarding.

Then we need to get the server to NAT which can be done via ipchains or iptables.

If ipchains is used, create a file called rc.fw and add following lines

$ vi /etc/rc.d/init.d/rc.fw


# First Load the ipchains kernel module. Required only if ipchains is compiled as a module.
/sbin/modprobe ipchains
insmod ipchains

# MASQ the full network
/sbin/ipchains -A forward -s -j MASQ

# List the rules
/sbin/ipchains -L -n

If iptables is used instead of ipchains, create a file called rc.fw and add the following lines -

$ vi /etc/rc.d/init.d/rc.fw


# First Load the ipchains kernel module . Required only if iptables is compiled as a module
/sbin/modprobe iptables
insmod iptables

# MASQ the full  network
/sbin/iptables -t nat -A POSTROUTING -j MASQUERADE

# List the rules
/sbin/iptables -L

This would look after all the entire data traffic from the local network to the internet.

Finally add this to the startup -

$ vi /etc/rc.local
Add the line

This ensures that the settings are retained after a reboot also.

After rebooting the server would do all the routing functions for the entire network.

On the client side all that is required is to set the default router/gateway as the server's IP i.e. The entire network is now set to share the internet connection.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Internet Sharing using a linux box


Inexpensive and informative Apple related e-books:

Take control of Apple TV, Second Edition

Take Control of Apple Mail, Third Edition

Take Control of High Sierra

Take Control of IOS 11

Take Control of Numbers

More Articles by © amarjyoti

Tue Feb 22 12:50:21 2005: 59   TonyLawrence

amarjyoti had the honor of being the first to try out the new article posting system, and I see it still has a few bugs. The article did manage to get posted though, so that's the most important part.

Sat Dec 10 09:24:58 2005: 1427   anonymous

I feel very happy see my article on your site. :)

I'll surely post more now :)




Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

If debugging is the process of removing bugs, then programming must be the process of putting them in. (Edsger W. Dijkstra)

Linux posts

Troubleshooting posts

This post tagged:



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode