Skip to content
Latchkey

getent hosts: Resolve Names the Way the App Does

getent hosts resolves a name through the system nsswitch configuration, so it reflects exactly what an application sees, including /etc/hosts entries.

dig talks only to DNS, but applications resolve through nsswitch, which also reads /etc/hosts and other sources. getent hosts is the tool that matches application behavior, which is why a name can fail in dig yet work in your service, or the reverse.

What it does

getent hosts queries the hosts databases listed in /etc/nsswitch.conf in order, typically files (/etc/hosts) then dns. It returns what the C library resolver would return for getaddrinfo, which is what most applications actually call.

Common usage

Terminal
getent hosts db.internal
# resolve a service by its compose/k8s name
getent hosts postgres
# reverse: name(s) for an address
getent hosts 10.0.0.5

getent vs dig

ToolSources it uses
dig / hostDNS only, ignores /etc/hosts and nsswitch
getent hostsnsswitch: /etc/hosts, DNS, and any other configured source
Application getaddrinfoSame path as getent hosts

In CI

If dig resolves a name but the application cannot (or vice versa), getent hosts settles it because it uses the same resolution path the app does. In containers, a service name added to /etc/hosts resolves in getent and the app but not in dig, which is the usual explanation for that mismatch.

Common errors in CI

getent hosts prints nothing and exits with status 2 when the name cannot be resolved by any nsswitch source, which is the cleanest scripted "could not resolve" signal. If getent finds a name that dig cannot, an /etc/hosts entry is providing it. If neither resolves, the DNS source in nsswitch is failing.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →