17.6. Hostnames and DNS Client Configuration

Names are the last hop of the model: applications ask for a name, and something on the host turns it into an address. Which something answers - a local file, the machine's own name, a caching stub, or an upstream server - is configurable, and on Debian 13 the pieces are /etc/hosts, the NSS configuration, and systemd-resolved.

17.6.1. Setting the Hostname and Maintaining /etc/hosts

The hostname has three systemd faces: static (what /etc/hostname stores and the boot sets), transient (what the kernel runs with right now, overridable by DHCP or tools), and pretty (a free-form label for humans). The status command shows them together, as john on netlab1:

$ hostnamectl status
 Static hostname: netlab1
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 6b5a5e5702184a8a8d50f4d00f29a8c2
         Boot ID: 5f35b60795f74507a1dcf65913195f7a
  Virtualization: kvm
Operating System: Debian GNU/Linux 13 (trixie)
          Kernel: Linux 6.12.86+deb13-amd64
    Architecture: x86-64
 Hardware Vendor: QEMU
 Hardware Model: Standard PC _i440FX + PIIX, 1996_
Firmware Version: 1.16.3-debian-1.16.3-2
   Firmware Date: Tue 2014-04-01
    Firmware Age: 12y 7month 3w

Everything but the first line is identification this chapter needs only incidentally; the Static hostname line is the one to manage. Setting it is one elevated command:

$ sudo hostnamectl set-hostname netlab1.lab.example

The command prints nothing. It writes /etc/hostname, updates the running kernel's hostname, and notifies systemd-hostnamed, which is why no restart is involved. Confirm from the static selector:

$ hostnamectl --static
netlab1.lab.example

Shell prompts and already-running programs keep the old name until their next session; the file and the kernel do not. The hostname matters to more than prompts - sudo and some mail software warn when the local hostname does not resolve - so pair the change with an /etc/hosts entry. Return the hostname to the short form first, for the rest of the chapter's captures:

$ sudo hostnamectl set-hostname netlab1

Also silent. Now open /etc/hosts and add one line tying the FQDN and the short name to the practice address:

192.168.217.10 netlab1.lab.example netlab1

The file's existing entries serve the same purpose for the local system: 127.0.0.1 localhost, the IPv6 equivalents on ::1, and - the Debian installer's convention for DHCP machines - a 127.0.1.1 netlab1-style entry so the bare hostname resolves even before any network is up. The line above does the same for the lab address, with the FQDN first so the canonical name is what lookups return. Test with getent, which exercises the same NSS path as every other program on the host:

$ getent hosts netlab1.lab.example
192.168.217.10  netlab1.lab.example netlab1
$ getent ahosts netlab1.lab.example
192.168.217.10  STREAM netlab1.lab.example
192.168.217.10  DGRAM  
192.168.217.10  RAW    

getent hosts returns the canonical name and its aliases; getent ahosts returns the socket-view of the same lookup - one line per address and socket type, which is what a network application actually consumes. Both answered from the file, instantly and without any network. That is the property that makes /etc/hosts the right place for small fixed mappings - and the wrong place for anything a DNS server should own (Chapter 22).

17.6.2. NSS Lookup Order and the Role of /etc/resolv.conf

Which source answers a name is a policy, and the policy lives in one line of /etc/nsswitch.conf:

$ grep "^hosts:" /etc/nsswitch.conf
hosts:          files myhostname resolve [!UNAVAIL=return] dns

The modules run left to right, and the first one to answer ends the lookup. files is /etc/hosts. myhostname answers for the names localhost and the machine's own hostname with loopback addresses even when no file entry exists - the fallback that keeps sudo quiet on machines with no hosts entry. resolve is the client of systemd-resolved's stub; [!UNAVAIL=return] is the standard form of the action: continue to dns only if the resolve service itself is unavailable, otherwise its answer - including "name does not exist" - is final. dns is glibc's own resolver, which reads /etc/resolv.conf. The exact line varies with installed packages: a machine with libnss-mdns gains an mdns4_minimal entry for .local names, which is normal, not a misconfiguration.

The line explains one classic symptom. If resolve is listed but the service is not running, the [!UNAVAIL=return] test fails through and lookups fall back to the dns module - so DNS keeps working through whatever /etc/resolv.conf names, while resolvectl status reports the service down. The reverse mistake - both resolve active and a stale /etc/resolv.conf pointing elsewhere - produces lookups that disagree with resolvectl, because each program follows its own order. Which brings the chapter to the file itself:

$ ls -l /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Nov 21 10:19 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

/etc/resolv.conf is a single, global, traditionally file-based interface: nameserver lines, search domains, and options such as timeout and attempts, per resolv.conf(5). Historically every backend fought to own it - the installer wrote it, ifupdown's hooks rewrote it, NetworkManager rewrote it again - and that fight is exactly what systemd-resolved replaces: the symlink hands the file to one owner, permanently. The next subsection shows what the symlink points at and who writes what.

17.6.3. Resolver Configuration Ownership and systemd-resolved

On Debian 13, systemd-resolved is a separate package. Installing it does three things, by design: the service is enabled; /etc/resolv.conf is converted to the stub symlink seen above, with the previous file preserved as /etc/.resolv.conf.systemd-resolved.bak; and the recommended NSS packages (libnss-resolve, libnss-myhostname) install themselves into the hosts: line of Section 17.6.2. The stub file the symlink targets lists exactly one server - 127.0.0.53, the local resolver - so every program on the host, whatever its NSS order, asks the same cache.

What the stub forwards to is per-link, and that is the design's whole point. Each backend registers its links' DNS servers with resolved: NetworkManager through D-Bus for every active profile (the ipv4.dns/ipv6.dns values from Sections 17.3.2 and 17.5.4), systemd-networkd natively for its DNS= lines, and plain DHCP clients through hooks. The status command shows one link's registration:

$ resolvectl status eth1
Link 3 (eth1)
    Current Scopes: DNS LLMNR/IPv4 LLMNR/IPv6
         Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
       DNS Servers: 2001:db8:17:1::2 192.168.217.2
        DNS Domain: lab.example
     Default Route: yes

The DNS Servers line lists the lab-dual profile's registrations for both families. DNS Domain: lab.example is the search domain; +DefaultRoute means this link's servers may answer queries for names outside any routing domain - the flag Section 17.6.4 turns off. LLMNR scopes are the legacy link-local name protocol; DNSSEC=no/unsupported records that this link's upstream did not advertise DNSSEC support, so resolved reports the capability instead of asserting validation. Global fallbacks - servers used when no link provides any - live in /etc/systemd/resolved.conf and drop-ins under resolved.conf.d/, per resolved.conf(5).

resolvectl command Description
status [LINK] Show global and per-link servers, domains, and protocol flags.
query NAME Resolve NAME and report which link's server answered.
domain LINK DOMAIN... Set a link's routing (~d) and search domains at runtime.
default-route LINK BOOL Set whether a link's servers may answer non-routing-domain queries.
flush-caches Discard cached answers, including negative ones.

Removing resolved from a machine is the same sequence backwards: disable and stop the service, restore the plain /etc/resolv.conf from the backup or write a new one, and let the installed backend own the file directly. The mistake to avoid is leaving the symlink pointing at a stub no daemon serves - lookups then time out against 127.0.0.53 exactly as if the network were down.

Search domains complete short names: ping router1 becomes a query for router1.lab.example because lab.example is eth1's search domain. The name arrived from the same three places DNS servers do - a DHCP option, an RA's DNSSL field (Section 17.5.1's advertisement carried both RDNSS and DNSSL), or the profile's dns-search setting. Watching both forms resolve shows the mechanism:

$ resolvectl query router1.lab.example
router1.lab.example: 192.168.217.2                          -- link: eth1
                     2001:db8:17:1::2                       -- link: eth1

-- Information acquired via protocol DNS in 2.1ms.
-- Data is authenticated: no; Data was acquired via local or encrypted transport: no
-- Data from: network
$ resolvectl query router1
router1: 2001:db8:17:1::2                                   -- link: eth1
         192.168.217.2                                      -- link: eth1
         (router1.lab.example)

-- Information acquired via protocol DNS in 1.7ms.
-- Data is authenticated: no; Data was acquired via local or encrypted transport: no
-- Data from: network

The full name answered with both families via eth1. The short name resolved through the search domain - the parenthesized (router1.lab.example) records the expansion - and both families answered via eth1 as well. A name that belongs to no search domain goes to the general upstream instead:

$ resolvectl query example.com
example.com: 104.20.23.154                                  -- link: eth0
             172.66.147.243                                 -- link: eth0
             2606:4700:10::ac42:93f3                        -- link: eth0
             2606:4700:10::6814:179a                        -- link: eth0

-- Information acquired via protocol DNS in 12.6ms.
-- Data is authenticated: no; Data was acquired via local or encrypted transport: no
-- Data from: network

Public example name, general upstream, primary link - the default-route behavior from Section 17.6.3 doing exactly its job. That behavior becomes a problem on machines whose internal names live in domains the public resolver cannot see, which is what split DNS solves: route only the internal domains to the internal resolver, and let everything else use the general one. Figure 17.3 shows the whole arrangement as one map.

Per-link DNS and split DNS with systemd-resolved

Figure 17.3. A conceptual map of per-link DNS: applications all reach the same stub; routing domains decide which link's upstream answers each query, and search domains expand short names per link. The lab's eth1 carries the practice domains; eth0 carries the general default.

The lab demonstrates split DNS with the practice resolver serving a second domain, corp.example, that the primary link's resolver refuses. Confining eth1 to its routing domain takes two runtime settings, as john with single elevated commands:

$ sudo resolvectl default-route eth1 no
$ sudo resolvectl domain eth1 "~lab.example"

Both are silent. The tilde prefix is the whole trick: ~lab.example is a routing-only domain - lab.example queries go to eth1's servers, but the domain is not used as a search suffix, and eth1's servers answer nothing else (default-route … no). The confined link still serves its own domain:

$ resolvectl query router1.lab.example
router1.lab.example: 2001:db8:17:1::2                       -- link: eth1
                     192.168.217.2                          -- link: eth1

-- Information acquired via protocol DNS in 2.4ms.
-- Data is authenticated: no; Data was acquired via local or encrypted transport: no
-- Data from: network

But the second internal domain, still unrouted, is refused - its query falls to the primary link's resolver, which does not serve it:

$ resolvectl query corp.example
corp.example: resolve call failed: Could not resolve 'corp.example', server or network returned error: REFUSED (Not Ready)

The error is accurate: REFUSED came from the general upstream, which was the only server allowed to see the query. Route the second domain to the internal resolver too:

$ sudo resolvectl domain eth1 "~lab.example" "~corp.example"

The command is silent, but the earlier REFUSED is still cached, so flush first and ask again:

$ sudo resolvectl flush-caches
$ resolvectl query corp.example
corp.example: 192.168.217.2                                 -- link: eth1

-- Information acquired via protocol DNS in 1.7ms.
-- Data is authenticated: no; Data was acquired via local or encrypted transport: no
-- Data from: network

The internal resolver now answers corp.example, via eth1, and every other name still follows the primary link. The same ~-domain settings persist per backend - Domains=~lab.example in a .network file or ipv6.dns-search with the routing flag in a NetworkManager profile - so a working runtime arrangement can be written back to the file that owns the link. The failure modes to remember: a domain routed to a link whose servers do not actually serve it (the REFUSED above), and a stale negative answer surviving the fix until flush-caches clears it.