Hostname is set during the operating system installation or dynamically assigned to the virtual machine when it is created.
This guide explains how to set or change hostnames on Ubuntu 20.04 without the need to restart the system.
Hostnames are labels that identify devices on the network. You should not have two or more machines with the same hostname, on the same network.
In Ubuntu, you can edit the system host name and related settings using the hostname command. This tool recognizes three different hostname classes:
- static – Traditional hostname. This is stored in the file / etc / hostname and can be set by the user.
- pretty – The free form UTF8 host name used for presentations to users. For example, uberhowto laptops.
- transient – The dynamic hostname managed by the kernel. DHCP or mDNS servers can temporarily change hostnames at run time. By default, this is the same as a static hostname.
It is recommended to use a fully qualified domain name (FQDN) such as host.example.com for static and temporary names.
Only root or users with sudo rights can change the system hostname.
Displays Current Host Name
To see the current hostname, activate the hostname command without any arguments:
In this example, the current hostname is set to ubuntu2004.localdomain.
Change the System Hostname
Changing the system hostname is a simple process. 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 example, to change the system static hostname to neptune.uberhowto.com, you will use the following command:
sudo hostnamectl set-hostname neptune.uberhowto.com
Optionally, you can also set a beautiful hostname:
sudo hostnamectl set-hostname "uberhowto's laptop" --pretty
hostnamectl does not produce output. If successful, 0 is returned, not a zero failure code.
Static hostnames are stored in / etc / hostname, and beautiful hostnames are stored in the / etc / machine-info file.
You may not use the same hostname on two different machines on the same network.
On most systems, hostnames are mapped to 127.0.0.1 in / etc / hosts. Open the file and change the name of the old host to a new one.
127.0.0.1 localhost 127.0.0.1 neptune.uberhowto.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 you run Ubuntu on a cloud instance and the cloud-init package is installed, you also need to edit the /etc/cloud/cloud.cfg file. This package is usually installed by default in images provided by cloud providers, and is used to handle cloud instance initialization.
If the file is on your system, open it:
sudo nano /etc/cloud/cloud.cfg
Look for 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 and close your editor.
To verify the hostname has completely changed, enter the hostname command:
Your new hostname will be printed at the terminal:
Static hostname: neptune.uberhowto.com Pretty hostname: uberhowto'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
We have shown you how to easily change the hostname in an Ubuntu 20.04 installation without restarting the machine.
There are a number of reasons why you might need to change your hostname. The most common is when the hostname is automatically assigned to the instance creation.
Feel free to leave a comment if you have questions.