Resolving VM hostnames with dnsmasq

dnsmasq is a simple and easy to configure DNS and DHCP server. It’s perfect for setting up DNS and DHCP for your VirtualBox host-only network that will allow you to resolve the hostnames of your guests.

This makes it much easier to set up networks of co-operating VMs to replicate a production environment. Once set up as described below, you will be able to reference any guest by its hostname (normally assigned during OS setup) from any other guest or from the OS X host without fiddling with static IP addresses and /etc/hosts files.

In this setup, dnsmasq runs on the VirtualBox host (OS X) and is used by the host as a DNS server and guests as a DHCP and DNS server over a VirtualBox host-only network. Our domain names managed by dnsmasq will all end in “.vm”, so a VirtualBox guest with hostname “myhost” will be accessible under the name “myhost.vm”.

First, you need to disable the VirtualBox internal DHCP server. Since it doesn’t support the features we need, we are going to replace it with dnsmasq. In Preferences -> Network double-click your host-only network to bring up the configuration dialog. Take note of the IPv4 address and network mask, you’ll need these later to configure dnsmasq. Switch to the DHCP Server tab and make sure Enable Server is unticked and kill the VirtualBox DHCP server:

sudo pkill VBoxNetDHCP

You will also need to disable OS X Internet Sharing if this is enabled because it also seems to start a DHCP server.

Install dnsmasq from homebrew:

brew install dnsmasq

Create the file /usr/local/etc/dnsmasq.conf with contents as follows:


# Set this to the name of your VirtualBox host-only interface.
# dnsmasq will listen for DHCP and DNS requests on this interface as
# well as the loopback:
interface=vboxnet0
# This option will be forced anyway on OS X so set here to avoid a warning:
bind-interfaces
# Enable the integrated DHCP server, you need
# to supply the range of addresses available for lease and optionally
# a lease time. If you have more than one network, you will need to
# repeat this for each network on which you want to supply DHCP
# service.
# Select a range from the address and netmask shown in VirtualBox's
# host-only network configuration dialog:
dhcp-range=192.168.56.102,192.168.56.200,12h
# The default directory does not exist on OSX and this cause an
# error when enabling DHCP, set an alternative location here:
dhcp-leasefile=/var/lib/dnsmasq.leases
# Assume anything in the .vm domain is local
local=/vm/
# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
expand-hosts
# Set the domain for dnsmasq. this is optional, but if it is set, it
# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
# as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
# domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"
domain=vm
# This must be the address of you host on the vboxnet0 network
# find this in the VirtualBox host-only network preferences
address=/vbox-host.vm/192.168.56.1

view raw

dnsmasq.conf

hosted with ❤ by GitHub

Configure dnsmasq to be started automatically:

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Now we create a configuration file on the host that tells OS X to use dnsmasq to resolve names ending in “.vm”.

echo 'nameserver 127.0.0.1' | sudo tee /etc/resolver/vm

Next we configure the guest. For this example, we will assume a hostname of “web-1” – this is normally assigned during OS setup and (on Ubuntu at least) can be changed by editing /etc/hostname and restarting. To configure the guest to use host-only networking, in the VirtualBox manager right-click on the guest and choose Settings -> Network and change Attached to: to Host-only Adapter. Make sure the Name setting just below that is vboxnet0. Now start up the guest.

Provided that your guest is set to configure the host-only interface with DHCP (the default on most systems), it should pick up an IP address in the range you allocated as dhcp-range in the above configuration. It should also pick up dnsmasq on the host as its DNS server and register the name “web-1.vm” with dnsmasq.

You should now be able to resolve “web-1.vm” from the host or from any other similarly configured guest. You should also be able to resolve the host as “vbox-host.vm” from any of the guests.

From the host:

ping web-1.vm
ssh web-1.vm ping vbox-host.vm

If this doesn’t work, dig is your friend. From the host try:

dig @localhost web-1.vm

Once this is all set up, any new guests you create on the vboxnet0 network should be accessible to all other guests and the host by their hostnames without any additional configuration.

5 thoughts on “Resolving VM hostnames with dnsmasq

  1. I’m close to getting this working. I’m running dnsmasq on my mac. In Virtual Box, I have vboxnet0 set to 11.1.1.1. And my vm is definitely getting its IP from from dnsmasq; Its IP is in the range I specified.

    But if I ping mydomain.vm, it resolves to 11.1.1.1 instead of the vm’s IP. Any idea what I could be doing wrong?

    Thanks for the post!

  2. You share interesting things here. I think that your page can go viral easily, but you must give it initial
    boost and i know how to do it, just type in google for –
    mundillo traffic increase go viral

  3. centos - Domain wird mit DNSMASQ zu einer falschen IP aufgelöst

  4. DNSMASQ를 사용하여 잘못된 IP로 도메인 해결 내가 asterisk.vm에 - IT HOW

Leave a comment