What is the correct way to configure DNS resolution for hostnames assigned IPs by DHCP

I’m running a Turris Omnia (OSv7.03). I would like to be able to resolve DNS for hosts on my local network, that have been assigned their IP addresses by DHCP. I know, there several other questions about this, but they’re all quite old. One of the more recent solution was “Enable DHCP clients in DNS”, which I did, and it didn’t work.

I think I have a way of doing this. But I’m trying to find the right way.

I’ve created a script, and put it at /usr/local/bin/update_dns_from_dhcp.sh with owner and group executable set.

#!/bin/sh

$ipaddr=`grep turris /etc/hosts | cut -d ' ' -f 1`
$control_socket=`pgrep kresd`
echo "hints.config(/tmp/dhcp.leases.dynamic); hints.add('turris.mylocalsubdomain ${ipaddr}')" > socat - UNIX:/tmp/kresd/control/${control_socket}

This script updates the kresd hits via the kresd control socket using kresd’s internal LUA interpreter, which can dynamically update many aspects of the kresd config without a restart. This specific incantation will update the ‘hints’ table with the contents of DHCP leases found in /tmp/dhcp.leases.dynamic, and also add a consistent hint to make the router itself resolvable by name.

Then, in /etc/config/dhcp, I’ve added the following line to the config dnsmasq section

        option dhcp_script /usr/local/bin/update_dns_from_dhcp.sh

This should cause the script to be run every time a lease is added or removed.

I say ‘should’ because I’m really not sure if it will work, as I can’t find any documentation as to what options translate from /etc/config/dhcp to dnsmasq’s actual configuration.

Also, there already exists /usr/lib/dnsmasq/dhcp-script.sh. But looking into it, it references an environment variable $USER_DHCPSCRIPT which it treats a a filename to be source. This seems like exactly what I want, except … Where do I set $USER_DHCPSCRIPT? Also, I’m not even sure this script is called ever by default, and I’ve no idea where to check a setting or config that would specify this.

1 Like

It works without issues just by checking the checkbox. No need to manually create files or edit config files.

The problem, however, could be in the client computers. For example Ubuntu by default refuses to resolve these hostnames. You can try the guide from here: networking - systemd-resolved does not query dns server for local domain - Ask Ubuntu .

1 Like

It didn’t work until I made my changes. I’m checking from a Mac, and from an ubuntu machine. On the ubuntu machine, queries with a domain name didn’t work. They do after my modifications. On the mac, both bare hostname and hostname.domain lookups now work.

Are there other configuration options that are expected to be left at the default for this to work?

Resolving the bare hostnames isn’t done at the server side. (IIRC you’d run into issues if a computer named itself com, for example.) But DHCP should ensure that you get search lan into /etc/resolv.conf normally (or whatever domain you configure), so that the clients automatically try appending that .domain.

I struggled with this a lot and since years. I tried multiple approaches, including writing my own scripts and involving real authoritative DNS servers receiving the updates over nsupdate or dyndns, as well as hot-plug and many more.
At the end nothing really worked, especially in larger networks with many clients, since over half of the clients submit either no data or unusable data.
At the end, the only thing which really works all the time, is fixed IP reservation and setting hostnames in DNS to those fixed IPs yourself.
But also then, you have to disable privacy features like MAC address randomization on some clients (Android and iphone).
I have given up.