sudo: unable to resolve host <hostname>

Fixing 'sudo: unable to resolve host' on Linux

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

A quick fix for the 'sudo: unable to resolve host' error. You'll fix your /etc/hosts file or hostname mismatch in under a minute.

What's happening here

You type sudo and instead of your command running, you get sudo: unable to resolve host yourhostname. The command might still execute after a 5–10 second pause, but it's annoying and breaks scripts. What's actually happening is that sudo (or more accurately, the system's resolver) can't look up the machine's own hostname via DNS or /etc/hosts. The delay is sudo timing out while trying to resolve the hostname before falling back.

This error usually appears after you've changed your hostname — maybe during setup, or after cloning a VM. The fix is almost always in /etc/hosts.

The 30-second fix: Fix /etc/hosts

Open /etc/hosts in your editor with root privileges:

sudo nano /etc/hosts

Look for a line like:

127.0.0.1    localhost

Add your hostname after localhost on the same line, like this:

127.0.0.1    localhost yourhostname

If your hostname is mybox, the line becomes:

127.0.0.1    localhost mybox

Save the file, exit, and run sudo ls to test. The error should vanish instantly.

Why this works: The system resolver checks /etc/hosts before DNS. By mapping yourhostname to 127.0.0.1 (localhost), you're telling the system "this machine is local" — no network lookup needed. sudo then resolves the hostname immediately.

The 5-minute fix: Check hostname and /etc/hostname

If the quick edit didn't work, your hostname might not match what you put in /etc/hosts. Check the current hostname with:

hostname

Also check what's in /etc/hostname (on most distros) or /etc/sysconfig/network (on older Red Hat systems):

cat /etc/hostname

If these don't match, pick one. I prefer setting the hostname in /etc/hostname and then making /etc/hosts match it. To change the hostname temporarily (until reboot), run:

sudo hostname newhostname

To make it permanent, edit /etc/hostname to contain just the hostname on one line, no extra whitespace, no trailing characters. Then add that exact hostname to the 127.0.0.1 line in /etc/hosts.

Real-world trigger: This exact scenario happens when you clone a VM in VirtualBox or VMware — the clone keeps the original hostname but the network config can't resolve it. The /etc/hostname file still has the old hostname, and /etc/hosts doesn't have any entry for it.

The 15+ minute fix: DNS misconfiguration or NSS order

If both above fail, something deeper is wrong. The error could come from a DNS resolver that's failing to look up the hostname. Check your /etc/nsswitch.conf file — it controls the order of hostname resolution. The relevant line should be:

hosts: files dns

If it says hosts: dns files, the system checks DNS first. If DNS is slow or unreachable, you get the delay. Change it to files dns so /etc/hosts is checked first.

Next, check if your hostname resolves via DNS. Run:

getent hosts yourhostname

If it returns nothing or a wrong IP, your DNS setup might be the problem. On a local network, you don't want the machine looking up its own hostname via DNS — it's a waste of time and fragile. The /etc/hosts fix is simpler and more reliable.

Another rare cause: the hostname contains characters that the resolver doesn't handle well — like underscores (my_box) or spaces. Hostnames should be alphanumeric plus hyphens only, per RFC 952. Rename your machine if you've used weird characters.

Finally, check if avahi-daemon or systemd-resolved is interfering. On systems with systemd-resolved (Ubuntu 18.04+, Debian 10+), the resolver uses a local stub. You can check with:

systemctl status systemd-resolved

If it's running, you might need to configure it to use /etc/hosts properly, or disable it and use /etc/resolv.conf directly. But honestly, if the /etc/hosts fix didn't work, a simpler nuclear option is to purge systemd-resolved and install resolvconf — but that's overkill for most users. Stick with the /etc/hosts edit first.

Prevention: Don't let this happen again

Whenever you change a hostname — whether by hand or by cloning a VM — immediately update /etc/hosts. Add the hostname to the 127.0.0.1 line before you even reboot. It's a 10-second habit that saves you time and frustration later.

Was this solution helpful?