This post shows students and new user 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 given 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 also apply other Linux Debian based systems.
Also, for students and new users learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern, open source Linux operating system for desktop, servers and other devices.
When you’re ready to change or rename Ubuntu Linux, follow the steps below.
How to display current hostname on Ubuntu Linux
Before changing or updating the hostname on Ubuntu, you may want to know what the current name is. You can find out the hostname on Ubuntu by running the commands below.
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 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. There are multiple files that you may have to edit to change the hostname on Ubuntu Linux.
To quickly change the hostname on Ubuntu, run the commands below using the hostnamectl command.
For example, to change the hostname to vm1.ubuntuos.local, run the commands below.
sudo hostnamectl set-hostname vm1.ubuntuos.local
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:
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 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 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.
If you manually installed Ubuntu Linux, then you may not see the cloud.cfg file. In that case, skip the steps below.
On cloud host Ubuntu computer with 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 set to true. When it is set to false, it doesn’t preserve hostname changes across a reboot. Set to true or yes, and it preserves the new hostname when you reboot.
# 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.
When you log back in, the system should have a new name you assigned.
This post showed you how to change the hostname on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.