How to Change the Hostname on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to change Ubuntu hostname. If you’re running Ubuntu 20.04 | 18.04, then this post should come in handy.

Ubuntu hostname is set at installation time or dynamically assigned for cloud servers when new instances are created.

If you decide that the name chosen at installation time or assigned automatically is not what you want, you can follow the steps below to update to a new name.

Hostname should be unique on a network and should identify individual machine. There should not be two machines with same hostname.

To change your server name, follow the steps below:

Step 1: Display Current Hostname

Before changing or updating your system name you may want to know what the current name is. To find out your system name, simply run the commands below:

hostnamectl

That should display something similar to the lines below:

   Static hostname: ubuntu2004
         Icon name: computer-vm
           Chassis: vm
        Machine ID: e280aedec6a247d3a1d4beb4f85576bb
           Boot ID: b794a939b6264a5ea7ce18eae9c130d7
    Virtualization: oracle
  Operating System: Ubuntu 20.04 LTS
            Kernel: Linux 5.4.0-26-generic
      Architecture: x86-64

Step 2: Change Hostname

Now that you know the current hostname, you can then use the commands below to change it.

There are multiple places to change the hostname. The commands below will change the hostname to a new name.

sudo hostnamectl set-hostname new_hostname

Replace new_hostname with the new name you want for the system.

There are two files that should also be edited to successfully change Ubuntu computer name. These files are located in the /etc directory. They are:

/etc/hostname
/etc/hosts

Edit /etc/hosts and update the file name there.

sudo nano /etc/hosts

Then replace the old name with the new, then save the file and exit.

  GNU nano 4.8                       /etc/hosts                                 
127.0.0.1       localhost
127.0.1.1       new_hostname

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback

Then verify that the new hostname is also in the file below:

sudo nano /etc/hostname

Some cloud providers and will install the cloud-init package. Ubuntu servers might also it install it. If it is installed you will also need to edit the cloud.cfg file.

If there’s such file on your system, then open it and change the lighted line to preserve the hostname.

To do that, run the commands below:

sudo nano /etc/cloud/cloud.cfg

Then change the line:

# The top level settings are used as module
# and system configuration.

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

# Example datasource config
# datasource:
.............................

Save the file and exit

Step 3: Restart the Server

Finally, restart the server for the new name to apply.

sudo reboot

When you log back in, the system should have a new name you assigned.

Conclusion:

This post showed you how to change Ubuntu hostname. If you find any error above, please use the comment form below to report.

Thanks,

You may also like the post below:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.