When running virtual machines under a Linux host system for testing web apps in various browsers (e.g. Internet Explorer), I found it rather tedious having to continually tweak the hosts file within each VM for the purpose of adding entries pointing back to the host machine's development web server address.
Instead the steps below will setup Dnsmasq on a Ubuntu 14.04LTS or 12.04LTS host machine for the purpose of serving both it's own DNS queries and that of virtual machine guests. Dnsmasq will parse the /etc/hosts
file on your host machine where we will keep a single set of DNS entires to our test web application(s).
Ubuntu 12.04LTS and above by default runs it's own instance of Dnsmasq via the NetworkManager service for all DNS queries, but sadly it's configured to ignore /etc/hosts
and to only listen on the loopback interface 127.0.1.1
. By moving the Dnsmasq binary to an alternative filename and creating our own dnsmasq
bash script we can correct this behaviour.
Idea has been taken from a post at the Ask Ubuntu StackOverflow website. Bash scripts are slightly different between 14.04/12.04.
$ sudo mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq.bin
$ sudo curl \
-o /usr/sbin/dnsmasq \
https://gist.githubusercontent.com/magnetikonline/6236150/raw/dnsmasq.1404.sh
$ sudo chmod a+x /usr/sbin/dnsmasq
$ sudo restart network-manager
$ sudo mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq.bin
$ sudo curl \
-o /usr/sbin/dnsmasq
https://gist.githubusercontent.com/magnetikonline/6236150/raw/dnsmasq.1204.sh
$ sudo chmod a+x /usr/sbin/dnsmasq
$ sudo restart network-manager
Alternatively, we can stop NetworkManager running Dnsmasq entirely and install our own managed instance of Dnsmasq. Whilst this does work, I have found it a little flakey on system startup - Dnsmasq starts too early, before NetworkManager can do it's setup resulting in no DNS until Dnsmasq is restarted - so would suggest the above method as the better option.
$ sudo cp /etc/NetworkManager/NetworkManager.conf /etc/NetworkManager/NetworkManager.conf.pkg
$ sudo cat /etc/NetworkManager/NetworkManager.conf \
| sed -e 's/dns=dnsmasq/#dns=dnsmasq/' \
> /etc/NetworkManager/NetworkManager.conf
$ sudo restart network-manager
$ sudo apt-get install dnsmasq
$ sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.pkg
$ sudo wget \
https://gist.github.com/magnetikonline/6236150/raw/dnsmasq.conf \
-O /etc/dnsmasq.conf
$ sudo /etc/init.d/dnsmasq restart
Your host machine will now be running it's own configured instance of Dnsmasq, not one managed via NetworkManager.
- Modify guest virtual machine to use host machine's IP address as it's primary DNS server see note.
- Add required DNS entries to your hosts
/etc/hosts
file. - Restart Dnsmasq to reload
/etc/hosts
and add entries to it's own local DNS lookup list:- Method 1:
sudo restart network-manager
- Method 2:
sudo /etc/init.d/dnsmasq restart
- Method 1:
- Guest virtual machines will now be able to resolve DNS via the running Dnsmasq instance for both world-wide and
/etc/hosts
entries.
YMMV, but with regards to Windows VMs I find it only necessary to change primary DNS server details to the local host Dnsmasq listening IP for Windows XP OSes.
Windows 7/8 are able to correctly configure the correct DNS server (e.g. host Dnsmasq instance) automatically upon boot when using VirtualBox with NAT networking.