11

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?

5
  • Nope. Interestingly, it can poll /etc/resolv.conf for changes, but not /etc/hosts. You could use an inotify to trigger SIGHUP or service reload. Sep 18, 2015 at 12:43
  • The thing is that in my project, the hosts will be changed like a few times per minute. This will make dnsmasq restart a few times per minute. Is this a very bad problem?
    – roshkattu
    Sep 18, 2015 at 12:44
  • 1
    Consider a "proper" DNS server with a database backend that takes live changes. Sep 18, 2015 at 16:10
  • @AaronCopley which one, for example?
    – Groosha
    Nov 8, 2017 at 15:37
  • If you really want changes that frequent, wouldn't you be better of using dhcp? dnsmasq will happily (and automatically) serve hostnames received that way
    – Kees-Jan
    Dec 9, 2017 at 9:39

1 Answer 1

13

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.

1
  • To respond to the latter comment... it is by lack of design - he was too lazy to implement just a diff... let's see who gets around to it first... if it turns out to be necessary.
    – Dagelf
    Dec 28, 2018 at 2:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .