Gateway setup:
eth0 = the network adapter with internet (IP: 10.10.10.2)
eth1 = the network adapter with client machine (IP: 192.168.20.1)
The local network card cannot have the same subnet with the internet adapter.
Now, configure the internal network card for a static IP address as you need.
Configure the NAT (Network Address Translation):
Basically, here I’ll be configuring the iptables for NAT translation so that packets can be routed through the gateway.
$ sudo iptables -A FORWARD -o eth0 -i eth1 -s 192.168.20.0/24 -m conntrack --ctstate NEW -j ACCEPT $ sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT $ sudo iptables -A POSTROUTING -t nat -j MASQUERADE
These are the iptables rules.
Rule 1: It allows the packets being forwarded
Rule 2: Allows forwarding of established connection packets
Rule 3: It does the NAT
iptables rule doesn’t save by default. It has to be saved manually.
$ sudo iptables-save | sudo tee /etc/iptables.sav
Edit the /etc/rc.local and add the following lines so that it calls that file every time when the gateway machine is booted.
iptables-restore < /etc/iptables.sav
Run the following line
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Uncomment the following line from /etc/sysctl.conf
net.ipv4.ip_forward=1
Client setup:
eth0 = the network adapter with the gateway pc (IP: 192.168.20.2)
Change the gateway to the host machines IP address.
To configure DNS server edit the /etc/resolv.conf file and add ISP’s DNS servers.
nameserver xx.xx.xx.xx
And Boom!!
