I've found that when I'm testing tools on virtual machines that I've run into problems with host name resolution. While /etc/hosts works fine for many things, some tools (like Sitespeed.io) don't respect the entries there, and bomb out when trying to do DNS lookups on my vm network.
Dnsmasq to the rescue! It's pretty simple to set up, and while you can configure it to do things like DHCP and TFTP booting, all I want is to serve up DNS. The first step, of course, is to install it via your favourite package manager. I use CentOS, so I would use yum:
yum install dnsmasq
Once that's installed, a bit of configuration is all you need. All the configuration that we'll do takes place in /etc/dnsmasq.conf. Here's my configuration first, followed by some explanation.
/etc/dnsmasq.conf:
domain-needed
bogus-priv
domain=example.vm
expand-hosts
local=/example.vm/
listen-address=127.0.0.1
#listen-address=192.168.56.16
bind-interfaces
server=10.0.2.3
#server=8.8.8.8
First, we set a couple of network behaviour settings. domain-needed prevents plain names (those without a dot, or attached domain) from leaving your network, like if you searched for example instead of example.com. bogus-priv keeps addresses in non-routed space from being forwarded out to the Internet.
Next, we set the private domain name that you intend to use for your VM network with domain. I used example.vm here. expand-hosts instructs dnsmasq to automatically add this domain added to simple domains you've defined.
Now we can tell dnsmasq what addresses to listen on, with listen-address. In this configuration, I've left it as only listening to requests coming in on the interface bound to localhost, since I only needed it for one particular serve By adding additional lines, with the IP address assigned to other interfaces, you to provide custom DNS service for all of your VMs. bind-interfaces prevents dnsmasq from listening on other interfaces and just throwing away requests that it shouldn't really be listening to.
Finally, the server directive provides other name servers upstream, to forward DNS requests that can't be fulfilled by your local entries. This, again, can have multiple lines. I've set it here to use the DNS server provided by VirtualBox, but I could also set it to my ISP's server, Google's public DNS, or the OpenDNS servers.
I can hear you asking, though, where do we set the DNS names that we need? The beauty of dnsmasq is that it will read the entries you need out of /etc/hosts! Make sure that you include a line in there for localhost (127.0.0.1 localhost).
In order to make sure that you actually are accessing this, make sure to add a line to your /etc/resolv.conf file:
/etc/resolv.conf:
nameserver 127.0.0.1
Don't forget to enable and start the service in systemd. You'll probably need to update some configuration somewhere to make sure that this sticks, since resolv.conf is generally written automatically; this is left as an exercise for the reader (since I'm nearly done this article, and don't want to go figure it out at the moment. I'll update when I do.)