Fixing 'sudo: unable to resolve host' Error in Linux
This quick tutorial will show you how to fix 'sudo: unable to resolve host hostname' error in Ubuntu or any other Linux distribution.
I forgot to give it a reasonable name so a few days later, I had to change the hostname and gave it a better name. Now it’s called test-server because that’s what it is used for.
I created a sudo user because I prefer not to be root all the time, specially when I am tinkering with my system.
When I started using commands with sudo, I saw a strange error in the output of the commands:
sudo: unable to resolve host test-server
The command ran with sudo without any problem but this error message was displayed anyway.
Fix sudo: unable to resolve host
The root cause of the error was actually related to the hostname changing. Let me show you how to fix this unable to resolve hostname error.
First, check the hostname of your system with hostname command. In my case, the hostname is test-server.
The hostname is taken from /etc/hostname file.
cat /etc/hostname test-server
The same hostname should be listed in the /etc/hosts file as well. But in my case (and I guess in your case as well) this hostname was missing from the /etc/hosts file as you can see in the output below:
127.0.0.1 localhost The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts
Since it is missing from here, the system is not able to figure out the hostname and thus it throws the error ‘sudo: unable to resolve host’.
To fix this, edit this file and add a new line and set the loopback address with the hostname. You can use Vim to edit files in command line.
So now, my /etc/hosts file looks like this:
127.0.0.1 localhost 127.0.0.1 test-server The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts
Tip: If the old hostname is still present anywhere in your /etc/hosts file, you should replace them with the new hostname.
Immediately after adding the above mentioned line, ‘sudo: unable to resolve host’ error disappeared. I didn't even need to reboot my Ubuntu server.
I hope this quick little tip helped you to fix this irritating error. Let me know if it worked for you or not in the comment section.