When your Ubuntu system suddenly stops honoring entries in /etc/hosts
, you'll typically observe:
$ ping test
PING localhost (127.0.0.1) 56(84) bytes of data. # works as expected
$ host test
test.mydomain.com has address xx.xxx.161.201 # bypasses hosts file
Ubuntu uses multiple layers for name resolution. Let's examine the critical components:
$ cat /etc/nsswitch.conf | grep hosts
hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
The proper order should be:
files
(checks /etc/hosts first)mdns4_minimal
(multicast DNS with fallback)dns
(standard DNS resolution)
NetworkManager can override traditional UNIX configurations. Check for these indicators:
$ systemctl status NetworkManager
$ journalctl -u NetworkManager --since "1 hour ago"
Watch for these common offenders:
- DHCP pushing custom DNS servers
- DNS caching services like systemd-resolved
- Conflicting resolv.conf management
Option 1: Hardcode DNS priorities
# Edit /etc/NetworkManager/NetworkManager.conf
[main]
dns=none
systemd-resolved=false
# Then restart
sudo systemctl restart NetworkManager
Option 2: Full manual control
sudo chattr +i /etc/resolv.conf # Make file immutable
sudo nano /etc/resolv.conf # Set your preferred nameservers
Use these diagnostic commands:
$ getent hosts test # Should show 127.0.0.1
$ systemd-resolve --statistics # Check caching behavior
$ dig +short test # Bypasses local resolution
For developers needing consistent local resolution during testing, consider adding this to your ~/.bashrc
:
export RES_OPTIONS="rotate timeout:1 attempts:1"
Recently, I encountered a frustrating issue on my Ubuntu desktop where the system seemed to completely ignore my /etc/hosts
entries. While ping
worked correctly, other tools like host
and browser requests were resolving names through DNS instead.
Let's examine the relevant configuration files:
# /etc/hosts contents
127.0.0.1 localhost test
127.0.1.1 desktop
# /etc/nsswitch.conf (critical section)
hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
The configuration appears correct at first glance - the files
source is listed first in nsswitch.conf, which should prioritize /etc/hosts
.
The issue stems from how modern Linux systems handle name resolution. Even with correct configurations, there are several layers of caching that might interfere:
- nscd (Name Service Cache Daemon)
- systemd-resolved
- Application-level DNS caching
Here's how to properly troubleshoot and fix this issue:
# First, check if nscd is running
sudo systemctl status nscd
# If running, flush its cache
sudo nscd -i hosts
# Check systemd-resolved status
sudo systemctl status systemd-resolved
# Flush systemd-resolved cache
sudo systemd-resolve --flush-caches
# Verify resolution order
sudo getent hosts test
For systems using systemd-resolved (common in modern Ubuntu), create or modify:
# /etc/systemd/resolved.conf
[Resolve]
DNSStubListener=no
Then restart the service:
sudo systemctl restart systemd-resolved
If you prefer not to use systemd-resolved for DNS:
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
After making changes, verify with:
host test
dig test
getent hosts test
All three commands should now return the IP specified in your /etc/hosts
file.
Remember to:
- Check for any custom network manager configurations
- Verify no applications have their own DNS caching
- Test after reboot to ensure changes persist