El MehdiJOUAN EJUpStream
ENFR Contact

NewsInfra

Infra2026-12-033 min read

DNS on Linux: why name resolution fails

getent versus dig, /etc/hosts and nsswitch: tell whether the name does not exist or the server does not answer.

El Mehdi Jouan

El Mehdi Jouan · Senior System Administrator & Support Engineer · On-Prem & Cloud

· 3 min read

"Could not resolve host." The application cannot find a name, while dig on the same machine answers fine. Or the other way round. This is not a contradiction: dig and applications do not take the same path. Knowing which of the two fails already tells you where to look.

The tests were done in a Debian 12 container, with an /etc/hosts file and a DNS server I could break on purpose. The commands are the same on Ubuntu, RHEL and Oracle Linux.

1. Two different paths

An application (browser, curl, Java, Python, ssh) asks the system library for a name. The library follows the order written in /etc/nsswitch.conf:

grep ^hosts: /etc/nsswitch.conf
hosts:          files dns

files means /etc/hosts first, then dns, the servers in /etc/resolv.conf. dig, host and nslookup ignore that order: they query a DNS server directly, without ever reading /etc/hosts.

The command that follows the same path as applications is getent:

getent hosts app.interne.exemple
getent ahosts app.interne.exemple

2. The proof with /etc/hosts

I added a line 10.9.9.9 app.interne.exemple to /etc/hosts, then asked both tools:

$ getent hosts app.interne.exemple
10.9.9.9        app.interne.exemple

$ dig app.interne.exemple | grep status:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN

Same machine, same name, two answers. This is the classic case of an entry left in /etc/hosts after a migration: dig gives the right address, the application keeps going to the old one. When what an application gets does not match dig, I look at /etc/hosts first.

3. The name does not exist, or the server does not answer

These are two different faults, which often give the same message in the application.

The name does not exist: the server answers, and its answer is "unknown".

$ getent hosts nexiste-pas.invalid ; echo $?
2
$ host nexiste-pas.invalid
Host nexiste-pas.invalid not found: 3(NXDOMAIN)

The server does not answer: I put in /etc/resolv.conf an address where nothing listens (192.0.2.53), with options timeout:2 attempts:1.

$ getent hosts debian.org ; echo $?
2                                  (after waiting 4 seconds)
$ dig +time=2 +tries=1 debian.org
;; communications error to 192.0.2.53#53: timed out
$ dig +time=2 +tries=1 @1.1.1.1 +short debian.org
151.101.194.132

getent returns the same code 2 in both cases: it does not tell them apart. dig does: NXDOMAIN means the name does not exist, timed out that the server cannot be reached. The last command asks another server with @: if it answers, the problem is the configured server, not the name.

Slowness is a clue on its own: a lookup that takes several seconds before failing is almost always a server that does not answer.

4. Which server the machine really uses

cat /etc/resolv.conf

On Ubuntu and on distributions that use systemd-resolved, this file often contains nameserver 127.0.0.53: a local relay. The real servers are then given by:

resolvectl status

The search line in the same file matters too. It adds a suffix to short names, and can skew a test: during my tests, the host machine had search home, and a name that does not exist was completed before being sent to the server. I redid the tests in a container with no suffix to get clean results.

5. The causes I meet most

  • A stale entry in /etc/hosts, which comes before DNS.
  • An /etc/resolv.conf rewritten by NetworkManager, DHCP or a VPN client, which replaced the internal server.
  • An internal DNS server reachable from the office network but not from the server zone (firewall on port 53, UDP and TCP).
  • An internal name that only exists in the internal zone, asked to a public resolver.

The procedure, in short

getent hosts name
grep ^hosts: /etc/nsswitch.conf
grep -v '^#' /etc/resolv.conf
dig name
dig @other-server name

getent answers like applications do. dig says whether the name exists and whether the server answers. The gap between the two shows where to look.

  • dns
  • getent
  • dig
  • resolv.conf
  • nsswitch