Using Dnsmasq with Ubuntu for VM web application testing
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 16.04LTS, 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).
- Method 1
- Method 2
- Virtual machine & host entry setup
Ubuntu 12.04LTS and above by default runs their own instance of Dnsmasq via the NetworkManager service, 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 16.04-14.04/12.04.
Ubuntu 16.04LTS / 14.04LTS
$ sudo mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq.bin $ sudo curl \ -o /usr/sbin/dnsmasq \ https://gist.githubusercontent.com/magnetikonline/6236150/raw/dnsmasq.16.04-14.04.sh $ sudo chmod a+x /usr/sbin/dnsmasq # update ETH_INTERFACE to your network interface name # restart dnsmasq... # for Ubuntu 16.04LTS $ sudo systemctl restart NetworkManager # for Ubuntu 14.04LTS $ 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.12.04.sh $ sudo chmod a+x /usr/sbin/dnsmasq # restart 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 1 as the better solution.
Stop NetworkManager running it's own Dnsmasq
$ sudo cp /etc/NetworkManager/NetworkManager.conf /etc/NetworkManager/NetworkManager.conf.pkg $ sudo cat /etc/NetworkManager/NetworkManager.conf \ | sed "s/dns=dnsmasq/#dns=dnsmasq/" \ >/etc/NetworkManager/NetworkManager.conf $ sudo restart network-manager
Install Dnsmasq and config
$ 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.
Virtual machine & host entry setup
- 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
- Restart Dnsmasq to reload
/etc/hostsand 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
Note regarding guest VM DNS server change and VirtualBox
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.