When /etc/hosts Won’t Resolve: A Quick Systemd‑Resolved Fix

The Symptom

You’re pinging localhost or a custom hostname you added to /etc/hosts and the request hangs or returns “Name or service not known”. The same name resolves fine on another machine, and dig or nslookup against a public DNS server works. The problem is isolated to the local resolver. In most modern distributions this points to a mis‑configured systemd‑resolved service or a stale cache.

Why systemd‑resolved Matters

systemd‑resolved is the default name‑resolution daemon on Fedora, Debian, Ubuntu, Arch, and many other distros since 2015. It replaces the older dnsmasq‑based /etc/resolv.conf approach and offers:

  • Local hostname resolution via /etc/hosts and /run/systemd/resolve/resolv.conf.
  • DNS over TLS (DoT) and DNSSEC validation.
  • IPv6‑only or IPv4‑only mode.
  • Link‑local and multicast DNS (mDNS) support.

Because it runs as a system service, a mis‑state in its configuration or a stale cache can silently break local name resolution, even though /etc/hosts looks fine.


1. Verify the Basic State

# Check the current /etc/hosts entry
grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' /etc/hosts

# Verify systemd-resolved is active
systemctl status systemd-resolved.service

If the service is inactive or failed, start it:

sudo systemctl enable --now systemd-resolved

Common Pitfall: /etc/hosts Not Read

systemd‑resolved reads /etc/hosts only if the file is owned by root and has 0644 permissions. A 0600 or 0666 file will be ignored.

sudo chown root:root /etc/hosts
sudo chmod 0644 /etc/hosts

2. Inspect the Resolver Configuration

systemd‑resolved exposes its state via systemd-resolve --status. Look for:

  • Current DNS servers – are they reachable?
  • DNSSEC – is it enabled? A mis‑configured DNSSEC policy can cause failures for names that do not have valid signatures.
  • Link‑specific settings – sometimes a network interface is set to use a different DNS server.
systemd-resolve --status

Example Output

Global
         DNS Servers: 192.168.1.1
          DNSSEC NTA: 0.0.0.0/0
          DNSSEC supported: yes
          DNSSEC enabled: yes
          DNSSEC trust anchors: 2
          DNSSEC key type: RSA/SHA-256
          DNSSEC key size: 2048

If the DNS servers are unreachable, the resolver will fall back to the local cache, which may not contain the host you added.


3. Flush the Cache

Sometimes the cache holds an old negative answer. Flush it and try again.

sudo systemd-resolve --flush-caches

After flushing, test:

ping -c 1 myhost.local

If the ping still fails, move to the next step.


4. Check the Link‑Local and mDNS Settings

systemd‑resolved can be configured to ignore /etc/hosts if the --no-hosts flag is set in the unit file or via the systemd-resolved.conf file.

sudo systemctl edit systemd-resolved

If you see:

[Resolve]
NoHosts=yes

remove or comment it out, then reload:

sudo systemctl daemon-reload
sudo systemctl restart systemd-resolved

systemd‑resolved usually creates a symlink:

ls -l /etc/resolv.conf

It should point to /run/systemd/resolve/stub-resolv.conf. If it points elsewhere (e.g., /etc/resolve.conf or a static file), the resolver may not be used.

sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf

6. Test with dig and nslookup

dig bypasses the local resolver and queries the DNS servers directly. This helps confirm whether the issue is local or remote.

dig @192.168.1.1 myhost.local

If this returns NXDOMAIN, the DNS server is the problem. If it returns a valid A record, the local resolver is still mis‑behaving.


7. Disable systemd‑resolved Temporarily

If you need a quick workaround, you can disable systemd‑resolved and fall back to the classic /etc/resolv.conf approach.

sudo systemctl disable --now systemd-resolved
sudo rm /etc/resolv.conf
sudo touch /etc/resolv.conf
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf

This is not a permanent fix, but it lets you keep working while you investigate.


8. Common Security Implications

IssueRiskMitigation
systemd‑resolved disabledDNS spoofing, MITMKeep systemd‑resolved enabled and use DNSSEC.
NoHosts=yesLocal hosts bypassed, potential for host spoofingEnsure NoHosts=no unless you have a specific reason.
Stale cacheFalse positives in security monitoringFlush cache regularly or enable CacheSize=0 for no caching (not recommended for performance).

9. Advanced: Using systemd-resolve Options

You can override DNS settings per‑link without editing /etc/netplan or NetworkManager:

sudo systemd-resolve --interface eth0 --set-dns 10.0.0.1
sudo systemd-resolve --interface eth0 --set-domain example.com

These commands are useful when you have multiple networks with different DNS policies.


10. Persistent Configuration

Edit /etc/systemd/resolved.conf to make permanent changes:

[Resolve]
DNS=192.168.1.1 8.8.8.8
FallbackDNS=1.1.1.1
Domains=~.
LLMNR=no
MulticastDNS=no
DNSSEC=yes

After editing:

sudo systemctl restart systemd-resolved

11. Troubleshooting Checklist

StepCommandWhat to look for
1systemctl status systemd-resolvedActive, no errors
2systemd-resolve --statusDNS servers reachable, DNSSEC enabled
3cat /etc/hostsCorrect syntax, permissions
4systemd-resolve --flush-cachesCache cleared
5dig @<dns> <name>Direct DNS response
6ping <name>Local resolution success

If all checks pass but resolution still fails, consider:

  • A corrupted systemd-resolved binary (rare, but possible after a kernel upgrade).
  • A network interface mis‑configured to use a different DNS server.
  • SELinux or AppArmor blocking access to /etc/hosts (check audit logs).

12. Real‑World Example

Scenario

A Debian 12 server in a homelab had a custom hostname media.local added to /etc/hosts. After a kernel upgrade, ping media.local hung. The admin ran:

systemctl status systemd-resolved

The service was active, but systemd-resolve --status showed DNSSEC enabled: no. The admin had previously set DNSSEC=yes in /etc/systemd/resolved.conf. The file had been overwritten by the upgrade.

Fix

sudo nano /etc/systemd/resolved.conf
# Add:
[Resolve]
DNSSEC=yes

Then:

sudo systemctl restart systemd-resolved
sudo systemd-resolve --flush-caches
ping media.local

The ping succeeded. The admin added a comment in the file to remember why DNSSEC was re‑enabled.


13. When to Bypass systemd‑resolved

In environments where you need deterministic DNS resolution (e.g., embedded devices, minimal containers), you might prefer a static /etc/resolv.conf. In such cases:

  1. Disable systemd‑resolved.
  2. Create a static /etc/resolv.conf with the required nameservers.
  3. Ensure no other service rewrites the file (e.g., NetworkManager).

This approach sacrifices DNSSEC and DoT but can simplify troubleshooting in constrained environments.


14. Keeping the System Secure

  • Enable DNSSEC: systemd-resolved validates signatures, reducing DNS spoofing risk.
  • Use DoT: Configure systemd-resolved to use a DoT provider (e.g., Cloudflare 1.1.1.1:853).
  • Restrict mDNS: Disable MulticastDNS=yes unless you need local service discovery.
  • Audit logs: Monitor /var/log/systemd/journal for systemd-resolve errors.

15. Quick Reference Commands

# Check status
systemctl status systemd-resolved

# View config
systemd-resolve --status

# Flush cache
sudo systemd-resolve --flush-caches

# Edit config
sudo nano /etc/systemd/resolved.conf

# Restart service
sudo systemctl restart systemd-resolved

# Verify /etc/hosts permissions
stat -c "%U %G %a" /etc/hosts

See also