Tuesday, May 08, 2012

VirtualBox DHCP Nameserver Configuration on OS X

I recently maxed out my desktop with all the memory it could theoretically take. € 100 poorer, and 16 GB richer, I thought the best way to make use of all this excess memory would be to run virtual machines on it, and maybe set up a virtual cluster, and install some cloud-savvy software on it, like ElasticSearch, and watch the fun.

So I set out to install Ubuntu on VirtualBox. I chose VirtualBox over VMWare because it is fully open source, and I have found open source software to be typically of higher quality than free, closed source solutions at these "low" levels of innovation (i.e., technologies that don't affect end-users all that much, like web servers, and programming languages).

The installation process is dead simple, and is documented numerous times on the internet. I wanted simple internet connectivity on my guest OS, just browsing the internet. Unfortunately, it didn't work "out of the box."

Problem: Internet doesn't work on a freshly installed Ubuntu guest on an OS X host.

Solution: As it turns out, VirtualBox will take its resolv.conf(5) from OS X's resolv.conf(5), if it finds one. I had toyed with mine when setting up my router, and so VirtualBox picked it up and shoved it into Ubuntu.

This is useless, since VirtualBox guest machines exist in a virtual network, inside a 10.0.xxx subnet, behind a virtual router set up by VirtualBox. So the imported resolv.conf from OS X means your guest OS pretends to be a real computer on your network, when it absolutely isn't.

That's fine, that can be fixed. Just fix interfaces(5) to contain the appropriate directives to manipulate the resolv.conf(5) (resolv.conf(5) is now a derived file, so changing the file directly is pointless, you have to change the upstream configuration files from which it derives, in this case interfaces(5)).

So the relevant part of your interfaces(5) will look something like:

iface eth0 inet dhcp

Now it's just:

$ sudo ifdown eth0 && sudo ifup eth0

And you should be in good order.

But now even though you're connected, you won't be able to resolve FQDNs:

$ ping google.com
ping: unknown host google.com

To achieve this, you need to turn on VirtualBox's built-in nameserver.

$ VBoxManage modifyvm "octavius-v" --natdnshostresolver1 on


Discussion: For some reason, VirtualBox comes with this turned off. I'm sure it's a good reason, but I just don't know what that reason is. Finding this information, I found to be non-trivial. It's not in the user manual, and Google'ing for it yielded slim pickings, until I lucked out.

Another thing to note is that VBoxManage(1) doesn't come with an informative man page.

Also, the syntax for these switches that can be flipped is not explained. --parameter1 refers to the parameter for guest machine 1. So, apparently, --natdnshostresolver2 refers to your second guest machine, and so on and so forth.


About Me

My photo
I write essays in my spare time on things that are important to me. The ones that I feel are any good, or make any sense, I put them up here. :)