Troubleshooting Ubuntu’s /etc/hosts File Being Ignored: DNS Resolution Issues and Solutions


4 views

When your Ubuntu system ignores the /etc/hosts file, you'll typically experience:

ping: nowhere: Name or service not known
ping mydomain.com → resolves to external IP instead of 127.0.0.1

The modern Linux stack has several layers that can override traditional host resolution:

# Check Name Service Switch configuration
cat /etc/nsswitch.conf
# Typical output showing resolution order:
hosts: files dns [NOTFOUND=return] mdns4_minimal

Use these commands to diagnose exactly how resolution occurs:

# Trace host resolution
getent hosts mydomain.com

# Alternative using systemd
systemd-resolve --statistics

Try these solutions in order of increasing impact:

# 1. Force flush DNS cache (systemd-resolved)
sudo systemd-resolve --flush-caches

# 2. Disable DNS caching temporarily
sudo systemd-resolve --set-dns=127.0.0.1 --interface=lo

# 3. Modify nsswitch.conf (if needed)
sudo nano /etc/nsswitch.conf
# Ensure 'files' appears before 'dns':
hosts: files dns

Create a comprehensive test case:

#!/bin/bash
echo "127.0.0.1 test.local" | sudo tee -a /etc/hosts
ping -c 1 test.local > /dev/null && echo "Success" || echo "Failed"

For stubborn cases where corporate DNS or network policies interfere:

# Completely disable systemd-resolved
sudo systemctl stop systemd-resolved
sudo systemctl disable systemd-resolved

# Alternative: Use dnsmasq as local resolver
sudo apt install dnsmasq
sudo echo "listen-address=127.0.0.1" >> /etc/dnsmasq.conf
  • Always verify file permissions: sudo chmod 644 /etc/hosts
  • Check for trailing spaces or invisible characters
  • Use FQDNs (fully qualified domain names) in hosts file

When your Ubuntu system ignores the /etc/hosts file entries while resolving domain names, it typically indicates a misconfiguration in the name resolution order. The symptoms described show:

127.0.0.1 localhost.localdomain localhost
127.0.0.1 mydomain.com
127.0.0.1 nowhere

Yet pinging results in either "unknown host" errors or bypasses your local entries to fetch external DNS records.

First verify your current name resolution order:

cat /etc/nsswitch.conf | grep hosts

Typical output should show:

hosts: files dns

If you see dns before files, that explains why external DNS takes precedence.

Option 1: Modify nsswitch.conf

sudo nano /etc/nsswitch.conf

Ensure the hosts line reads:

hosts: files dns

Option 2: Test with getent

getent hosts mydomain.com

This shows which source (files or DNS) resolved the hostname.

Check systemd-resolved status if present:

systemd-resolve --status

Temporarily disable DNS caching:

sudo systemctl stop systemd-resolved

Edit the resolved.conf file:

sudo nano /etc/systemd/resolved.conf

Add/modify these lines:

[Resolve]
DNSStubListener=no

Then restart services:

sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
sudo systemctl restart systemd-resolved

Create a test script to validate resolution:

#!/bin/bash
echo "Testing host resolution:"
for host in localhost nowhere mydomain.com; do
  ip=$(getent hosts $host | awk '{ print $1 }')
  echo "$host → $ip"
done