414. DNS
This page is for anyone who needs to see or change how a KDOS machine turns names into addresses, or to find out why names stop resolving. It covers the caching resolver NetworkManager runs on the loopback address, the one program that writes /etc/resolv.conf, the files involved, and how to see and change which servers answer. The design is explained in Networking; this page gives the procedures.
Installation
Four ports take part, and all four are on every image:
- dnsmasq, the caching resolver NetworkManager starts on
127.0.0.1and::1. NetworkManager runs a seconddnsmasqof its own for each hotspot; - openresolv, whose
resolvconfis the one program that writes/etc/resolv.conf; - networkmanager, which feeds the resolver each connection’s servers;
- ldns, whose
drillis the tool for asking a DNS server a question directly.
Programs on KDOS resolve names through musl’s resolver, which reads /etc/hosts and the servers in /etc/resolv.conf. There is no service script for DNS: the resolver starts and stops with NetworkManager (see NetworkManager), and on a machine where NetworkManager is disabled dhcpcd hands the servers it is given to resolvconf directly.
Configuration
/etc/resolv.conf
The file the image ships, from fs/:
# Generated by resolvconf
# The line above is openresolv's signature and must stay the first line:
# resolvconf refuses to replace a file that does not start with it, and the
# refusal fails every `resolvconf -a` — NetworkManager's, dhcpcd's and
# wg-quick's, which then tears its tunnel down. resolvconf rewrites this file
# when one of them brings up a link. Until then it names only this machine,
# where nothing answers before NetworkManager starts its dnsmasq, so a lookup
# made before the network is up fails at once and reaches no outside resolver.
nameserver 127.0.0.1
resolvconf replaces this file as soon as a network link comes up.
Keep the first line. resolvconf replaces the file only because its first line is resolvconf’s signature, # Generated by resolvconf. Given a file that starts with anything else, resolvconf -a prints signature mismatch: /etc/resolv.conf and fails; NetworkManager then carries on with the file as it was, and wg-quick takes its tunnel down again.
While NetworkManager runs, the file names only 127.0.0.1, the local dnsmasq: when one of the entries handed to resolvconf names a local resolver, it writes only that. The exception is a wg-quick tunnel with a DNS= line, whose entry is marked exclusive, so its servers are the only ones in the file until wg-quick down (see A WireGuard tunnel with wg-quick).
The installer copies the live system’s file to the disk (see What the installer writes).
resolvconf
subs="libc dnsmasq named pdnsd pdns_recursor unbound"
restart='"/usr/sbin/service status \\$$1 >/dev/null 2>\&1 \&\& /usr/sbin/service restart \\$$1"'
./configure --prefix=/usr --sysconfdir=/etc --sbindir=/usr/sbin \
--libexecdir=/usr/lib/resolvconf --rundir=/run --mandir=/usr/share/man
make SUBSCRIBERS="$subs" RESTARTCMD="$restart"
make SUBSCRIBERS="$subs" RESTARTCMD="$restart" DESTDIR=$PKG install
ports/core/network/openresolv/build.sh, lines 12–17
SUBSCRIBERSnames the programsresolvconfcan pass the merged result to;libcis the one that writes/etc/resolv.conf.RESTARTCMDis the command a subscriber runs, with a service’s name in$1, to restart that service whenservice statusreports it running, so that it reads the new servers;resolvconf -Rprints it. Why it is quoted this way is in the openresolv port’s notes.
The package also installs upstream’s /etc/resolvconf.conf, whose resolv_conf line names the file to write; resolvconf.conf(5) lists the other keys.
Each program that brings a link up hands resolvconf the servers for that link under a name of its own, which resolvconf -i lists. resolvconf merges them, so none overwrites another’s entries.
NetworkManager’s resolver
install -d "$PKG/usr/lib/NetworkManager/conf.d"
cat > "$PKG/usr/lib/NetworkManager/conf.d/10-kdos-dns.conf" <<'EOF'
[main]
dns=dnsmasq
EOF
chmod 644 "$PKG/usr/lib/NetworkManager/conf.d/10-kdos-dns.conf"
ports/core/network/networkmanager/build.sh, lines 64–69
dns=dnsmasq makes NetworkManager start dnsmasq listening on 127.0.0.1 and ::1, give it each active connection’s servers, and write 127.0.0.1 to /etc/resolv.conf through resolvconf (the port names /usr/sbin/resolvconf at build time).
The local resolver splits names by domain. Each connection’s servers are asked about the domains in that connection’s search list; a name in no connection’s list goes to the servers of the connections that hold the default route (the route most traffic leaves by). A VPN that carries only its own networks is therefore asked only about its own domains, and one that takes the default route is asked about everything else as well. The rules, and the ipv4.dns-priority property that orders them, are in nm-settings-nmcli(5).
NetworkManager starts this dnsmasq with --no-hosts, so the local resolver itself does not read /etc/hosts. Programs on this machine still see /etc/hosts, because the C library reads it before it asks a server.
dnsmasq also reads every file in /etc/NetworkManager/dnsmasq.d/ (its --conf-dir). The image ships one, kdos-mdns.conf, whose server=/local/127.0.0.1#5380 sends every .local name to kdos-mdnsd, which asks Avahi (see .local names through DNS). An option of your own goes in a file beside it, such as a server for one domain (server=/<domain>/<address>); dnsmasq(8) lists the options.
/etc/hosts
127.0.0.1 localhost
127.0.1.1 kdos
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
The installer rewrites the 127.0.1.1 line with the host name you choose. Add a line of your own for any name this machine should resolve without asking a server.
Usage
Seeing which servers are in use
Read what the C library uses:
cat /etc/resolv.conf# Generated by resolvconf, thennameserver 127.0.0.1andoptions edns0 trust-ad, means NetworkManager’s resolver is in charge.See the servers each link handed to
resolvconf, each under a heading such as# resolv.conf from NetworkManager, and the names they are kept under:resolvconf -l resolvconf -iNetworkManager’s entry is
NetworkManagerand names127.0.0.1; awg-quicktunnel’s is the tunnel’s interface name. On a machine with NetworkManager disabled,dhcpcdhands in an entry per link, such aseth0.dhcpforeth0.See the servers NetworkManager gave the resolver for one device:
nmcli -f IP4.DNS,IP6.DNS,IP4.DOMAIN device show <interface>
Asking a question
getent hosts example.org # through the C library, as every program asks
drill example.org # the servers in /etc/resolv.conf, with the full answer
drill @9.9.9.9 example.org # one server, by-passing the local resolver
When getent fails and drill @<server> answers, the fault is between this machine and its resolver, not in the outside world.
Choosing the servers for a connection
Set the servers on the profile and tell NetworkManager to ignore the ones DHCP offers:
nmcli connection modify "<profile>" ipv4.dns "9.9.9.9 149.112.112.112" ipv4.ignore-auto-dns yesUse
ipv6.dnsandipv6.ignore-auto-dnsfor IPv6.Bring the profile up again so the resolver gets the new list:
nmcli connection up "<profile>"
Adding a dnsmasq option
Write the option into a file of your own:
sudo mkdir -p /etc/NetworkManager/dnsmasq.d echo 'server=/corp.example/10.0.0.53' | sudo tee /etc/NetworkManager/dnsmasq.d/50-local.confRestart the resolver. This interrupts name resolution for a moment:
sudo nmcli general reload dns-full
reload is one of the NetworkManager actions the polkit rule leaves out, so it needs sudo.
Running without the local resolver
To have NetworkManager hand each connection’s servers to resolvconf directly, with no dnsmasq in between, override the port’s file with one of the same name:
printf '[main]\ndns=default\n' | sudo tee /etc/NetworkManager/conf.d/10-kdos-dns.conf
and restart NetworkManager with sudo service restart networkmanager (see Restarting the daemon). Afterwards:
/etc/resolv.confnames the connection’s own servers;pgrep -a dnsmasqshows only libvirt’s, if any;- no program resolves a
.localname, because only NetworkManager’sdnsmasqforwards those tokdos-mdnsd.
Know what you give up. Without the local resolver, NetworkManager hands every active connection’s servers to resolvconf in one list, ordered by ipv4.dns-priority, and nothing splits names by domain. The C library sends every name to the first three servers in the file at once and takes the first answer, so a VPN’s servers are asked about every name, and a server after the third is never asked.
To return to the shipped arrangement, delete the file and restart NetworkManager.
On the live medium and on an installed system
The two behave the same. On the live medium a file you add under /etc lasts until the next reboot unless the medium has a persistence store; see Live persistence.
Tips
kdos why /etc/resolv.confnames no package for the file: the image’s copy is the placeholder above, part of the base filesystem, andresolvconfwrites the one in use.sudo resolvconf -urebuilds/etc/resolv.conffrom what the links last handed it, which is what to run after editing/etc/resolvconf.conf. After it, and after everywg-quick upanddown, thelibcsubscriber asksservice status nscdand restartsnscdonly when that reports it running; nonscdis installed, so nothing is restarted and nothing is printed.- A
.localname is mDNS, answered by Avahi rather than by a DNS server. The C library asksdnsmasq, which forwards thelocaldomain tokdos-mdnsd, and that asks Avahi; without NetworkManager’sdnsmasq,avahi-resolve -n <name>.locallooks one up. See Resolving a .local name. - After changing the host name, change the
127.0.1.1line of/etc/hoststo match. The C library finds the machine’s own name only there; without it the name goes to the DNS, which answers that it does not exist when the machine is online and times out when it is not. drill -Schecks a DNSSEC chain, and needs a trust anchor given with-k; none is installed (see Known gaps).
Troubleshooting
Addresses work and names do not. cat /etc/resolv.conf. If it holds only the shipped nameserver 127.0.0.1 line and nothing answers there, the resolver is not running: check service status networkmanager and pgrep -a dnsmasq. NetworkManager’s dnsmasq is the one with --listen-address=127.0.0.1. Two more, started with --conf-file=/var/lib/libvirt/dnsmasq/default.conf, belong to libvirt’s default network and serve its virbr0 while that network runs. With NetworkManager disabled on purpose, service status network should show dhcpcd, and resolvconf -l should list the servers it was given.
Names fail just after boot and work a moment later. The shipped file names only this machine, and nothing answers on 127.0.0.1 until NetworkManager has started its dnsmasq. A lookup made before the network is up fails at once instead of reaching an outside server, as the comment in the file says.
Names fail after a VPN goes down. resolvconf -i lists the names entries are kept under, and resolvconf -l shows each entry, so a tunnel’s that outlived the tunnel is visible. sudo resolvconf -d <name> removes it; sudo resolvconf -u rewrites the file.
A dnsmasq of your own will not start, saying port 53 is in use. NetworkManager’s resolver holds port 53 on 127.0.0.1 and ::1, and libvirt’s on 192.168.122.1. Give your dnsmasq bind-interfaces and an interface= line, so it binds only where it serves; see Serving a network with hostapd.
One domain resolves wrongly on a VPN. nmcli -f IP4.DNS,IP4.DOMAIN device show <tunnel> shows the servers and the domains the tunnel gave the resolver. A domain that is missing there is not sent to the tunnel’s servers, unless the tunnel holds the default route. Add it to the profile with nmcli connection modify "<profile>" ipv4.dns-search <domain> and bring the profile up again.
Known gaps
- Features whose library has no port: the
ldnsrow (no default DNSSEC trust anchor) and thednsmasqrow (no connection-tracking marks)
See also
- Networking: DNS, split by connection
- One DHCP client on the link: why only one program writes
/etc/resolv.conf - NetworkManager, VPN, Access point
- Avahi and mDNS, Firewall