sudo: unable to resolve host 🐧 Linux

sudo: unable to resolve host <hostname>: Name or service not known

The machine’s hostname isn’t in /etc/hosts (or DNS), so sudo prints this warning — usually after changing the hostname.

Seen on: Linux

Meaning

sudo looks up the local hostname. If /etc/hostname was changed (or a cloud image renamed the VM) without updating /etc/hosts, the lookup fails. Commands still run, but slowly.

Common causes

  • Hostname changed without updating /etc/hosts
  • Cloud-init/container generated a hostname not in /etc/hosts
  • /etc/hosts edited incorrectly

⚡ Quick fix

  1. Add the hostname to /etc/hosts on the 127.0.1.1 line
  2. Or set the hostname back with hostnamectl

Detailed fix by platform

Linux

  1. bash
    hostname
    echo "127.0.1.1 $(hostname)" | sudo tee -a /etc/hosts

How to diagnose

  1. Name — hostname output
  2. Hosts — grep "$(hostname)" /etc/hosts

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.