Fix 'sudo: unable to resolve host' in Ubuntu 22.04 LTS
This error pops up when sudo can't match your hostname to an IP. It's a DNS issue, not a sudo one. Fix it in /etc/hosts.
You run sudo apt update on your Ubuntu 22.04 server, and instead of getting package lists, you see this line: sudo: unable to resolve host ubuntu-server-01. It doesn't stop the command from running, but it's annoying and makes you wonder if something's broken. This usually happens right after you change the hostname, spin up a new VPS, or clone a VM. It can also make sudo commands hang for 5–10 seconds before executing.
What causes this
The root cause is dead simple: your system's hostname doesn't match any entry in /etc/hosts. When you run sudo, it tries to resolve the hostname to an IP address so it can log the action. If it can't find a matching line, it throws that warning and falls back to using localhost. The real fix isn't changing sudo configs or DNS servers—it's adding your hostname to the local hosts file.
Step-by-step fix
You'll need a text editor with sudo privileges. I'll use nano here, but vim or vi works too if that's your thing.
- Check your current hostname
Open a terminal and run:
Look for the line that sayshostnamectlStatic hostname:. That's the name you need. On my test server, it'subuntu-server-01. Yours will be different. Write it down. - Open /etc/hosts for editing
Run:
You'll see a file that looks like this:sudo nano /etc/hosts
Notice the second line:127.0.0.1 localhost 127.0.1.1 ubuntu-server-01 # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters127.0.1.1followed by your hostname. That's where the problem starts. If that line is missing, or if the hostname is misspelled, you get the error. - Add or fix the hostname entry
If the line isn't there, add it. If it exists but has the wrong hostname, fix it. The correct entry should be:
Make sure there's a tab or space between the IP and the hostname. Then save the file (in nano:127.0.1.1 ubuntu-server-01Ctrl+O, thenEnterto confirm, thenCtrl+Xto exit). - Test the fix
Run any sudo command, like:
You should see the directory listing without the hostname warning. If it still shows, move to the next section.sudo ls /root
What to check if the error persists
Sometimes the fix above isn't enough. Here's what else to check:
- Hostname with a FQDN — If your hostname has a dot in it (like
web01.example.com), you need two entries on the same line. In/etc/hosts, write:
The full name first, then the short alias. This is common on servers joined to Active Directory or using Kerberos.127.0.1.1 web01.example.com web01 - Check /etc/hostname — Run
cat /etc/hostname. It should match the static hostname fromhostnamectl. If they don't match, change one so they agree. Edit/etc/hostnamewithsudo nano /etc/hostname, put the correct hostname, save, and reboot. - IPv6 only system — On a system with IPv6 disabled, the
::1line won't help. Make sure the IPv4127.0.1.1line is present. If you want IPv6 too, add:
::1 ip6-localhost ip6-loopback ubuntu-server-01 - Cloud VPS (AWS EC2, DigitalOcean, Linode) — These often manage hostnames via cloud-init. If you change the hostname manually, the next reboot might revert it. You'll need to set it permanently using
hostnamectl set-hostnameand then update/etc/cloud/cloud.cfgto setpreserve_hostname: true. That's an advanced step—try the simple /etc/hosts fix first. It works 90% of the time. - Still stuck? — Run
sudo -Vand look for the lineHost name resolution:. It shows how sudo resolves the hostname. If it saysdnsbeforefiles, your system is checking DNS before /etc/hosts. You can change that order in/etc/nsswitch.conf, but you shouldn't need to. Fixing /etc/hosts is almost always the answer.
A quick note: don't use127.0.0.1for the hostname entry. That IP is reserved forlocalhost. Using127.0.1.1keeps localhost clean and avoids confusing some network services. Ubuntu defaults to127.0.1.1for a reason—stick with it.
That's it. The error is cosmetic in most cases, but fixing it removes the delay and cleans up your logs. You're done in about two minutes.
Was this solution helpful?