sudo: unable to resolve host <hostname>: Name or service not known
The machine’s hostname isn’t in /etc/hosts (or DNS), so sudo prints this warning — usually after changing the hostname.
Seen on:
Linux
Meaning
sudo looks up the local hostname. If /etc/hostname was changed (or a cloud image renamed the VM) without updating /etc/hosts, the lookup fails. Commands still run, but slowly.
Common causes
- Hostname changed without updating /etc/hosts
- Cloud-init/container generated a hostname not in /etc/hosts
- /etc/hosts edited incorrectly
⚡ Quick fix
- Add the hostname to /etc/hosts on the 127.0.1.1 line
- Or set the hostname back with hostnamectl
Detailed fix by platform
Linux
- bash
hostname echo "127.0.1.1 $(hostname)" | sudo tee -a /etc/hosts
How to diagnose
- Name — hostname output
- Hosts — grep "$(hostname)" /etc/hosts
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in Linux
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026