Share an internet connection to a local network

Required: One PC with two network cards, one connecting to the outer world and the other to the inner world. Assuming that the eth0 interface goes out and the eth1 interface goes in, the internal network card can be configured by editing the interfaces file:

sudo nano /etc/network/interfaces
auto eth1
iface eth1 inet static
#this will be the gateway adress for the clients connected to this network
gateway 'real_world_ip_address_of_eth0'
dns-nameservers 'some_dns_servers'

Then, the IP forwarding settings must be defined; via command line, using iptables, we have to:

sudo ip addr add dev eth1


sudo iptables -A FORWARD -o eth0 -i eth1 -s -m conntrack --ctstate NEW -j ACCEPT 
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

sudo iptables-save | sudo tee /etc/iptables.sav
echo "iptables-restore < /etc/iptables.sav" > /etc/rc.local
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo sed "s/\#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/" -i

Reboot and it’s done.

On the client, the /etc/network/interfaces file should be edited to allow the clients to connect to the otter world via the above configured machine:

iface eth0 inet static
#this is the address of the internal card defined above
 dns-nameservers 'some_dns_servers'

Reboot and it’s done. The server can also be configured to assign IP addresses on connection via DHCP.