This post shows students and new users the steps to change the hostname in Ubuntu Linux from the command terminal or console. The Ubuntu hostname is set at installation time or dynamically assigned to cloud servers when new instances are created.

A hostname is a unique label that identifies a machine on the network. You shouldn’t have two machines with the same hostname on a given network. If you decide that the name chosen at installation or automatically assigned is not what you want, you can follow the steps below to upgrade to a new name.

Although this tutorial is written for Ubuntu Linux, it should also apply other Debian Linux-based systems.

Also, for students and new users learning Linux, the easiest place to start learning is Ubuntu Linux. Ubuntu is the modern open source Linux operating system for desktops, servers, and other devices.

When you’re ready to change or rename Ubuntu Linux, follow the steps below.

How to display current hostname in Ubuntu Linux

Before changing or updating the hostname in Ubuntu, you may want to know what the current name is. You can find out the hostname in Ubuntu by running the following commands.

hostnamectl

That should show something similar to the lines below with a hostname for your Ubuntu computer.

   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

How to change the hostname in Ubuntu Linux

Now that you know the hostname of your Ubuntu machine, you can change or rename the machine with a simple command. There are several files that you may have to edit to change the hostname in Ubuntu Linux.

To quickly change the hostname in Ubuntu, run the following commands using the hostnamectl command.

For example, to change the hostname to vm1.ubuntuos.local, run the following commands.

sudo hostnamectl set-hostname vm1.ubuntuos.local

There are two files that must also be edited to correctly rename the Ubuntu computer. These files are located in the / etc directory. Are:

/etc/hostname
/etc/hosts

Edit / etc / hosts and update the filename there.

sudo nano /etc/hosts

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

  GNU nano 4.8                       /etc/hosts                                 
127.0.0.1       localhost
127.0.1.1       vm1.ubuntuos.local

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

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

sudo nano /etc/hostname

Some cloud providers will install the cloud-init package. Ubuntu servers could also install it. If installed, you will also need to edit the cloud.cfg proceedings.

If that file exists on your system, open it and change the lit line to preserve the hostname.

If you installed Ubuntu Linux manually, you may not see the cloud.cfg proceedings. In that case, skip the steps below.

On an Ubuntu cloud host computer with cloud.cfg configuration file, run the following commands to open the file.

sudo nano /etc/cloud/cloud.cfg

Then change the highlighted line to certain.

the preserve_hostname in the /etc/cloud/cloud.cfg the context should be set in certain. When set to fake, does not preserve hostname changes during a reboot. Set to true or yes, it retains the new hostname when it reboots.

# 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 close

Finally, restart the server for the new name to take effect.

sudo reboot

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

Conclusion:

In this tutorial we have seen how to change the hostname in Ubuntu Linux. If you find any errors above or have something to add, use the comment form below.

Write A Comment