Choose a different version or distribution
Introduction
The hostname is either predetermined during operating system installation or dynamically provided to the virtual machine when it is created.
In this tutorial, you will change hostname on Ubuntu 20.04.
Understanding Host Names
A device on a network can be identified by its hostname. Duplicate hostnames should not be used on the same network.
Using the hostnamectl
command on Ubuntu, you can change the system hostname and associated settings. This tool can distinguish between three classes of hostnames: static
, transient
, and pretty
.
static
: The standard hostname. It can be set by the user and is kept in the/etc/hostname
file.pretty
: A descriptive free-form UTF 8 hostname used for user presentation. For example,Vegastack's laptop
.transient
: A dynamic hostname that the kernel maintains. The transient hostname can be modified at runtime by mDNS or DHCP servers. It is the same as thestatic
hostname by default.
For both static
and transient
names, it is advised to use a fully-qualified domain name (FQDN
), such as host.example.com
.
The system hostname can only be modified by root or users with sudo privileges.
Displaying the Current Hostname
Use the hostnamectl
command without any arguments to view the current hostname:
hostnamectl
Changing the System Hostname
The system hostname can be easily modified. The syntax is as follows:
sudo hostnamectl set-hostname host.example.com
sudo hostnamectl set-hostname "Your Pretty HostName" --pretty
sudo hostnamectl set-hostname host.example.com --static
sudo hostnamectl set-hostname host.example.com --transient
For instance, you would use the following command to modify the system's static hostname to v1.vegastack.com
:
sudo hostnamectl set-hostname v1.vegastack.com
You can also set the pretty hostname:
sudo hostnamectl set-hostname "VegaStack's's laptop" --pretty
No output is generated by hostnamectl
. When successful, 0
is returned; otherwise, a non-zero failure code is returned.
The pretty hostname is kept in the /etc/machine-info
file, while the static hostname is kept in /etc/hostname
.
The same hostname should not be used on two different machines connected to the same network.
The hostname is mapped to 127.0.0.1
in /etc/hosts
on most of the systems. Open the file and update the old hostname to the new one.
127.0.0.1 localhost
127.0.0.1 neptune.linuxize.com
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
If the cloud-init
package is installed and Ubuntu is operating on a cloud instance, you also need to make changes to the /etc/cloud/cloud.cfg
file. This package, which manages the initialization of the cloud instances, is generally installed by default in the images provided by the cloud providers.
If the file is already on your computer, open it:
sudo vim /etc/cloud/cloud.cfg
Find preserve_hostname
, and change the value from false
to true
:
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true
Save the file, then exit your editor.
Verify the change
Enter the hostnamectl
command to confirm the hostname has been fully changed:
hostnamectl
Your new hostname will appear on the terminal as follows:
Output
Static hostname: neptune.linuxize.com
Pretty hostname: Linuxize's desktop
Icon name: computer-vm
Chassis: vm
Machine ID: a04e3543f3da460294926b7c41e87a0d
Boot ID: aa31b274703440dfb622ef2bd84c52cb
Virtualization: oracle
Operating System: Ubuntu 20.04 LTS
Kernel: Linux 5.4.0-26-generic
Architecture: x86-64
Conclusion
You have learned how to easily change the hostname on Ubuntu 20.04 installation without restarting the machine.
You might need to change the hostname for a number of reasons. The hostname is most commonly automatically specified when an instance is created.
If you have any queries, feel free to leave a comment below, and we'll be happy to help.