Linux Commands

Fix “failed to start hostname.service unit hostname.service is masked”

Whenever you bring in a new computer system, there is a default hostname associated with it that is mapped onto a specific IP address. However, you can always change this hostname for your convenience. Once you change the hostname of your Linux-based system, you should always restart the “hostname.service” module for the new changes to take effect. Sometimes, when restarting this service, you may encounter the “failed to start hostname.service unit hostname.service is masked” error. This article highlights the root cause of this error and shows you how to resolve this error.

Causes of the Error

The system hostname is stored in two main files in Linux. The first file is the “/etc/hostname” file, and the other file is the “/etc/hosts” file. The former only consists of the hostname of your system, whereas the latter contains a mapping of the hostname onto a specific IP address. The “failed to start hostname.service unit hostname.service is masked” error arises when the contents of these two files are mismatched, i.e., the hostname mentioned in one of these files is different from the hostname in the other file. Due to this inconsistency between the contents of the “/etc/hostname” and “/etc/hosts” files, your system will fail to start the hostname.service, and the error will occur.

How to Resolve the Error

The simplest way to resolve this error in Linux is by ensuring that the hostname mentioned in both files is the same. To do so, you will need to check the contents of both these files. You can access the “/etc/hostname” file by executing the following command in the Linux terminal:

$ sudo nano /etc/hostname

Our “/etc/hostname” file is shown in the image below:

The “/etc/hosts” file can be accessed with the following command:

$ sudo nano /etc/hosts

Our “/etc/hosts” file is shown in the image below:

As you can see from the contents of both files, our hostname is the same in both files, i.e., kbuzdar-VirtualBox. Once you have ensured that the hostname in your respective files is exactly the same, then you can try restarting the hostname.service once again. This time, it should not render the error.

Conclusion

This article enlightened you about the causes of the “failed to start hostname.service unit hostname.service is masked” error. Moreover, it also shared with you the simplest method through which you can get rid of this error in Linux.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.