Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readLinux
#linux#sudo#hostname#dns
Bold white headline reading HOST UNKNOWN with a Linux host crate missing its cyan name record on the right.

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/hosts still contains the stock image entry.
  • Containers and Kubernetes Pods whose hostname is the Pod name with no DNS record for it.
  • Machines where /etc/hosts lacks 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:

bash
echo "127.0.1.1 my-test-host" >> /etc/hosts getent hosts my-test-host # 127.0.1.1 my-test-host

Which address to use for the local hostname:

EnvironmentAddressReason
Debian/Ubuntu desktop or VM127.0.1.1convention that avoids claiming 127.0.0.1 for the hostname
Server with a static private IPthat private IPreverse lookups then agree with the forward lookup
Cloud instance with changing IP127.0.1.1the address survives reboots and re-attachment
Kubernetes Podthe Pod name with 127.0.0.1the hostname is ephemeral
Container with --hostname127.0.0.1 <name>Docker does not add it automatically

Key Comparison Table

Diagnosis order, with what each command proves:

CommandExpected outputWhat a wrong result means
hostnamethe short namethe kernel's name is not what you think
hostname -fthe FQDNno domain configured, or no DNS record
getent hosts $(hostname)one line with an addressresolver cannot answer from files or DNS
cat /etc/hostslocalhost plus the hostnamethe mapping is missing or misspelled
cat /etc/resolv.confreachable nameserverDNS server unreachable, causing 5s stalls
getent hosts localhost127.0.0.1 localhosteven loopback is broken

The same missing record breaks more than sudo:

ProgramMessageFix
sudounable to resolve host <name>/etc/hosts entry
sshCould not resolve hostnameDNS or hosts entry for the remote name
ApacheCould not reliably determine the server's fully qualified domain nameServerName plus a hosts entry
PostgreSQLslow startup on listen_addresseshosts entry
mail/sendmailMy unqualified host name unknownhosts 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:

bash
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:

code
127.0.1.1 web-01 web-01.internal 10.0.14.52 web-01.internal

The 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:

bash
time getent hosts "$(hostname)" cat /etc/resolv.conf resolvectl status 2>/dev/null | head -20

If 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:

bash
grep '^hosts' /etc/nsswitch.conf getent hosts "$(hostname)" # must print the address without a delay

Editing 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 documentation

The Linux documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Linux guides

More in Linux

All Linux guides

The guides filed next to this one in Linux, in library order.