The unsettling part of this warning is that sudo often still runs the command—just after printing an error or pausing. That points away from authorization and toward local name lookup. During startup, sudo obtains the machine name and may resolve it; when the current hostname and the resolver’s local sources disagree, the administrative command can proceed while the lookup complains.
Confirm the exact failure without changing files
printf 'active hostname: '
hostname
printf 'static hostname: '
cat /etc/hostname
printf 'NSS lookup: '
getent hosts "$(hostname)" || echo 'not resolvable through NSS'
printf 'hosts policy: '
grep -E '^[[:space:]]*hosts:' /etc/nsswitch.confactive hostname: laptop
static hostname: laptop
NSS lookup: not resolvable through NSS
hosts policy: hosts: files mdns4_minimal [NOTFOUND=return] dnsThese checks isolate identity from lookup
hostnamereports the active kernel hostname in the current UTS namespace./etc/hostnamenormally stores the static name used at boot on systemd distributions.getent hostsuses the same glibc Name Service Switch path applications use;pingandnslookupare not equivalent diagnostics.The
hosts:line controls lookup sources and order;filesrefers to/etc/hosts, while other modules can resolve local names differently.An empty
getentresult confirms lookup failure without assuming DNS or/etc/hostsis responsible.
Inspect the local files for a mismatch
hostnamectl status 2>/dev/null || true
printf '\n/etc/hostname:\n'
sed -n '1,20p' /etc/hostname
printf '\n/etc/hosts:\n'
sed -n '1,80p' /etc/hostsStatic hostname: laptop
...
127.0.0.1 localhost
127.0.1.1 old-laptopA stale alias explains the warning
In the example, the active/static name is
laptop, but/etc/hostsstill mapsold-laptop.127.0.0.1 localhostshould normally remain intact.Ubuntu/Debian commonly use
127.0.1.1for the machine hostname when it has no permanent IP suitable for/etc/hosts.A server with stable address and managed DNS may intentionally use another mapping; follow its network design rather than forcing the desktop convention.
Comments begin with
#, and each hosts row isIP-address canonical-name aliases....
Back up and edit with sudoedit
sudo cp --preserve=mode,ownership,timestamps /etc/hosts /etc/hosts.before-hostname-fix
sudoedit /etc/hostssudo: unable to resolve host laptop: Name or service not known
# The editor opens a temporary copy.Risk level: caution. Review the command before running it.
The warning can appear during its own repair
The backup is narrow, local, and recoverable; record its path and remove it later according to system policy.
sudoeditedits a temporary user-owned copy and installs it with elevated privilege after the editor exits.The existing resolution warning does not necessarily mean sudo denied authorization; read the command’s final status and output.
Do not use shell redirection such as
sudo echo ... > /etc/hosts; the unprivileged shell owns the redirection.If sudo genuinely cannot run, use an already-open root session, recovery environment, or console procedure appropriate to the system rather than weakening permissions.
Correct the hostname mapping
127.0.0.1 localhost
127.0.1.1 laptop
# IPv6-capable hosts commonly retain their existing ::1 entries below.Use the literal active hostname
Replace
laptopwith the exact output ofhostname, respecting spelling and dots.A short static hostname should use valid hostname characters and should not be confused with the login username shown in the shell prompt.
127.0.1.1is inside IPv4 loopback space and is traditionally used by Debian-derived systems for the local machine name.Do not use
127.0.0.0; it denotes the zero address of the127.0.0.0/8loopback network.Retain
localhoston127.0.0.1and existing IPv6 localhost configuration.
Alternatively, change the hostname to match policy
sudo hostnamectl set-hostname laptop
hostnamectl statusStatic hostname: laptop
...Risk level: caution. Review the command before running it.
Changing identity has a wider blast radius
hostnamectl set-hostnameupdates systemd-managed hostname state and normally the static/etc/hostnamevalue.It does not guarantee that every
/etc/hosts, DNS, DHCP, TLS, monitoring, backup, SSH, cluster, or inventory record is updated.Choose the canonical hostname from organizational policy instead of merely matching whichever stale file is easiest.
Existing shells can retain the old prompt until a new shell or session starts.
Some services cache or publish the hostname and may need an intentional restart or reboot after the complete change.
Verify through NSS before trying sudo again
current_host=$(hostname)
getent hosts "$current_host"
sudo -k
time sudo true
printf 'sudo exit status: %d\n' "$?"127.0.1.1 laptop
[sudo] password for user:
...
sudo exit status: 0Verification tests resolution and authorization separately
getentshould return at least one usable address for the exact active name.sudo -kinvalidates the cached timestamp so the next test exercises normal authentication.sudo trueperforms no system mutation; timing shows whether lookup delay disappeared.Exit status zero proves the test command succeeded, while absence of the warning confirms the visible symptom is resolved.
Delete the backup only after the machine has rebooted and dependent services remain healthy, if policy permits deletion.
Why getent is better than editing DNS first
Host lookup on Linux is composed by NSS. Depending on /etc/nsswitch.conf, the answer can come from /etc/hosts, nss-myhostname, systemd-resolved, DNS, mDNS, LDAP, or another source. getent follows that configured path. A public DNS query tool can report NXDOMAIN while local NSS resolution is working exactly as designed.
Systems where /etc/hosts is not the owner
Cloud images: cloud-init or provider agents may regenerate hostname state at boot. Configure the provider/cloud-init ownership, including
preserve_hostname, instead of fighting generated files.Configuration-managed servers: change the Ansible, Puppet, Chef, Salt, or image source and deploy it; a manual edit will drift or be overwritten.
Containers: the hostname lives in a UTS namespace and
/etc/hostsis commonly generated by Docker, Kubernetes, or the runtime. Change the workload/runtime specification.Corporate DNS hosts: the fully qualified hostname may need a correct DNS record and search-domain policy in addition to local fallback.
Systems using nss-myhostname: the active local hostname may resolve without an
/etc/hostsentry; diagnose why the NSS module is missing or bypassed before duplicating configuration.Read-only appliances: use the vendor-supported configuration interface or recovery method.
If the names match but lookup still fails
Check that
/etc/nsswitch.confhas a validhosts:line and the intended source such asfilesormyhostname.Validate
/etc/hostssyntax, whitespace, encoding, permissions, and line endings.Use
getent ahosts "$(hostname)"andresolvectl query "$(hostname)"where applicable to compare resolver paths.Look for an FQDN-versus-short-name mismatch and inspect
hostname -fcarefully; it depends on successful resolution.Check whether a VPN, NSS plugin, LDAP, DNS timeout, or broken IPv6 path delays lookup before the local source is tried.
Trace the lookup only after simple configuration evidence is exhausted; do not disable security services blindly.
Common mistakes
Putting the username in `/etc/hosts`: the warning displays a hostname, even when a prompt or old article labels it as “my_user.”
Mapping to `127.0.0.0`: use a valid loopback host address such as the distribution’s conventional
127.0.1.1mapping.Deleting IPv6 rows: unrelated localhost behavior can break.
Editing only `/etc/hostname`: the active kernel name and resolver mapping can remain inconsistent until applied.
Using `nslookup` as the only test: it queries DNS and bypasses much of NSS policy.
Making `/etc/hosts` world-writable: this creates a serious local security problem; preserve root ownership and normal permissions.
Assuming every delay is this bug: sudo can also wait on authentication, PAM, directory services, DNS, mounts, or auditing.
Primary references
Linux hosts file manual defines
/etc/hostsrow syntax and hostname rules.Name Service Switch manual explains lookup sources and their order.
getent manual documents querying the configured NSS hosts database.
systemd-resolved manual explains local hostname,
/etc/hosts, DNS, LLMNR, and mDNS resolution.
Comments and corrections