VMware ESXi: Setting up NAT with a linux virtual machine


other than the Workstation, VMware ESXi does not has the functionality to “NATting” virtual machines network traffic into a public network.

My approach to implement NAT is a virtual machine with a slimmed-down (Debian) Linux.

These virtual machine has two network interfaces. The first interface is connected to a vSwitch called “Public LAN” with access to the public LAN, the second interface is connected to a vSwitch which has no physical adapter attached to and is called “NAT Network”. On the NAT interface a DHCP server is assigning IP Addresses.

Before we can start we have something to define, in braces are the values for my configuration example:

  • IP Parameters for the Public LAN Interface (IP Address:, Subnet Mask:, Default Gateway:
  • An IP  Address Range for the NAT Clients (Range:, Subnetmask:
  • IP Address, from the Range of “NAT Clients”, for the NAT Interface (IP Address:, Subnet Mask:
  • Locate DNS Servers in your network (DNS 1:, DNS 2:
  • If you want to deliver a “primary DNS suffix” to the NAT Clients, identify or define one (Primary DNS suffix: natclients.local)

Create a new virtual machine with 1 CPU and 1GB RAM. This should be enough. Add tw0 e1000 network interfaces. Connect NIC 1 to the “Public LAN” and NIC 2 to the “NAT Network” and install debian with a minimum set of packages (“Standard system utilities” and “ssh-server”)

Minimum Debian package installation
Minimum Debian package installation

Login as User root. Disable respectively remove some system daemon which are not required:

root@debdev:/# update-rc.d -f exim4 remove
root@debdev:/# update-rc.d –f rpcbind  remove
root@debdev:/# update-rc.d -f nfs-common remove

Install the ISC DHCP Server

root@debdev:/# apt-get update
root@debdev:/# apt-get install isc-dhcp-server

Bind the dhcp server only to the interface which is connected to the “NAT Network”, is this example eth1. Edit the file /etc/default/isc-dhcp-server and alter the INTERFACES parameter to


Create a config file /etc/dhcp/dhcpd.conf for the dhcp server.

option domain-name "natclients.local";
option domain-name-servers,;
option ntp-servers;
max-lease-time 7200; # 2 hours
default-lease-time 1800;
ddns-update-style none;
subnet netmask {
    option routers;
    option broadcast-address;

Enable NAT by the following command

root@debdev:/# iptables -t nat -A POSTROUTING -m iprange --src-range -o eth0 -j MASQUERADE

In order to use NAT you have to enable IP forwarding. Therefore some addional firewall rules are necessary to prevent that someone in the “Public LAN” Subnet can send IP packets directly to the NAT Clients. Only outgoing connections from the NAT Clients are allowed.

root@debdev:/# iptables -A FORWARD -i eth1 -o eth0 -s -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
root@debdev:/# iptables -A FORWARD -i eth0 -o eth1 -d -m state --state RELATED,ESTABLISHED -j ACCEPT
root@debdev:/# iptables -P FORWARD DROP

Also drop all dhcp request at the “Public LAN” interface.

root@debdev:/# iptables -A INPUT -i eth0 -p udp --dport 67 -j DROP

Save all the rules to a file

root@debdev:/# iptables-save > /etc/firewall.conf

To load all rules at system startup create a file /etc/network/if-up.d/iptables

iptables-restore < /etc/firewall.conf

and make it executable

root@debdev:/# chmod +x /etc/network/if-up.d/iptables

Finally enable ip forwarding. Edit /etc/sysctl.conf. Locate net.ipv4.ip_forward, remove the comment hash and set the option to "1".


Reboot your virtual machine.


Advertisment to support michlstechblog.info

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.