Wednesday, April 11, 2018

How can I setup a Linux NAT Router in Ubuntu?

Steps:

1. Enable packet forwarding for IPv4 between NICs:

  • Open and edit /etc/sysctl.conf
  • Ucomment net.ipv4.ip_forward=1
  • To execute command sysctl -p
2. Create a postrouting rule within iptables:

  • iptables -t nat -A POSTROUTING -o enp0s8 -s 192.168.1.0/24 -j MASQUERADE
  • Where enp0s8 is WAN and 192.168.1.0/24 is LAN, please modify those parameters depend on your environment.
3. Save and restore iptables rule after reboot:

  • Save rules: iptables-save > /etc/iptables.save
  • Restore rules:
  • Open and edit /etc/network/interfaces to add pre-up.....

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto enp0s3
iface enp0s3 inet static
address 192.168.1.254
netmask 255.255.255.0
pre-up iptables-restore < /etc/iptables.save

    Done~