Saturday, July 14, 2012

The Best Ubuntu NAT Tutorial

It is my goal here to wade through all the BS and get straight to things that work. I looked at numerous tutorials and spent a few hours on this, but I can confidently say I know the right method to get simple NAT working with ubuntu Server.

Assuming both the NAT router and the ubuntu host have been installed and updated, we have to set the network configuration on the NAT router. Edit /etc/network/interfaces to reflect below.

(eth0 is your WAN connection to ISP and eth1 is your LAN connection to your hosts)

auto eth0
iface eth0 inet dhcp 
pre-up iptables-restore < /etc/iptables.rules

auto eth1
iface eth1 inet static

The pre-up line restores the iptables rules you define in /etc/iptables.rules upon reboot. Otherwise you will lose them and NAT will cease to function.

Edit /etc/sysctl.conf and uncomment:

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 -A POSTROUTING -t nat -j MASQUERADE
The first rule allows forwarded packets (initial ones). The second rule allows forwarding of established connection packets (and those related to ones that started). The third rule does the NAT. 
Next we copy the working iptable NAT rule to the file that will be run when eth0 comes up.
sh -c "iptables-save > /etc/iptables.rules"


On the client side I just edited /etc/network/interfaces and gave eth0 an IP address
on the same subnet as eth1 on the NAT router. You can use DHCP if you like. This was a test. I will be posting my entire configuration once I get this working as my main router. I just did it in vmware workstation for now.


No comments:

Post a Comment