How to Change Hostname on Ubuntu Linux

|

|

The presented post instructs students and new Ubuntu Linux users on changing the hostname from the command terminal or console. A hostname, unique to each machine on a network, can be updated using a series of outlined commands. The tutorial can also apply to other Linux Debian-based systems. The writer underscores the importance of system…

This post shows students and new users steps to change the hostname on Ubuntu Linux from the command terminal or console. Ubuntu hostname is set at installation time or dynamically assigned for 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 using the same hostname on a network. 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.

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

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

Follow the steps below when you can change or rename Ubuntu Linux.

How to display the current hostname on Ubuntu Linux

Before changing or updating the hostname on Ubuntu, you may want to know the current name. You can find out the hostname on Ubuntu by running the commands below.

hostnamectl

That should display 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 rename the hostname on Ubuntu Linux

Now that you know the hostname for your Ubuntu machine, you can change or rename the machine with a simple command. You may have to edit multiple files to change the hostname on Ubuntu Linux.

To change the hostname on Ubuntu, run the commands below using the hostnamectl command.

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

sudo hostnamectl set-hostname vm1.ubuntuos.local

Two files should also be edited to change the Ubuntu computer name successfully. 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 one, 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 verify 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 might also install it. If it is installed, you will also need to edit the cloud.cfg file.

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

You may not see the cloud if you manually installed Ubuntu Linux. cfg file. In that case, skip the steps below.

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

sudo nano /etc/cloud/cloud.cfg

Then change the highlighted line to true.

The preserve_hostname in the /etc/cloud/cloud.cfg context should be true. It doesn’t preserve hostname changes across a reboot when set to false. Set to true or yes, and it preserves the new hostname when 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 exit.

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 the hostname on Ubuntu Linux. Please use the comment form below if you find any errors above or have something to add.


Discover more from Geek Rewind

Subscribe to get the latest posts to your email.

Like this:



Leave a Reply

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

Blog at WordPress.com.

Discover more from Geek Rewind

Subscribe now to keep reading and get access to the full archive.

Continue reading