News
Seven days of Black Friday madness — great deal from Serverspace!

THE NEW YEAR
WHEEL OF FORTUNE

Spin the wheel and win a guaranteed prize right now!

By registering, you are signing up to receiving emails.
WB
William Bell
May 28 2020
Updated December 11 2024

How to Change Hostname on Ubuntu 18.04

Ubuntu

The first thing we need to do is - to get the current hostname.

To display the current hostname - simply enter the following command:

hostnamectl
Get the current hostname
Screenshot №1. Get the current hostname

As you can see in the image above, the current hostname of this server is - wyldebuntu.

The following steps, described below, show how to change the hostname in Ubuntu 18.04

Change the hostname using command hostnamectl.

For example, to change the system static hostname to serverspace, you would use the following command:

sudo hostnamectl set-hostname serverspace
Change the system static hostname
Screenshot №2. Change the system static hostname

The hostnamectl command does not produce output. On success, 0 is returned, otherwise - a non-zero failure code.

Edit the /etc/hosts file.

Open the /etc/hosts file with preferred text editor and change the old hostname to the new one:

sudo nano /etc/hosts

127.0.0.1 serverspace:

Change the old hostname to the new one
Screenshot №3. Change the old hostname to the new one

Press ctrl+O and then Enter to save modified file and next press ctrl+X to exit from nano.

Edit the cloud.cfg file.

If the cloud-init package is installed you also need to edit the cloud.cfg file. This package is usually installed by default in the images provided by the cloud providers such as AWS and it is used to handle the initialization of the cloud instances.

To check if the package is installed simply run the following command:

ls -l /etc/cloud/cloud.cfg
Check if the package is installed
Screenshot №4. Check if the package is installed

If you see the following output it means that the package is not installed and no further action is required.

ls: cannot access '/etc/cloud/cloud.cfg': No such file or directory.

If the package is installed the output will look like the following:

-rw-r--r-- 1 root root 3169 May 21 17:35 /etc/cloud/cloud.cfg

If the package is installed the output will look like this
Screenshot №5. If the package is installed the output will look like this

In this case you’ll need to open the /etc/cloud/cloud.cfg file:

sudo nano /etc/cloud/cloud.cfg

Search for preserve_hostname and change the value from false to true.

How to Change Hostname on Ubuntu 18.04
Screenshot №6. How to Change Hostname on Ubuntu 18.04

Press ctrl+O and then Enter to save modified file and next press ctrl+X to exit from nano.

Verify the change

To verify that the hostname was successfully changed, once again use the hostnamectl command:

hostnamectl
Verify the change
Screenshot №7. Verify the change
Vote:
5 out of 5
Аverage rating : 5
Rated by: 4
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300
We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.