Dnsmasq doesn’t automatically reload when entry is added to /etc/hosts

dnsmasq

I am struggling for a few days to configure dnsmasq to automatically reload or take into knowledge the new hosts added to /etc/hosts or to another configured file /etc/hosts.dnsmasq.

Is this even possible?

Best Answer

There are two ways to cause dnsmasq to reload a hosts file:

  1. As Aaron Copley noted in his comment, send SIGHUP to dnsmasq. From the man page:

    When it receives a SIGHUP, dnsmasq clears its cache and then re-loads /etc/hosts and /etc/ethers and any file given by --dhcp-hostsfile, --dhcp-hostsdir, --dhcp-optsfile, --dhcp-optsdir, --addn-hosts or --hostsdir. The dhcp lease change script is called for all existing DHCP leases. If --no-poll is set SIGHUP also re-reads /etc/resolv.conf. SIGHUP does NOT re-read the configuration file.

    Note that dnsmasq doesn't restart in this case, but it does re-read a number of other files (and calls the dhcp lease change script for all existing DHCP leases). If triggering reloads too quickly is a concern, you can debounce the signal.

  2. Use the --hostsdir option. Again from the man page:

    --hostsdir=<path>
            Read all the hosts files contained in the directory. New or changed files are read automatically. See --dhcp-hostsdir for details.

    For reference, here is the documentation for --dhcp-hostsdir:

    --dhcp-hostsdir=<path>
            This is equivalent to dhcp-hostsfile, except for the following. The path MUST be a directory, and not an individual file. Changed or new files within the directory are read automatically, without the need to send SIGHUP. If a file is deleted or changed after it has been read by dnsmasq, then the host record it contained will remain until dnsmasq receives a SIGHUP, or is restarted; ie host records are only added dynamically.

    This has a few advantages compared to the first option: dnsmasq will re-read the host file(s) automatically, no SIGHUP required, and; only the host files are reloaded, no other actions are taken.

    One potential disadvantage is that, as the documentation for --dhcp-hostsdir points out, new host entries are dynamically added but deleted or changed entries are not updated. Simon Kelley, the author of dnsmasq, has confirmed that this is by design.

Related Topic