How to Change the Hostname on Ubuntu 18.04 Server


In this tutorial, you will be shown ho to rename a Ubuntu 18.04 server., and how to ensure your changes persist after a system reboot. Much has changed since earlier releases of Ubuntu and some of these changes may catch system administrators off guard, including how to rename the server.

Much of Ubuntu 18.04 design changes were done to optimize the operating system for the cloud. A new cloud.cfg file has been introduced to ease the configuration of Ubuntu server instances on cloud platforms, and you will need to work with this paradigm.

Rename Server using hostnamectl

To rename an Ubuntu 18.04 server a new command is used, as part of the SystemD transition. The new command is named hostnamectl, and it is fairly simple to use.

To rename a server you use the following command.

sudo hostnamectl set-hostname NEW_HOSTNAME

For example, to change the hostname of the server to server01, you would use the following command.

sudo hostnamectl set-hostname server01

Unfortunately, there is still an additional step that must be taken. Otherwise, the new hostname will be reverted back to the server’s original hostname after a system reboot.

Persisting Hostname Changes

One would believe that using the hostnamctl command to change the hostname of an Ubuntu server is all that is needed. However, this is not the case with a new installation.

By default, Ubuntu will not preserve hostname changes, and any changes to the hostname will be lost when the system is rebooted. The reason for this is due to a new cloud configuration, which sets a value named preserve_hostname to false.

To enable persistent hostname changes we must turn set the preserve_hostname parameter to true.

Open the cloud.cfg file into a text editor, such as Vim.

sudo vi /etc/cloud/cloud.cfg

Change the value of preserve_hostname to true.

preserve_hostname: true

Save your changes and exit the text editor. When you restart the Ubuntu server the new hostname will now be preserved.