devutils/ATFQ/book ATFQ git

Part II · Your system

19.2 DNS resolution

alsodnsresolverresolv.confnameserversystemd-resolvedresolvectlnsswitch/etc/hostsgetent

TL;DR

  • Programs ask the system resolver to turn a name into an address. It tries the sources on the hosts: line of /etc/nsswitch.conf, in order.
  • files means /etc/hosts; dns means the servers in /etc/resolv.conf.
  • With nameserver 127.0.0.53, systemd-resolved does the work: ask it with resolvectl.
getent hosts example.com    # the answer your programs get
resolvectl status           # the real DNS servers, per interface
dig example.com             # ask a DNS server directly

On your machine

alice@laptopexample
$ atfq 19.2
  Servers   127.0.0.53 (systemd-resolved: see resolvectl status for the real
            servers)
  Search    home.lan
  Order     mymachines → resolve → files → myhostname → dns

An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.

Pitfalls

Deep dive 2 questions

Why is the nameserver 127.0.0.53?

That's systemd-resolved's local stub. Programs send it their questions; it caches answers and forwards the rest to the real servers it learned from DHCP or your settings. It can use different servers per interface, so names of a company network go to the VPN's DNS and the rest to your usual one.

What's on the hosts line?

The sources, tried in order. A typical systemd line is mymachines (local containers), resolve (systemd-resolved), files (/etc/hosts), myhostname (your own hostname always resolves) and dns. The [!UNAVAIL=return] after resolve means: if systemd-resolved is running, take its answer and stop there.

Commands
getent resolvectl dig
Paths
/etc/resolv.conf /etc/nsswitch.conf /etc/hosts
See also
19. Network
19.1 Who listens on which port