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
address 192.168.0.1
#this will be the gateway adress for the clients connected to this network
netmask 255.255.255.0
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 192.168.0.1/24 dev eth1

 

sudo iptables -A FORWARD -o eth0 -i eth1 -s 192.168.0.0/24 -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
 address 192.168.0.101
 gateway 192.168.0.1
#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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s