Quick Summary: The warning
sudo: unable to resolve host <hostname>: Name or service not known(orTemporary failure in name resolution) occurs in Linux when the system’s active hostname in/etc/hostnamedoes not have a corresponding local IP mapping in/etc/hosts. Whilesudousually still executes after a noticeable DNS timeout delay, fixing the mismatch eliminates the lag and prevents hostname resolution failures in local services.
Understanding the Root Causes
Every time you invoke sudo, it checks the local machine’s hostname against /etc/hosts and configured DNS servers to evaluate host-specific rules in /etc/sudoers. When the local hostname cannot be resolved to 127.0.0.1 or 127.0.1.1, sudo stalls waiting for DNS queries to time out before printing the warning. Common triggers include:
- Incomplete Hostname Renaming: Updating
/etc/hostnameor runninghostnamectl set-hostnamewithout adding the new name to/etc/hosts. - Cloud VM & Container Provisioning: AWS EC2, DigitalOcean, or Docker/LXC containers dynamically assigning a hostname at boot that is missing from the static
/etc/hoststemplate. - WSL2 Auto-Generated Hosts Conflicts: Windows Subsystem for Linux (WSL2) overwriting or failing to map the Windows computer name inside
/etc/hosts.
Step 1: Quick Fix – Align /etc/hostname and /etc/hosts
Inspect your current active hostname and map it directly to the loopback address in /etc/hosts:
# 1. Check your current active system hostname
hostname
# 2. Inspect the current contents of /etc/hostname
cat /etc/hostname
cat /etc/hosts
# 3. Append or update the 127.0.1.1 (or 127.0.0.1) entry in /etc/hosts
# Replace 'my-new-hostname' with the exact output of the hostname command
sudo sed -i "s/^127\.0\.1\.1.*/127.0.1.1\t$(hostname)/" /etc/hosts
# If no 127.0.1.1 line exists yet, append it safely:
grep -q "$(hostname)" /etc/hosts || echo "127.0.1.1 $(hostname)" | sudo tee -a /etc/hosts
A healthy /etc/hosts file on Ubuntu/Debian should look similar to this:
127.0.0.1 localhost
127.0.1.1 my-new-hostname
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
Step 2: Use hostnamectl for Clean Systemd Renaming
If you intended to change the server’s hostname permanently, use hostnamectl and verify both files match:
# Set the static hostname cleanly via systemd
sudo hostnamectl set-hostname web-server-01
# Update /etc/hosts to match the new hostname
sudo nano /etc/hosts
# Ensure line reads: 127.0.1.1 web-server-01
Step 3: Persist Fixes on Cloud-Init VMs and WSL2
On cloud instances (EC2, GCP, Azure) or WSL2, automatic boot scripts may revert /etc/hosts on reboot unless configured to preserve your changes:
# Option A: Cloud-Init (Ubuntu/Debian cloud images)
# Edit /etc/cloud/cloud.cfg and set preserve_hostname to true:
sudo sed -i 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg
# Or update the cloud-init hosts template directly in /etc/cloud/templates/hosts.debian.tmpl
# Option B: WSL2 (Windows Subsystem for Linux)
# Ensure /etc/wsl.conf allows or manages hosts cleanly, or add the entry to C:\Windows\System32\drivers\etc\hosts
cat /etc/wsl.conf
Verification & Testing Steps
Verify that sudo executes instantaneously without any hostname warnings and that the hostname resolves locally:
# 1. Run a lightweight sudo command and check for zero warnings or delay
sudo -v
sudo echo "Hostname resolution fixed!"
# 2. Verify local resolution using getent or ping
getent hosts $(hostname)
# Expected output: 127.0.1.1 my-new-hostname
# 3. Confirm hostname matches across both hostname and hostnamectl
hostname && hostnamectl --static
Troubleshooting Summary Comparison
| Environment / Scenario | Primary Root Cause | Recommended Solution |
|---|---|---|
| Standard Ubuntu / Debian | /etc/hosts missing 127.0.1.1 <hostname> |
Add 127.0.1.1 $(hostname) to /etc/hosts |
| Cloud VM (AWS / GCP / Azure) | cloud-init resets hostname or hosts on boot |
Set preserve_hostname: true in /etc/cloud/cloud.cfg |
| After Renaming Server | Updated /etc/hostname without updating /etc/hosts |
Sync both using hostnamectl and edit /etc/hosts |
| WSL2 Distribution | Windows hostname missing from Linux hosts file | Add hostname mapping in /etc/hosts or Windows hosts |
Leave a Reply