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 1.0.0.1 to
126.255.255.254 and support 16 million hosts on each of 127
networks.
Class B networks have an address range  between  128.1.0.1 to
191.255.255.254 and support 65,000 hosts on each of 16,000
networks.
Class C networks have an address range between  192.0.1.1 to
223.255.254.254 and support 254 hosts on each  of 2 million
networks.
Class D networks have an address range between  224.0.0.0 to
239.255.255.255 and are reserved for multicast groups.
Class E  networks have an address range between  240.0.0.0 to
254.255.255.254 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 192.168.1.0-255 range.

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

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

$ vi /etc/sysctl.conf
Change the line
net.ipv4.ip_forward = 0
to
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

#!/bin/bash

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

# MASQ the full 192.168.1.0/24 network
/sbin/ipchains -A forward -s 192.168.1.0/24 -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

#!/bin/bash

# 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
/etc/rc.d/init.d/rc.fw
 
 

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. 192.168.1.1. 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

2 comments


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

gravatar
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


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

I'll surely post more now :)

Regards
Amar

(link)





------------------------


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:

Linux

Networking



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode





SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)
www.SCOsales.com