How to share internet via Linux box to the private networks? {Tested OK}

Let's consider the linux machine is having two interfaces eth0 and eth1. eth1 is connected to the internet and eth0 is connected to the private network. How can i make internet accessible via the clients/servers residing in the private networks? in this article i'll give the steps to achieve the same in Ubuntu.

Step1: Enable the ipv4 traffic forwarding

 echo 1 > /proc/sys/net/ipv4/ip_forward

To enable permanently, add following line in /etc/sysctl.conf

net.ipv4.ip_forward = 1


Step2:  Add following rules to IP Tables

sudo iptables -A FORWARD -o eth1 -i eth0 -m conntrack --ctstate -NEW -j ACCEPT
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -s [ Private network eth0 belong to ex:10.2.0.0/16 ] -o eth1 -j MASQUERADE 

Step3: Save IP Tables 

sudo iptables-save | sudo tee /etc/iptables.sav
sudo iptables-restore < /etc/iptables.sav

Step4: Update the DNS server as 8.8.8.8 in private network nodes

Add 'nameserver 8.8.8.8' in /etc/resolv.conf

Step5: Ping from private network node to Internet 

ping google.com

It should be successful.







Comments