GrepBoard/Manuals
605 manuals ·

Fix "sudo: unable to resolve host" after a hostname change

·Tested on Ubuntu 24.04 · sudo 1.9.15p5 (Docker) · 2026-10·Test scope ↓·5 steps

"sudo: unable to resolve host" after renaming a server means /etc/hosts lacks the new name. Make the 127.0.1.1 line match hostname and confirm with getent.

output
sudo: unable to resolve host server1: Name or service not known

sudo could not turn the current hostname (server1) into an address. Almost always the name was changed (for example with hostnamectl set-hostname) while the 127.0.1.1 line in /etc/hosts kept the old one. Fix that line and the warning goes away.

[1] Check whether the command actually ran. Only the warning was printed and the exit code was 0, so urgent work can go ahead.

console
$ sudo true; echo "exit=$?"sudo: unable to resolve host server1: Name or service not knownexit=0

[2] Compare the current name with /etc/hosts.

console
# hostnameserver1# cat /etc/hosts127.0.0.1 localhost127.0.1.1 oldname ::1     ip6-localhost ip6-loopback

The 127.0.1.1 line still carries the old name (oldname). getent hosts looks names up in the same /etc/nsswitch.conf order. No output and exit code 2 means the name is found nowhere.

console
# getent hosts "$(hostname)"; echo "exit=$?"exit=2

[3] Make the name on the 127.0.1.1 line match hostname. Back up the file first and look at that line. If an edit goes wrong, restore with sudo cp -a /etc/hosts.bak /etc/hosts.

console
$ sudo cp -a /etc/hosts /etc/hosts.bak$ grep -n '^127\.0\.1\.1' /etc/hosts2:127.0.1.1 oldname

How to fix it depends on what the line looks like.

grep resultWhat to do
Just 127.0.1.1 oldnameThe sed one-liner below
An FQDN or aliases too (127.0.1.1 old.example.com old)Open it with sudoedit /etc/hosts and change only the names — sed replaces the whole line and drops the aliases
NothingAdd the line with the tee -a command below

sudo only warns and still runs, so these commands work as is. Both insert the current name via $(hostname), so they can be copied unchanged.

console
$ sudo sed -i "s/^127\.0\.1\.1[[:space:]].*/127.0.1.1 $(hostname)/" /etc/hosts

Add the line only when none exists (adding it anyway leaves two lines for the same address).

console
$ echo "127.0.1.1 $(hostname)" | sudo tee -a /etc/hosts127.0.1.1 server1

The file then looks like this:

console
# cat /etc/hosts127.0.0.1 localhost127.0.1.1 server1 ::1     ip6-localhost ip6-loopback

[4] Check the lookup and sudo again.

console
# getent hosts "$(hostname)"127.0.1.1       server1$ sudo true; echo "exit=$?"exit=0

If you also listed an FQDN, check hostname -f as well.

console
# getent hosts server1; hostname -f127.0.1.1       server1.example.com server1server1.example.com

[5] Next time you rename the machine, change both places. hostnamectl set-hostname changes /etc/hostname and leaves /etc/hosts alone. On a cloud VM, if your edit reverts after a reboot, check cloud-init's manage_etc_hosts. When it is true, the file is regenerated from /etc/cloud/templates/hosts.tmpl on every boot, so edit the template, not /etc/hosts (cloud-init module docs, "Update Etc Hosts").

If you touched the SSH config while renaming, run sshd -t from test sshd config before reload before reloading.

#sudo#hostname#etc-hosts#hostnamectl