[Dnsmasq-discuss] Automatic nameserver configuration

Kjell Rönnmark Kjell.Ronnmark at space.umu.se
Fri Aug 15 10:05:09 UTC 2025


Dear Dnsmasq users,

The description of how to update upstream nameservers dynamically is not 
very useful for several popular Linux distributions that have moved to 
systemd-networkd and netplan. The DHCP client in systemd-networkd seems 
well hidden.

To implement dynamic updates I wrote a small script "DNS-fix":

#!/bin/bash
# After testing this script can be copied to
# /etc/networkd-dispatcher/routable.d/
#
# set the name of the device connected to internet with DHCP (cf. ip a)
device=WAN
FILE=/etc/dnsmasq-resolv.conf


echo -e "## Don't edit this file, it will be overwritten by "  > $FILE
echo -e "## /etc/networkd-dispatcher/routable.d/DNS-fix"  >> $FILE
echo -e "## when the system is booted.\n"  >> $FILE

serv=`netplan ip leases $device |grep "DNS="`

set $serv
if [ $# -ge 2 ]; then
   serv1=`echo $1|sed -e 's/DNS=/nameserver\t/'`
   echo $serv1 >> $FILE
   serv2=`echo $2|sed -e 's/^/nameserver\t/'`
   echo $serv2 >> $FILE
fi

#fallback server
echo "nameserver 8.8.8.8" >> $FILE


and tested it with "sudo ./DNS-fix" to confirm that the content of 
/etc/dnsmasq-resolv.conf made sense. After copying DNS-fix to
/etc/networkd-dispatcher/routable.d/
and setting
resolv-file=/etc/dnsmasq-resolv.conf
the upstream nameservers will be updated from the current lease info.

Questions:
- Can you spot any problems with this idea?
- Will it work on any distribution running netplan?
- Is anyone interested in improving this idea and updating the "Automatic 
nameserver configuration" section of the Dnsmasq setup information?

Any response is appreciated!

Best wishes,

Kjell




More information about the Dnsmasq-discuss mailing list