news
Serverspace has became Free TON validator, launched DePool, and gives bonuses for TON Crystals holders

How to Change Hostname on Ubuntu 18.04

WB
William Bell
May 28, 2020

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

You should see your new server name printed in the output.

Start Your Cloud Journey Migration made simplified. Take the first step right now.
We use cookies to provide our services and for analytics and marketing. To find out more about our use of cookies, please see our Privacy Policy. By continuing to browse our website, you agree to our use of cookies.