When systemd‑resolved hides /etc/hosts, how to get it back
You’ve edited /etc/hosts to add a local hostname for a home‑lab service, but after a reboot the entry disappears from the output of getent hosts. The system still resolves the name, but it comes from a different source. That’s because systemd‑resolved, the default DNS stack on most recent Linux distributions, now prefers its own cache and configuration over the traditional /etc/hosts file. The result is a silent break in the workflow many beginners rely on.
Below is a step‑by‑step guide to diagnose the problem, restore the expected behaviour, and understand the trade‑offs of each approach. The examples use Ubuntu 24.04, Debian 12, and Fedora 38, but the concepts apply to any system running systemd‑resolved.
Why systemd‑resolved hides /etc/hosts
systemd‑resolved started to read /etc/hosts in 2020, but it also introduced its own “local‑host” namespace that can override or shadow the file. The behaviour is controlled by the ResolveFile and CacheFile directives in /etc/systemd/resolved.conf. When systemd‑resolved is enabled, it creates a private DNS namespace that is consulted before the standard /etc/hosts. If a name is already present in that namespace, the file entry is ignored.
The most common cause of the issue is a stale or corrupted systemd-resolved cache. When the cache contains a stale mapping for a hostname, the file entry is never read. Another cause is a mis‑configured resolved.conf that explicitly disables /etc/hosts handling.
1. Verify the current DNS stack
$ systemctl status systemd-resolved
● systemd-resolved.service - Network Name Resolution
Loaded: loaded (/lib/systemd/system/systemd-resolved.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2026-11-16 10:12:34 UTC; 5h 12min ago
If the service is inactive, the system is using the legacy resolvconf or dnsmasq. In that case /etc/hosts is read normally and the problem is elsewhere. Assuming the service is active, proceed to the next step.
2. Check the current hosts resolution
$ getent hosts myservice.local
127.0.0.1 myservice.local
If the line is missing or shows a different IP, the entry is not being used. Run:
$ systemd-resolve --status | grep -A5 "DNS Servers"
Look for the “DNS Servers” block and the “Current DNS Server” line. If the server is a local DNS (e.g., 127.0.0.53), systemd‑resolved is in use.
3. Inspect the resolved configuration
$ cat /etc/systemd/resolved.conf
[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes
If the file is empty or commented out, systemd‑resolved uses defaults. The defaults do read /etc/hosts. However, if you see DNSStubListener=no or Cache=no, the behaviour may change.
4. Flush the systemd‑resolved cache
A stale cache is the most common culprit. Flush it with:
$ sudo systemd-resolve --flush-caches
$ systemd-resolve --statistics
The statistics output should show Cache Size: 0. Re‑run getent hosts and see if the entry appears.
5. Re‑enable the hosts file in the cache
systemd‑resolved has a per‑service setting that can be toggled. The following command forces it to re‑read /etc/hosts:
$ sudo systemctl restart systemd-resolved
After the restart, test again:
$ getent hosts myservice.local
127.0.0.1 myservice.local
If the entry is still missing, the cache might have a higher‑priority record. Use systemd-resolve --resolve to see the lookup chain:
$ systemd-resolve --resolve myservice.local
The output will show whether the name was resolved via the cache, DNS, or /etc/hosts. If it shows “Cache” with a different IP, you need to clear that specific record.
6. Remove a specific cache entry
systemd‑resolved does not expose a per‑record delete command, but you can force a refresh by editing the hosts file, saving, and then flushing again. A trick is to temporarily rename the file:
$ sudo mv /etc/hosts /etc/hosts.bak
$ sudo systemctl restart systemd-resolved
$ sudo mv /etc/hosts.bak /etc/hosts
$ sudo systemctl restart systemd-resolved
This forces the daemon to reload the file and drop any cached entries that conflicted.
7. Permanent solutions
7.1. Keep /etc/hosts and let systemd‑resolved read it
The simplest approach is to leave the default configuration untouched. If you’re only adding a few local entries, this is usually fine. The downside is that any DNS server you point to can override the file if it contains a conflicting record. That’s rarely a problem for local names, but it can be for names that clash with public DNS (e.g., localhost).
7.2. Disable the DNS stub listener
If you prefer to use the classic /etc/resolv.conf and /etc/hosts stack, you can disable the stub listener:
$ sudo nano /etc/systemd/resolved.conf
[Resolve]
DNSStubListener=no
Then restart:
$ sudo systemctl restart systemd-resolved
Now /etc/hosts is consulted directly by the resolver library, and the system falls back to /etc/resolv.conf for DNS queries. The trade‑off is that you lose the benefits of systemd‑resolved’s DNSSEC validation and split‑DNS features.
7.3. Use a local DNS server
For homelab setups that already run a DNS server (e.g., Pi‑hole, dnsmasq, or CoreDNS), you can delegate all local names to that server. Add the local zone to the server and point systemd‑resolved to it:
$ sudo nano /etc/systemd/resolved.conf
[Resolve]
DNS=192.168.1.2
Domains=~local
The tilde (~) tells systemd‑resolved to use the zone only for names ending in .local. After restarting, getent hosts will still show the file entry, but the DNS server will take precedence for any name that matches the zone. This approach scales well for larger homelabs and keeps /etc/hosts minimal.
8. Common pitfalls and troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
getent hosts shows no entry |
Cache still holds old record | sudo systemd-resolve --flush-caches |
| Entry appears but wrong IP | DNS server has conflicting record | Check DNS server, or use ~ domain in resolved.conf |
systemd-resolve --status shows “DNS Stub Listener: no” |
Stub listener disabled | Re‑enable or use /etc/resolv.conf |
systemd-resolve --resolve shows “Cache” for a name that should be in /etc/hosts |
Cache entry older than file | Rename file, restart, rename back |
When you’re unsure, the systemd-resolve --status command is the most informative. It lists all current DNS servers, the active domain, and whether the stub listener is active.
9. Security considerations
systemd‑resolved validates DNSSEC by default (DNSSEC=yes). If you disable it, you expose the system to spoofed DNS responses. Even when using /etc/hosts, it’s a good idea to keep DNSSEC enabled for all external queries. The only time you might want to disable it is during a local network test where you control all DNS traffic.
If you use a local DNS server, ensure it’s hardened: restrict zone transfers, use TLS for upstream queries, and keep the software up to date. The official systemd documentation recommends using the systemd-resolved stub listener only on trusted networks.
10. Real‑world example: a media server
You run a Plex server on plex.local. You added the following line to /etc/hosts:
127.0.0.1 plex.local
After a reboot, curl http://plex.local resolves to 192.168.1.10 (your router’s DNS). Running getent hosts plex.local shows the router’s IP. The router’s DNS zone contains a record for plex.local that was added by a previous DHCP lease.
Fix: Add a local DNS zone to your Pi‑hole:
zone plex.local {
type master;
file "/etc/pihole/plex.local.db";
}
Add the record:
plex.local. IN A 127.0.0.1
Point systemd‑resolved to Pi‑hole:
sudo nano /etc/systemd/resolved.conf
[Resolve]
DNS=192.168.1.2
Domains=~plex.local
Restart and test:
$ getent hosts plex.local
127.0.0.1 plex.local
Now the local name is resolved by the local DNS server, and the router’s record is ignored.
11. When to keep the default
If you’re just experimenting with a few local services, the default systemd‑resolved behaviour is usually fine. The daemon automatically reads /etc/hosts and falls back to DNS. The only time you should consider disabling it is if you need full control over DNS resolution and want to avoid any caching or DNSSEC validation that might interfere with a custom setup.
12. Quick checklist
-
Confirm systemd‑resolved is running
systemctl status systemd-resolved -
Flush the cache
sudo systemd-resolve --flush-caches -
Restart the service
sudo systemctl restart systemd-resolved -
Verify hosts entry
getent hosts <name> -
If still missing, consider a local DNS server
Add a zone and point systemd‑resolved to it. -
Optional: Disable stub listener
Edit/etc/systemd/resolved.confand setDNSStubListener=no.
Happy troubleshooting, and enjoy your homelab!