[SOLVED] Error: sudo: unable to resolve host in Ubuntu & Debian
Whenever running commands with sudo, the terminal stutters and prints sudo: unable to resolve host <server-name>: Name or service not known. While the requested command often completes, this DNS resolution timeout adds annoying delays to CLI interactions, breaks CI/CD automated runners, and can disrupt daemons dependent on local hostname validation.
Quick Diagnostics
Quick Solution (1 Minute):
- Check system hostname:
hostname- Append local loopback resolver to /etc/hosts:
echo "127.0.1.1 $(hostname)" | sudo tee -a /etc/hosts
Step-by-Step Solution
-
1
Step 1: Verify Current System Hostname
Check the exact static hostname assigned to your running Linux kernel:
BASHhostname # Or via systemd hostnamectl --staticSuppose the terminal outputs
vps-prod-01. -
2
Step 2: Update /etc/hosts Local IP Mapping
Open your system hosts lookup table with root privileges:
BASHsudo nano /etc/hostsEnsure an entry links loopback IP
127.0.1.1to your hostname:PLAINTEXT127.0.0.1 localhost 127.0.1.1 vps-prod-01Save and exit the editor (
Ctrl + O,Ctrl + X). -
3
Step 3: Confirm Warning Elimination
Run an immediate sudo execution to verify resolution latency is zero:
BASHsudo trueThe command should return immediately without emitting any host lookup warning.
Frequently Asked Questions
Why does sudo attempt hostname lookups on every execution?
sudo consults NSS to check whether the current user is restricted to running commands on specific remote hosts listed in the sudoers ruleset.
Why do Ubuntu and Debian utilize 127.0.1.1 instead of 127.0.0.1?
127.0.0.1 is standard localhost. Debian distributions map the host domain/FQDN to 127.0.1.1 so systems with dynamic DHCP IPs can still resolve their own hostnames locally.
Prevention Advice
Recommended security practices:
- Whenever renaming a server with
hostnamectl set-hostname, update/etc/hostsin the same deployment step. - On cloud VPS instances deploying
cloud-init, ensure/etc/cloud/cloud.cfgpreserves custom hostname settings across reboots.