Linux: sudo unable to resolve host
sudo resolves the machine's own hostname on every call. Add the name to /etc/hosts, check nsswitch order, and stop the five-second DNS stall behind it.

Introduction & Quick Summary
Every sudo call on a freshly provisioned server prints a warning before it does anything:
sudo: unable to resolve host ip-10-0-14-52: Name or service not known
The command usually still runs, and the warning is ignored for weeks — until a script treats stderr output as failure, or sudo starts taking five seconds per invocation. The message means exactly what it says: sudo tried to resolve the machine's own hostname and the resolver returned nothing.
Where it appears:
- Cloud instances whose hostname was set from instance metadata while
/etc/hostsstill contains the stock image entry. - Containers and Kubernetes Pods whose hostname is the Pod name with no DNS record for it.
- Machines where
/etc/hostslacks the loopback alias (127.0.1.1) that Debian and Ubuntu use for the local hostname. - Hosts whose DNS server has gone away, so every lookup now waits for a timeout.
- Laptops that changed networks while an old search domain lingered in
resolv.conf.
Quick answer: add one line to /etc/hosts mapping the hostname to 127.0.1.1 (or to the machine's real address), confirm with getent hosts $(hostname), and check hosts: in /etc/nsswitch.conf so the resolver consults files before DNS.
Core Mechanics, Signatures & Reproducible Setup
sudo logs every command with the invoking user, the target user, and the hostname. To produce a fully qualified name it performs a forward lookup of gethostname() and, on some configurations, a reverse lookup of the address. When that returns NXDOMAIN or times out, sudo logs the warning and continues with the short name.
The resolver order is defined by /etc/nsswitch.conf:
hosts: files dns myhostname
files reads /etc/hosts; dns queries the servers in /etc/resolv.conf; myhostname is provided by systemd and answers for the local name and localhost. The warning appears when none of them knows the name.
Reproduce it without touching a production host:
# inside a disposable container
docker run --rm -it --hostname my-test-host ubuntu:24.04 bash -c '
apt-get update -qq && apt-get install -y -qq sudo >/dev/null
grep -c my-test-host /etc/hosts || true
getent hosts my-test-host || echo "lookup failed"
sudo -u nobody true
'The container's /etc/hosts holds 127.0.0.1 localhost and the container ID, not my-test-host, so getent fails and sudo prints the warning. Adding the line fixes it:
echo "127.0.1.1 my-test-host" >> /etc/hosts
getent hosts my-test-host # 127.0.1.1 my-test-hostWhich address to use for the local hostname:
| Environment | Address | Reason |
|---|---|---|
| Debian/Ubuntu desktop or VM | 127.0.1.1 | convention that avoids claiming 127.0.0.1 for the hostname |
| Server with a static private IP | that private IP | reverse lookups then agree with the forward lookup |
| Cloud instance with changing IP | 127.0.1.1 | the address survives reboots and re-attachment |
| Kubernetes Pod | the Pod name with 127.0.0.1 | the hostname is ephemeral |
Container with --hostname | 127.0.0.1 <name> | Docker does not add it automatically |
Key Comparison Table
Diagnosis order, with what each command proves:
| Command | Expected output | What a wrong result means |
|---|---|---|
hostname | the short name | the kernel's name is not what you think |
hostname -f | the FQDN | no domain configured, or no DNS record |
getent hosts $(hostname) | one line with an address | resolver cannot answer from files or DNS |
cat /etc/hosts | localhost plus the hostname | the mapping is missing or misspelled |
cat /etc/resolv.conf | reachable nameserver | DNS server unreachable, causing 5s stalls |
getent hosts localhost | 127.0.0.1 localhost | even loopback is broken |
The same missing record breaks more than sudo:
| Program | Message | Fix |
|---|---|---|
sudo | unable to resolve host <name> | /etc/hosts entry |
ssh | Could not resolve hostname | DNS or hosts entry for the remote name |
| Apache | Could not reliably determine the server's fully qualified domain name | ServerName plus a hosts entry |
| PostgreSQL | slow startup on listen_addresses | hosts entry |
mail/sendmail | My unqualified host name unknown | hosts entry and hostname -f |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Fix it in one line, permanently
Add the mapping and verify with the resolver rather than by eye:
NAME="$(hostname)"
if ! getent hosts "$NAME" >/dev/null; then
echo "127.0.1.1 $NAME" | sudo tee -a /etc/hosts
fi
getent hosts "$NAME"On cloud images the entry usually exists but names the old hostname that the image builder used, so the check must be for the current name, not for the presence of a line. tee -a needs sudo — which prints the warning once more before it fixes itself. Use sudo -e /etc/hosts (editor) or an Ansible lineinfile task to avoid the chicken-and-egg moment.
2. Separate the hostname from the reverse DNS address
hostname -f returns the FQDN only when the name resolves to an address that also reverse-resolves to itself. On a VM whose private IP changes, mapping the hostname to that IP breaks DNS consistency after a stop/start; mapping it to 127.0.1.1 does not. For services that need the real interface address, add both:
127.0.1.1 web-01 web-01.internal
10.0.14.52 web-01.internalThe second line is optional for sudo and necessary for software that advertises its address.
3. Stalls, not just warnings: DNS timeouts
When /etc/hosts has no entry and the DNS server is unreachable, each sudo waits for the resolver timeout (5 seconds by default, twice) before printing the warning. That is why sudo feels slow on a disconnected laptop or a broken VPC resolver. Check the timing and the server:
time getent hosts "$(hostname)"
cat /etc/resolv.conf
resolvectl status 2>/dev/null | head -20If the nameserver listed is a stub such as 127.0.0.53, systemd-resolved is in use and the real upstream servers are in its status output. Adding the hostname to /etc/hosts removes the stall regardless of DNS.
4. Containers and Kubernetes
Docker adds an entry for the container's short ID, not for --hostname. Compose lets you declare it:
services:
api:
hostname: api
extra_hosts:
- "api:127.0.0.1"In Kubernetes, the Pod hostname is the Pod name and has no record unless a headless Service or hostAliases provides one:
spec:
hostAliases:
- ip: "127.0.0.1"
hostnames: ["web-0.local"]Without either, sudo inside the container warns on every call. Note that many slim images omit sudo entirely; installing it just to see the warning is a choice, not a requirement.
5. nsswitch order that hides /etc/hosts
A hardened image or a misapplied playbook can leave:
hosts: dns [!UNAVAIL=return] files
With DNS consulted first and "not available" treated as final, the /etc/hosts entry is never read. Restore the standard order — files dns — and confirm:
grep '^hosts' /etc/nsswitch.conf
getent hosts "$(hostname)" # must print the address without a delayEditing nsswitch.conf by hand is risky on managed images; change it through the image build so a redeploy does not revert it.
Common Compilation/Runtime Mistakes & Version Gotchas
Editing /etc/hosts inside a container that will be replaced. The change disappears on the next docker run. Put it in the Dockerfile or Compose extra_hosts, or accept the warning.
Adding the hostname to 127.0.0.1 on a host that runs services bound to the hostname. A server that binds to the address resolved from the hostname may then listen only on loopback, so remote clients cannot connect. Use the real interface address when services bind by name.
Ignoring the warning inside automation. A pipeline that asserts empty stderr fails on a healthy machine once sudo is involved. Fix the hosts file rather than filtering the message, and check the exact text with the regex tester if a log filter is unavoidable.
Version differences worth knowing. sudo 1.9.x logs the warning once per command; older 1.8 releases printed it only when logging required the FQDN, so an upgrade can surface a long-standing misconfiguration. sudo 1.9.10 and later honour log_host and log_year options that change the message format. On systemd hosts, myhostname in nsswitch.conf answers for localhost and the local name even without a hosts entry, which is why the same image behaves differently depending on whether systemd is PID 1.
Comparing configurations across machines. Diff the hosts file from a working host with the broken one using Diff, and dump the environment that the service sees with the env viewer to confirm the hostname and proxy variables before changing resolver settings.
Reference material: hosts(5) manual page and sudoers manual.
Official references
primary documentationThe Linux documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Linux guides
All Linux guidesnginx 502 Bad Gateway: upstream connection refused
The log line names the address and errno, which is the diagnosis. Fix the host, the start-up order, the buffers, or the timeout that actually fired.
Linux: Read-only file system (EROFS) on a mounted volume
EROFS is a mount condition, not a permission error. Read the kernel log first: I/O errors, a full thin pool, an ro fstab entry or a read-only root.
apt: The repository does not have a Release file
apt disables any repository whose Release file returns 404. Correct the suite name, move an end-of-life release to the archive host, or remove a dead repo.
Linux: bind: Cannot assign requested address
EADDRNOTAVAIL means the kernel had no address to bind: too many TIME_WAIT sockets, an IP missing from the interface, or a wrong listen address.
More in Linux
All Linux guidesThe guides filed next to this one in Linux, in library order.