Sai Karthik

Balancing the braces ...

Fixing the hostname resolving error while using sudo

March 8, 2024 | 1 minute read

When we change the linux system’s hostname via hostnamectl hostname command, It’s not reflected in the /etc/hosts file which causes sudo to show a warning as shown below.

1
2
3
4
5
6
sk@sun:~$ sudo apt update
sudo: unable to resolve host sun: Name or service not known
Hit:1 http://deb.debian.org/debian sid InRelease
Reading package lists... Done
Building dependency tree... Done
...

Here’s how to properly set a hostname.

1
2
3
4
# sets the hostname to sun
sk@moon:~$ sudo hostnamectl hostname sun
# replaces the old hostname to new one in /etc/hosts
sk@sun:~$ sudo sed -i 's/moon/sun/' /etc/hosts

That’s it!

Comment |
Tags: linux hostname