sudo: unable to resolve host <hostname>

Fix 'sudo: unable to resolve host' in Ubuntu 22.04 LTS

Linux & Unix Beginner 👁 1 views 📅 May 29, 2026

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.

  1. Check your current hostname
    Open a terminal and run:
    hostnamectl
    Look for the line that says Static hostname:. That's the name you need. On my test server, it's ubuntu-server-01. Yours will be different. Write it down.
  2. Open /etc/hosts for editing
    Run:
    sudo nano /etc/hosts
    You'll see a file that looks like this:
    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-allrouters
    
    Notice the second line: 127.0.1.1 followed by your hostname. That's where the problem starts. If that line is missing, or if the hostname is misspelled, you get the error.
  3. 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:
    127.0.1.1       ubuntu-server-01
    Make sure there's a tab or space between the IP and the hostname. Then save the file (in nano: Ctrl+O, then Enter to confirm, then Ctrl+X to exit).
  4. Test the fix
    Run any sudo command, like:
    sudo ls /root
    You should see the directory listing without the hostname warning. If it still shows, move to the next section.

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:
    127.0.1.1       web01.example.com web01
    The full name first, then the short alias. This is common on servers joined to Active Directory or using Kerberos.
  • Check /etc/hostname — Run cat /etc/hostname. It should match the static hostname from hostnamectl. If they don't match, change one so they agree. Edit /etc/hostname with sudo nano /etc/hostname, put the correct hostname, save, and reboot.
  • IPv6 only system — On a system with IPv6 disabled, the ::1 line won't help. Make sure the IPv4 127.0.1.1 line 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-hostname and then update /etc/cloud/cloud.cfg to set preserve_hostname: true. That's an advanced step—try the simple /etc/hosts fix first. It works 90% of the time.
  • Still stuck? — Run sudo -V and look for the line Host name resolution:. It shows how sudo resolves the hostname. If it says dns before files, 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 use 127.0.0.1 for the hostname entry. That IP is reserved for localhost. Using 127.0.1.1 keeps localhost clean and avoids confusing some network services. Ubuntu defaults to 127.0.1.1 for 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?