4

I have configured dnsmasq to dish out 10.0.0.* addresses to hosts on an ethernet LAN. The hosts on this network request static IP addresses using DHCP from the dnsmasq server running on 10.0.0.1. They also have wifi access to a router in order to access the internet. E.g. host on 10.0.0.2 has these entries in /etc/dhcpcd.conf:

### Wifi-router
interface wlan0
static ip_address=192.168.1.202
static routers=192.168.1.1

### Ethernet Switch
interface eth0
static ip_address=10.0.0.2
static domain_name_servers=10.0.0.1 ### Where dnsmasq runs

Now, I'd like dnsmasq to automatically act as a DNS server for these hosts so they can communicate to each other by name. E.g. if host at 10.0.0.2 has hostname 'node2', then one would be able to ssh from any host in this network to 10.0.0.2 with e.g. ssh user@node2.

My question is, how can I configure dnsmasq to cache the name of each host automatically? I know you can add entries to /etc/hosts on the machine where dnsmasq runs and apply the expand-hosts option in /etc/dnsmasq.conf, but I don't want to maintain such an additional list -- I'd prefer to just set the hostname on each individual host and let that be the SSOT on how they are named. How can I set that up?

Here is my /etc/dnsmasq.conf file.

interface=eth0
listen-address=127.0.0.1
dhcp-range=10.0.0.0,10.0.0.10,12h
bind-interfaces
domain-needed
bogus-priv
expand-hosts

### Upstream DNS servers
server=8.8.8.8
server=8.8.4.4

1 Answer 1

4

The problem is that you are specifying a static address on the client. From man 5 dhcpcd.conf:

             Configures a static value.  If you set ip_address then dhcpcd
             will not attempt to obtain a lease and will just use the value
             for the address with an infinite lease time.

Thus dnsmasq never receives a DHCPREQUEST and is never aware of the client's existence.

There are two options here:

  • Assign fixed addresses from the DHCP server. dnsmasq allows you to match on MAC address, client ID, or other custom tags - MAC address is typically used. You'd add a line such as:

    dhcp-host=00:53:00:11:22:33:10.0.0.2
    

    and remove your static address on the client side.

  • Or, use the dhcpcd request or inform option instead, which:

         request [address]
                 Request the address in the DHCP DISCOVER message.  There is no
                 guarantee this is the address the DHCP server will actually give.
                 If no address is given then the first address currently assigned
                 to the interface is used.
    
         inform [address[/cidr[/broadcast_address]]]
                 Behaves like request as above, but sends a DHCP INFORM instead of
                 DISCOVER/REQUEST.  This does not get a lease as such, just
                 notifies the DHCP server of the address in use.  You should also
                 include the optional cidr network number in case the address is
                 not already configured on the interface.  dhcpcd remains running
                 and pretends it has an infinite lease.  dhcpcd will not de-
                 configure the interface when it exits.  If dhcpcd fails to
                 contact a DHCP server then it returns a failure instead of
                 falling back on IPv4LL.
    

Generally, I'd recommeng assigning static addresses from the server, as it minimises the chance of clashes. If you merely have a preferred address and don't mind it changing if it's already in use, using request from the client might be alright. inform should generally be considered dangerous, as it risks clashes.

1
  • This is super useful info. I had mistakenly believed that settings static ip_address=xxx meant that dhcpcd would send out a request to a dhcp-server and iff it were free would it be assigned that address. I now understand this syntax to mean that the device is simply assigning itself that address, regardless of other devices (hence, this option is prone to clashes). I tried following your suggestion to switch this to request [ip_address] on my hosts. Unfortunately, I'm now getting this problem where dnsmasq asigns ALL of my hosts the same address (10.0.0.2) :(
    – Magnus
    Commented Oct 13, 2019 at 21:46

You must log in to answer this question.

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