Easy Way to Fix: Sudo Unable to Resolve Host Name or Service Not Known

Getting error sudo unable to resolve host name or service not known after executing the sudo command, then read these steps to fix it up.

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 kept the default hostname.

But later, you decided to change the hostname to some meaningful text, which can help you identify your system easily, and to do that, you may have followed this article or directly mentioned the new hostname in the /etc/hostname file.

However, after restarting your system, when you invoke the sudo command, you encounter an error message that states “sudo: unable to resolve host your-hostname: name or service not known.”

So, let’s learn how to fix the sudo unable to resolve host name or service not known in your Linux system.

Fix: Sudo Unable to Resolve Host Name or Service Not Known Error

Once in a while, you will encounter the above error after making changes to /etc/hostname, and fixing this issue is no more complicated than a few simple steps.

First and foremost, find your current machine’s hostname, which you can do by running the following command:

$ cat /etc/hostname

Like in my case, it is “trendoceans-HUB1”.


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

The output shows the main culprits lie here.

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

As you can see, my system hostname is trendoceans-HUB1, but here “SHEN” is still appearing, which makes a system print error about the hostname being incorrect.

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.

If you are not finding the hostname in the following, simply paste the highlighted line into the file by replacing it with your hostname and save 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

Like here, I have replaced the old hostname with the new hostname, and now let me save and close the file. Once you’re done with the changes, you can execute the command that raised the above error.

Wrap up

That’s all for this guide to fixing the error.

Even after following these steps, if you still face the issue, then do let me know in the comment section.

As a side note, you can also use the hostenamctl command to change the hostname of your system without manually making any changes to /etc/hostname.

See you in the next article.

This Post Has 9 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.

  5. Roah

    It worked! Thankss a bunchh!!

    1. Gagan Mishra

      Glad to hear!

  6. Christopher

    Thanks so much!

Leave a Reply