VirtualBox networking for web development

16th November 2012 (12 years ago)

At work our development machines have quite a large stack of software including search services, database servers and other tools that are required to run the site. I like to upgrade my desktop linux distribution more regularly than we would upgrade servers, however this can cause problems with the web stack required for development. For this reason I decided to run the web stack as a VirtualBox virtual machine but there are a few networking scenarios that needed the be accounted for.

  • I needed to be able to access the internet from the guest VM to download package files etc.
  • I needed to be able to communicate directly between the host and guest VM even if there was no network available due to travelling outside the office.

This blog post was helpful in explaining the various networking types. The approach that suited my needs was to create two network adaptors - one for a host-only network and one for NAT.

First of all you need to create a host only network that will allow the host and guest to communicate independently of any physical networking hardware. This is set up independently of of the virtual machine. In File > Preferences add a host-only network under the Network section. I also enabled the DHCP server as it makes configuration easier. By default this network is called vboxnet0. Then in the network settings for your VM select Host-only Network for the Attached to value under the Adapter 1 tab. You will need to select the name of the host-only network we created as the Name.

Now under the Adapter 2 tab the type under Attached to should be NAT. This allows the guest VM to get an IP address from the network router (when available) and connect to the wider internet. Bridged networking is another option instead of NAT, but I wouldn't recommend it unless you know you will always be connected to wired Ethernet. Theoretically, some combinations of routers and wireless chipsets allow more than one MAC address to an interface but I've never managed to get it to work.

If you now boot the VM you will be able to tell if you have been given IP addresses for both of the adapters using ifconfig -a. You can try pinging the host from the guest to check the host-only network works. Note that the host address you want to ping is not the host's main IP but the one that was in the host-only network details in VirtualBox's general settings. You can also find this IP by running ifconfig on the host as there will be a new fake adaptor created by VirtualBox called something like vboxnet0. You should also try pinging the guest VM from the host with the IP that has been allocated to the guest in the same subnet. If the second NAT adapter is working on the guest you will be able to ping a domain on the internet.

At this stage, always ping IP addresses rather than domain names as it is possible you will need to configure DNS nameservers separately.

If you haven't been given IPs for both of your adaptors check you have DHCP enabled for the host-only network and make sure you have lines like this in /etc/network/interfaces then restart networking with something like sudo /etc/init.d/networking restart.

auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet dhcp

I hope this helps out some people that would like to achieve a similar setup.