Configure NetworkManager's dnsmasq to use /etc/hosts

Solution 1:

To speed up internet, ubuntu 12.04 has added a plugin to NetworkManager to start dnsmasq, a local dns server that caches dns entries. The problem is the plugin has hardcoded the --no-hosts string.

So one solution is to comment out the line that load the plugin in the NetworkManager config file and restart NetworkManager :

sudo mv /etc/NetworkManager/NetworkManager.conf /etc/NetworkManager/NetworkManager.conf.bak
sudo bash -c 'cat /etc/NetworkManager/NetworkManager.conf.bak | sed -e "s/^\(dns=dnsmasq\)$/#\1/" > /etc/NetworkManager/NetworkManager.conf'
sudo restart network-manager

Another solution is to wrap dnsmasq to filter out the undesired arguments:

sudo mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq.bin
sudo bash -c 'cat > /usr/sbin/dnsmasq' << EOF
#!/bin/sh
dnsmasq=/usr/sbin/dnsmasq.bin

exec $dnsmasq `echo $@ | sed -e s/--no-hosts//`
EOF

sudo chmod 755 /usr/sbin/dnsmasq

Please mark the bug as affecting you.

Another solution without patching system files

cat /etc/NetworkManager/dnsmasq.d/hosts.conf 

addn-hosts=/etc/hosts

Solution 2:

This bug still affected me even now ( Ubuntu 14.04 ).

Finally I found a solution that, simply add this line 'addn-hosts=/etc/hosts' to dnsmasq configuration file of Newworkmanager package.

echo 'addn-hosts=/etc/hosts' > /etc/NetworkManager/dnsmasq.d/etc-hosts
service network-manager restart

The idea is, we are adding /etc/hosts as a additional host file .

Even if I could find a solution, hard-coded option '--no-hosts' in the binary file /usr/sbin/NetworkManager disappoints me.

Solution 3:

As of August 2015, the other answers are outdated.

Simple answer

  1. Create /etc/NetworkManager/dnsmasq.d/hosts.conf.
  2. Put lines like address=/whatever/1.2.3.4 in it. See the docs (look for --address). Wildcards are possible: address/.whatever./1.2.3.4.
  3. Kill dnsmasq (bug).
  4. Restart it: $ service network-manager restart.

Solution 4:

dnsmasq should automatically use the /etc/hosts file. This can be disabled by the -h command line option or no-hosts configuration option. I would not expect either to be set in the default configuration.

Try forcing dnsmasq to reload its hosts file. (Changes to the configuration file require a restart). Either of these commands should work.

service dnsmasq reload

kill -HUP $(pidof dnsmasq)

If you are working with a system that has no-hosts specified you should be able to use the addn-hosts option to override it. Normally, this would be used to read an additional file in /etc/hosts format. This can be used to specify additional host data that you want DNS to provide, but don't want in your /etc/hosts file. This can be used to allow the package manager and related tools to manage /etc/hosts while additional hosts data is provided in an alternate file.