Linux  ·  low  ·  Day-to-day operations

sudo: unable to resolve host

sudo tries to resolve the machine's own hostname and cannot, so every invocation pauses and warns. It still works — it is just slow and noisy.

What you see

Every sudo command prints "sudo: unable to resolve host myserver" and often takes several seconds before running.

What is actually wrong

The hostname was changed without updating /etc/hosts, so the name in /etc/hostname has no entry mapping it to a loopback address.

Codes and articles

sudo: unable to resolve hostunable to resolve host

The fix

Map the hostname to loopback
Shell5 minuteslow riskreversible

Any time this warning appears. It is a one-line fix.

  1. Find out what the machine thinks it is called.

    Shell
    hostnamehostnamectl statuscat /etc/hostname
  2. Look at what /etc/hosts currently says.

    Shell
    cat /etc/hosts
  3. Add the name to the loopback line. Keep localhost first — order matters to some resolvers.

    Shell
    sudo sed -i "s/^127.0.1.1.*/127.0.1.1\t$(hostname)/" /etc/hosts || echo "127.0.1.1\t$(hostname)" | sudo tee -a /etc/hosts

    Debian and Ubuntu use 127.0.1.1 for the machine's own name specifically so it is distinct from localhost. RHEL-family conventionally appends it to the 127.0.0.1 line instead.

  4. Confirm it resolves now.

    Shell
    getent hosts $(hostname)
  5. If the hostname itself is wrong, set it properly rather than editing files by hand.

    Shell
    sudo hostnamectl set-hostname myserver.example.com
Confirm it workedsudo runs instantly with no warning.
Shell
time sudo true
If you need to undo itRemove the line you added from /etc/hosts.

Where this stops. This write-up was written and checked by hand. It says what each step changes, how to confirm it worked and how to reverse it, and anything destructive is flagged before you reach it. If it does not match what your machine is doing, search the Support Centre for the exact code or message — and when something needs a person, get in touch.