An Open Access Peon

24 April 2012

VirtualBox guest with Host-Only Networking and NAT

Follow-up: the much easier way to allow external access plus access to the VM from the host is to use two Network devices. Set the first network device to be "NAT" and the second to "Host only adapter".

The Host-Only networking in VirtualBox 4.1 allows the host/guest to talk to each other but requires some extra steps to allow the guest to access the outside world. We can use NAT on the host machine to NAT the virtual network through the host's real interface.

Configure the VirtualBox guest's network to use Host-Only Networking:

You will need to set up static networking on the guest O/S because the VirtualBox DHCP server won't assign a gateway or DNS servers. Replace with the guest's IP address and modify the DNS to your local network settings:

# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 #iface eth0 inet dhcp iface eth0 inet static address netmask network broadcast gateway dns-search your-domain.example dns-nameservers

On the Host machine edit /etc/default/ufw:


Uncomment net/ipv4/ip_forward and net/ipv6/conf/default/forwarding in /etc/ufw/sysctl.conf:

net/ipv4/ip_forward=1 net/ipv6/conf/default/forwarding=1

Add masquerading rules to the top of /etc/ufw/before.rules:

# NAT table rules *nat :POSTROUTING ACCEPT [0:0] # Forward traffic from eth1 through eth0. -A POSTROUTING -s -o eth0 -j MASQUERADE # commit the NAT rules COMMIT

(Re)enable the ufw:

sudo ufw disable sudo ufw enable