Easy way to fix: sudo unable to resolve host name or service not known

As you know, while installing a new operating system like Debian, Ubuntu, or any other Linux distribution, installers ask you to set the hostname. At that time, most users keep the default hostname. 

But later, you have decided to change the hostname to some meaningful text, therefore you have modified the /etc/hostname to update the hostname.

And after the restart, you invoke the sudo command, which gives the error message “sudo: unable to resolve host your-hostname: Name or service not known.”

I have checked /etc/hostname, and everything looks good here. Then why did this error pop up?

Fix sudo unable to resolve host name or service not known

Once in a while, you will encounter the above error after making changes to /etc/hostname. And the resolution of this error is pretty simple.

First and foremost, find your current machine hostname in my case it is “trendoceans-HUB1”, and to verify that, paste the below code on your terminal to find out:

$  cat /etc/hostname



Once you know the machine-name or hostname from /etc/hostname, you can proceed to /etc/hosts to find the existence of your machine name in the respective file.

$ cat /etc/hosts


# Host addresses  localhost  SHEN
::1        localhost ip6-localhost ip6-loopback
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters

Alright, now I found my problem. My hostname is an older one, “SHEN”, and the latest hostname or machine-name is “trendoceans-HUB1“.

Very few people know How to use cat and tac command in Linux

$ cat /etc/hostname 

So simply modify the /etc/hosts with the correct hostname, and sometimes you may not find the hostname in the following file without dwelling much.

Add the highlighted line in /etc/hosts and make sure to replace the machine with the actual hostname name, or else your problem will not be resolved.

You can use any command line editor to modify /etc/hosts. In the following example, I’m using nano to edit the file.

$ sudo nano /etc/hosts
# Host addresses  localhost  trendoceans-HUB1
::1        localhost ip6-localhost ip6-loopback
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters

Once the file is modified save and close it.

After that, try to execute the sudo command again and you will see that the error no longer bothers you.

Wrap up

That’s all to fix sudo unable to resolve host name or service not known.

Most readers preferred to read “How to set or change the temporary hostname in Linux”.

This Post Has 6 Comments

  1. Rick Romig

    I use a script to change hostnames that checks to ensure the new hostname is valid then changes it in both /etc/hostname and /etc/hosts. If the change isn’t successfull, it reverts back to the previous hostname.

    1. Gagan Mishra

      We would love to have it

  2. Bill Parsons

    Thank you! This solved my issue!

  3. silent guru

    thanks a lot. my problem is solved

  4. Steve Williams

    Thank you! A simple fix to a relatively simple problem.

Leave a Reply

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.