Unable to resolve host {hostname}: Name or service not known (Linux)


To totally unlock this section you need to Log-in

The error/warning defined in the subject of this article usually can be shown while using, for example, the apt-get tool on Ubuntu/Debian Linux distribution. This could happen after we have (finally) decided to change the hostname of our Linux system giving it a better name. Let's say that wow it’s called test-server because that is we will use it for. Now, a common scenario would be that, after creating a sudo user because we do not prefer not to be root all the time, we start using commands with sudo, but at some point we see a strange error in the output of the commands:

unable to resolve host {your_hostname}: Name or service not known

The command run with sudo without any problem but this error message will be displayed anyway.

The Fix

The root cause of the error is actually related to the hostname changing. Let's now show how to fix this unable to resolve hostname error. First, check the hostname of your system with hostname command. For example, let's say that the hostname is test-server.

$hostname
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 our case (and basically in all the cases in which this error appears) the new hostname will miss in 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 or unable to resolve host {your_hostname}: Name or service not known while using apt-get.

To fix this, edit this file and add a new line and set the loopback address with the new hostname (or just replace the old hostname with the new one). You can use Vim or Nano to edit files in command line.

127.0.0.1 <hostname>

So now, our /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

Again, if the old hostname is still present anywhere in your /etc/hosts file, you should replace them with the new hostname (it is recommended).

Immediately after adding the above mentioned line, sudo: unable to resolve host or unable to resolve host {your_hostname}: Name or service not known errors will disappear.


Summary
Article Name
Unable to resolve host {hostname}: Name or service not known (Linux)
Description
The "Unable to resolve host" on Linux systems, while using sudo or apt-get on Terminal or on headless configurations could be annoying. The fix for this kind of scenario is simple and involves the system hostname.
Author
Publisher Name
Heelpbook.net

1 thought on “Unable to resolve host {hostname}: Name or service not known (Linux)”

Comments are closed.