Not long ago, I found myself in need of a subnet of virtual machines. Using VirtualBox I configured a number of VMs with Internal Network interfaces and a gateway VM with two interfaces — one Internal Network interface and one NAT interface.

It isn’t the first time I needed to configure a Linux box as a gateway, but I always forget how to configure IP masquerading. So, needless to say I’m taking note of it here:

1. Configure the external interface:

  $ ifconfig eth0 netmask 
  $ echo "nameserver" > /etc/resolv.conf
  # or for dhcp
  $ dhclient eth0

2. Configure the internal interface:

  $ ifconfig eth1 netmask

3. Configure iptables:

  $ iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

4. Enable ip_forwarding:

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

or configure it in /etc/sysctl.conf, which will persist after reboot.

Obviously, depending on your distro, your interface configuration, etcetera, etcetera, you will have to adjust.