Why /etc/hosts suddenly stops being respected after a kernel upgrade
When a kernel upgrade lands, you expect only low‑level changes. In practice, the upgrade can ripple through the entire userland, especially when systemd‑resolved is involved. A common symptom is that hostname lookups that used to resolve via /etc/hosts now fall back to DNS or fail entirely. The culprit is often a subtle change in how systemd‑resolved loads its configuration or how the Name Service Switch (NSS) library consults /etc/hosts.
Below is a step‑by‑step guide that walks through the problem, shows how to diagnose it, and provides several robust fixes. The examples are taken from a recent Debian 12 upgrade to kernel 6.6, but the same pattern applies to Fedora, Arch, OpenSUSE, and other distributions that ship systemd‑resolved.
1. The anatomy of hostname resolution in systemd‑based systems
| Component | Role | Typical path |
|---|---|---|
/etc/hosts | Static, local mapping | hosts: entry in /etc/nsswitch.conf |
systemd‑resolved | Stub resolver and DNS cache | /run/systemd/resolve/stub-resolv.conf |
nsswitch.conf | NSS lookup order | hosts: files mdns4_minimal [NOTFOUND=return] dns |
/etc/resolv.conf | DNS server list | symlink to /run/systemd/resolve/stub-resolv.conf |
systemd‑resolved runs as a system service. It listens on the stub port 53 (127.0.0.53) and provides DNS caching, DNS‑SEC validation, and local hostname resolution. The stub resolver is enabled by default, and /etc/resolv.conf is usually a symlink to its stub file.
When a program calls getaddrinfo(), the NSS library consults /etc/nsswitch.conf. If the files keyword is present, it reads /etc/hosts first. Only if that lookup fails does it forward the query to the stub resolver, which in turn may query upstream DNS servers.
2. What changed after the kernel upgrade?
A kernel upgrade can:
- Replace the libc implementation – the new libc may load a different NSS module order.
- Alter the initramfs – if
systemd‑resolvedis packaged in the initramfs, the new kernel may bring a newer version of the service. - Modify the
systemdbinary – a newersystemdmay change default configuration files, such as/etc/systemd/resolved.conf.
In many cases, the kernel upgrade triggers a re‑installation of systemd packages. The new systemd‑resolved binary may have a different default for the Cache=yes or DNSStubListener=yes options, causing the stub resolver to be disabled or misconfigured.
3. Symptoms to look for
| Symptom | Typical cause |
|---|---|
ping localhost resolves to 127.0.0.1, but ping myhost.local fails | /etc/hosts not consulted |
systemd-resolve --status shows “DNS server: 127.0.0.53” but no entries for local names | Stub resolver disabled |
journalctl -u systemd-resolved contains “Failed to read /etc/hosts” | NSS module missing or misordered |
dig myhost.local returns NXDOMAIN | DNS query bypassed /etc/hosts |
If you notice any of these, start with the following diagnostics.
4. Diagnostics checklist
Verify the service status
systemctl status systemd-resolvedLook for
Loaded: loaded (/lib/systemd/system/systemd-resolved.service; enabled; vendor preset: enabled)andActive: active (running).Check the stub resolver configuration
systemd-resolve --statusThe output should contain a section like:
DNS Server: 127.0.0.53 DNS Server: 2001:4860:4860::8888If the
DNS Server: 127.0.0.53line is missing, the stub listener is disabled.Inspect
/etc/nsswitch.confgrep hosts /etc/nsswitch.confThe line should include
filesbeforedns. Example:hosts: files mdns4_minimal [NOTFOUND=return] dnsIf
filesis missing,/etc/hostswill never be consulted.Confirm
/etc/resolv.confpoints to the stubls -l /etc/resolv.confExpected:
lrwxrwxrwx 1 root root 39 Sep 16 12:34 /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.confIf it points elsewhere, DNS queries may bypass
systemd‑resolved.Check the actual
/etc/hostsfilecat /etc/hostsEnsure the entries are correct and that the file is readable by all users (mode 644). A common mistake is accidentally changing the file to 600, which prevents non‑root programs from reading it.
**Look at
See also
- Adding per‑interface DNS search domains to systemd‑resolved on Ubuntu 24.04
- Using journalctl to Track Down the Hidden ‘eth0’ Carrier Lost Messages That Cause Network Flaps After a Kernel Upgrade
- Rebuilding initramfs to exit emergency mode after a kernel update on Ubuntu 24.04
- How to Extract a Single File from a Borg Backup Archive Without Recreating the Entire Directory Tree
- Configure systemd’s OnFailure to email me when my daily backup service dies