Virtualbox VM with two network cards

A common use case for a virtual machine is that you want to run a server application on your machine locally; because of privacy/security reasons or just for testing purposes.

In this scenario it makes sense to set up the VM with two network cards. One adapter ‘attached to’ the virtual NAT and the other one as ‘host-only adapter’ connected to a virtual net.

The VM will access the internet through the virtual NAT and at the same time will be accessible from the host at a specified address.

  • Create a virtual network in Virtualbox’s ‘Host network manager’, e.g. vboxnet0 with IP range 192.168.1.1/24.

  • In the virtual machine: Set the primary network adapter to NAT, or if you want the machine to directly talk to your network’s router to ‘Bridged’.

  • Add a second network adapter; set to host-only on vboxnet0

Start up the VM.

  • In the VM: Get the name of the second network adapter with ‘ip a’. This is probably something like enp0s8.

  • Setup the network card:

On debian based systems edit /etc/network/interfaces and add:

allow-hotplug enp0s8
	iface enp0s8 inet static           
	address 192.168.1.2/24

(for a statically assigned IP address; alternatively use DHCP if you like)

On redhat based system edit /etc/sysconfig/network-scripts/ifcfg-enp0s8 and add:

ONBOOT=yes
IPADDR=192.168.1.2
NETMASK=255.255.255.0

Reload the network

systemctl restart network

Check that you can still access the general internet in the VM. Check that you can reach your VM from the host on 192.168.1.2.