The hostname is simply a name assigned to the computer. The hostname can be used to identify the system on the network. While installing Ubuntu Desktop or Server system, it asks for the system or computer name which is the same as the hostname. We should assign the hostname to only one system in the same network i.e. the hostname is supposed to be unique within the same network. This tutorial provides the steps required to change the Hostname on Ubuntu 20.04 LTS using both command line i.e. terminal and graphical options. The steps should be similar on other versions of Ubuntu and Linux systems.
Hostname Rules
Only letters, digits, hyphen, and period are allowed
It must start and end with either a letter or a number
It must be between 2 and 63 characters long
Change Hostname - hostnamectl
We can use the command hostnamectl to check the hostname on Ubuntu systems as shown below.
# Hostname on VMware Virtual Machine ubuntu@ubuntupc:~$ hostnamectl Static hostname: ubuntupc Icon name: computer-vm Chassis: vm Machine ID: 11260de95fda41e4a902d0622a5334d6 Boot ID: 2e616eeeaee04a31a159f44004190aa4 Virtualization: vmware Operating System: Ubuntu 20.04 LTS Kernel: Linux 5.4.0-1009-aws Architecture: x86-64
# Hostname on AWS Virtual Machine ubuntu@ip-101-31-1-24:~$ hostnamectl Static hostname: ip-101-31-1-24 Icon name: computer-vm Chassis: vm Machine ID: ec215b20031efbe79957568a8cde1213 Boot ID: 4252f86ccdd842efa23fd664eb9ce877 Virtualization: kvm Operating System: Ubuntu 20.04 LTS Kernel: Linux 5.4.0-1009-aws Architecture: x86-64
We can change the hostname using the command hostnamectl as shown below.
# Change Hostname - Personal Usage sudo hostnamectl set-hostname alpha
# OR
# Change Hostname - Production Usage
sudo hostnamectl set-hostname mail.mydomain.com
sudo hostnamectl set-hostname server1.mydomain.com
sudo hostnamectl set-hostname server2.mydomain.com
It might be possible that your cloud service provider has disabled the change in the hostname. We can check the configuration in the file /etc/cloud/cloud.cfg as shown below.
# Open the Configuration using Nano Editor sudo nano /etc/cloud/cloud.cfg
# Content
----
----
# If this is set, 'root' will not be able to ssh in and they
# will get a message to login instead as the default $user
disable_root: true
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false ---- ----
We can see that the preserve_hostname is set to false which will revert back to the old hostname on system restart. Now update the value of preserve_hostname to true as shown below and press Ctrl + O -> Enter -> Ctrl + X to save cloud.cfg and exit the nano editor. Also, verify the change in hostname using the command hostnamectl.
# Hostname on VMware Virtual Machine ubuntu@ubuntupc:~$ hostnamectl Static hostname: alpha Icon name: computer-vm Chassis: vm Machine ID: 11260de95fda41e4a902d0622a5334d6 Boot ID: 2e616eeeaee04a31a159f44004190aa4 Virtualization: vmware Operating System: Ubuntu 20.04 LTS Kernel: Linux 5.4.0-1009-aws Architecture: x86-64
# Hostname on AWS Virtual Machine
ubuntu@ip-101-31-1-24:~$ hostnamectl
Static hostname: mail.mydomain.com
Icon name: computer-vm
Chassis: vm
Machine ID: ec215b20031efbe79957568a8cde1213
Boot ID: 4252f86ccdd842efa23fd664eb9ce877
Virtualization: kvm
Operating System: Ubuntu 20.04 LTS
Kernel: Linux 5.4.0-1009-aws
Architecture: x86-64
Change Hostname - hostname
Apart from hostnamectl, we can also change the hostname using the hostname command as shown below.
# Change Hostname - Personal Usage sudo hostname alpha
# OR
# Change Hostname - Production Usage
sudo hostname mail.mydomain.com
Verify the change in hostname using the command hostname as shown below.
# Change Hostname hostname
# Output - Personal Usage alpha
# OR
# Output - Production Usage mail.mydomain.com
We must also update the files /etc/hostname and /etc/hosts to preserve the change in hostname on system restart.
Update the file /etc/hostname as shown below.
# Update hostname sudo nano /etc/hostname
# Hostname - Personal Usage alpha
# OR
# Hostname - Production Usage mail.mydomain.com
Update the file /etc/hosts as shown below.
# Update hosts sudo nano /etc/hosts
# Hostname --- --- xx.xx.xx.xx alpha
# OR
xx.xx.xx.xx mail.mydomain.com mail --- ---
The hostname will be preserved by updating the files /etc/hostname and /etc/hosts and it remains the same on system restart.
Change Hostname - Desktop
This section provides the steps to change the hostname using the GUI provided by the Ubuntu 20.04 LTS Desktop. Click the Show Applications Icon at the bottom right of the desktop. It will show the applications as shown in Fig 1. You might be required to scroll down and find the Settings Application as shown in Fig 1.
Now click the Settings Icon as highlighted in Fig 1 and also click the Details Option at the bottom of the Left Sidebar as highlighted in Fig 2.
Click the Right Arrow next to the Device Name as highlighted in Fig 2. It will open the Dialog to change the Hostname as shown in Fig 3.
Now provide appropriate hostname and click the Rename Button to change it.
Summary
This tutorial provided the steps required to change or update the hostname on Ubuntu 20.04 LTS. It provided the steps to change the hostname using the commands hostnamectl and hostname. It also provided the steps to change the hostname graphically on the Ubuntu 20.04 LTS Desktop.