sudo: unable to resolve host

Fixing 'sudo: unable to resolve host' on Ubuntu 22.04

Linux & Unix Beginner 👁 0 views 📅 May 25, 2026

This error shows when sudo can't map your hostname to an IP. Usually happens after a hostname change or DHCP lease renewal. The fix is updating /etc/hosts.

When this error hits

You type sudo apt update and instead of running, it hangs for 5-10 seconds. Then you see:

sudo: unable to resolve host <your-hostname>

Then it works. Annoying, right? This happens right after you change the hostname with hostnamectl or after your DHCP server assigns a new IP. I've seen it on Ubuntu 22.04 LTS, but it's the same on Debian 11 and even older CentOS 7 boxes. The culprit is almost always the /etc/hosts file.

Root cause

When you run sudo, it checks the system's hostname against DNS or the local hosts file. If your hostname doesn't resolve to 127.0.1.1 (or your assigned IP), sudo throws that warning and waits for a timeout. It's not actually broken — it's just slow. The fix is dead simple: make sure your hostname matches an entry in /etc/hosts.

Don't bother messing with resolv.conf — that rarely helps here. This is a local resolution problem, not a DNS one.

The fix — step by step

  1. Check your current hostname
    Run this to see what the system thinks its name is:
    hostname
    Or the long version:
    hostnamectl
    Write down the output. Usually something like web-server or ubuntu-22-04.
  2. Open /etc/hosts in a text editor
    Use nano or vim:
    sudo nano /etc/hosts
    You'll see a file that looks like this:
    127.0.0.1 localhost
    127.0.1.1 old-hostname
    
    # 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
    ff02::3 ip6-allhosts
  3. Update the hostname entry
    Change old-hostname to whatever you got from step 1. The line should map your hostname to 127.0.1.1 (not 127.0.0.1, that's for localhost).
    So if your hostname is web-server, the line becomes:
    127.0.1.1 web-server
  4. Optional — add your LAN IP
    If you use a static IP on your network, add a line for that too. For example, if your IP is 192.168.1.100:
    192.168.1.100 web-server
    This helps if other machines on the network need to resolve your hostname.
  5. Save and exit
    In nano, hit Ctrl+X, then Y, then Enter. In vim, :wq.
  6. Test it
    Run sudo ls or any sudo command. The warning should be gone. No more 5-second delay.

If it still fails

Two things to check:

  • Reboot or reload hostname — Sometimes the changes don't take until the next boot. Run sudo hostnamectl set-hostname <your-hostname> to force it, or just reboot.
  • Check if DHCP is overriding your hostname — This is common on cloud VMs (AWS EC2, DigitalOcean droplets). The DHCP client may rewrite /etc/hosts on each boot. To stop that, edit /etc/dhcp/dhclient.conf and add:
    supersede host-name "<your-hostname>";

    Then restart networking: sudo systemctl restart networking. Or just live with it if the hostname doesn't matter much — the warning is cosmetic.

Real-world trigger scenario

Last week I had a client's Ubuntu 22.04 server that was renamed from app-01 to app-02 during a migration. They forgot to update /etc/hosts. Every sudo command took 10 seconds. The fix took 30 seconds. This is one of those errors that looks scary but is trivial to fix.

Summary (skip if you already fixed it)

Match your hostname in /etc/hosts to what hostname returns. Use 127.0.1.1, not 127.0.0.1. If it's a cloud VM, check DHCP. Done.

Was this solution helpful?