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 -