Tag Archives: hostname

Changing the Hostname in Ubuntu Server 16.04: Unit hostname.service is masked Tips, Tricks and Tutorials 03 OCT 2017

For older versions of Ubuntu, many of the tutorials online show you how to edit your server’s hostname by simply editing /etc/hostname and then restarting the service with “service hostname restart”.

However, attempting this on a 16.04 Ubuntu Server build will most likely fail with the following error message:

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

As it turns out, the new way of changing your hostname on systems running systemd (i.e. Ubuntu 16.04) requires you to use the hostnamectl command, meaning that to set your new hostname you need to run:

sudo hostnamectl set-hostname NEWNAME

(Where obviously NEWNAME is the new/desired hostname that you want to use).

You can check that the change has been affected by running:

hostname

Worth jotting down here for future reference.

Ubuntu Server: How to Change Your Hostname CodeUnit 05 AUG 2010

Changing the hostname of your Ubuntu server installation is a pretty simple affair.

To view the current hostname of your system, simply enter hostname.

Now to affect a termporary change of hostname, you could simply use:

sudo hostname mynewhostname

However, be careful here as this is really just a temporary hostname change and won’t be saved when the system restarts. The reason for this is because the hostname is usually set on system startup when the system loads the detail from a file, generally located at “/etc/hostname”.

So in order to permanently change your system’s hostname, you need to edit the correct host-related files like so:

sudo nano /etc/hosts

Set your new desired hostname next to the entry for 127.0.1.1 (leaving the 127.0.0.1 localhost entry as is). Next, run:

sudo nano /etc/hostname

Simply enter your desired hostname on a single line and save.

Once you have made your changes and saved both of the files, you should be good to go – as you can test out for yourself by simply rebooting your machine with a good old sudo reboot and then checking with hostname!

Nifty.