9

Failed to start hostname.service: Unit hostname.service is masked.

This happened after hostname update, tried to do a sudo service hostname start.

warlockdead
  • 131
  • 1
  • 1
  • 8
  • 1
    This looks like OS support, not a programming question. – melpomene Feb 14 '18 at 07:22
  • Indeed my description was insufficient. I have a python backup script that looks for hostnames, in order to proceed with the backup, and was failing in finding one,(error in script), and because of that I have asked this question. – warlockdead Feb 14 '18 at 10:45

4 Answers4

22

Update the hostname in both /etc/hostname file and /etc/hosts file before running service hostname restart.

On newer systems running systemd (e.g. Ubuntu 16.04), have to use:

sudo hostnamectl set-hostname new-host-name . In here, edit new-host-name with your host name.

Thusitha Sumanadasa
  • 1,669
  • 2
  • 22
  • 30
6

This happens when there is a mismatch in hostname which causes the system to fail the lookup of the hostname.

Make sure these files are in-order and reflect proper entry for your new hostname

  1. /etc/hostname
  2. /etc/hosts

say your older hostname is: hostname.old and the new one is hostname.new

then /etc/hostname should be as following

hostname.new

and /etc/hosts should contain an entry as follows:

...
127.0.0.1    hostname.new
...
anand
  • 1,506
  • 14
  • 28
  • 1
    Thank you anand , I have everything configured as you explained, my only mistake was to believe something is wrong(needed to run a python backup script, that looks for hostnames and was failing on finding the hostname.) – warlockdead Feb 14 '18 at 10:42
3

Nothing to worry about. I've got the same problem when I tried to rename my computer name (hostname) in :

  • /etc/hostname
  • /etc/hosts

after editing both files, the command sudo service hostname restart returned the same error.

I simply rebooted my computer and the saw the computer name (hostname) has been successfully changed. Just reboot your machine and you'll be fine.

smerllo
  • 3,117
  • 1
  • 22
  • 37
0

With latest Debian Stretch 9.6 you have to change /etc/hosts by yourself and also use hostnamectl. Here is a one-liner who does all stuff, original files are saved with date appended to the end. Maybe this works for other Distros

sudo -- bash -c 'echo "Please enter new Hostname"; old=$(hostname);read host; \
sed -i.$(date "+%H%M%S%d%m%y") -e "s:$old:$host:g" /etc/hostname;  \
sed -i.$(date "+%H%M%S%d%m%y") -e "s:$old:$host:g" /etc/hosts; \
hostnamectl set-hostname $host; echo "Old Name:$old and New Name is:$host"'

You should reboot after this to reflect all changes..

Jackfritt
  • 27
  • 6