This article was first published in: https://www.itcoder.tech/posts/how-to-change-hostname-on-ubuntu-20-04/
Usually the host name is set during the installation of the operating system or dynamically specified during the creation of the virtual machine.
This guide explains how to set or change the hostname on Ubuntu 20.04 without rebooting the system.
A host name is the label name that marks a device on the network. In the same network, you should not have two or more machines with the same hostname.
In Ubuntu, you can use the hostnamectl
command to edit the system hostname and related settings. This tool recognizes three different host names:
static
-the traditional host name. It is stored in the /etc/hostname
file and can be set by the user pretty
-a free-form UTF8 hostname used to represent the user. For example: Linuxize's desktop
. transient
-The dynamic host name maintained by the kernel. During operation, the DHCP or mDNS server can change the transient host name. By default, it is exactly the same as the static hostname.We strongly recommend using the full domain name (FQDN) to set static
and transient
names, such as host.example.com
.
Only root or users with sudo authority can modify the system hostname.
To view the current host name, run the hostnamectl
command without any parameters:
hostnamectl
In this example, the current hostname is set to: ubuntu2004.localdomain
.
Modifying 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, if you want to modify the hostname of the system today to neptune.linuxize.com
, you can use the following command:
sudo hostnamectl set-hostname neptune.linuxize.com
You can also set a pretty hostname:
sudo hostnamectl set-hostname "Linuxize's laptop"--pretty
hostnamectl
does not produce any output. If successful, return 0, otherwise, return a non-zero error code.
The static hostname is stored in /etc/hostname
, and the pretty hostname is stored in the /etc/machine-info
file.
You should not use the same hostname on different machines on the same network.
In most systems, the host name is mapped to 127.0.0.1
in /etc/hosts
. Open the file and modify the old hostname to the new hostname.
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 you are running Ubuntu on a cloud instance and have the cloud-init
package installed, you can also edit the /etc/cloud/cloud.cfg
file. This software package is provided by the Cloud Server vendor and is usually installed by default, and it can be used to handle the initialization of the cloud server instance.
If the file exists on your system, open it:
sudo nano /etc/cloud/cloud.cfg
Search for "preserve_hostname" and change the value from false
to true
.
# This will cause the set+update hostname module to not operate(iftrue)
preserve_hostname:true
Save the file and close the editor.
To verify that the hostname has been completely modified, enter the hostnamectl
command:
hostnamectl
Your new hostname will be printed out on the terminal.
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
We have shown you how to easily modify the hostname on Ubuntu 20.04 without rebooting the machine.
There are many reasons why you need to change the hostname. The most common reason is that the host name needs to be set automatically when the instance is created.
Original: https://linuxize.com/post/how-to-change-hostname-on-ubuntu-20-04/
Copyright notice: This work is licensed under the Creative Commons Attribution-Share in the Same Way 4.0 International License Agreement.
If you have any questions, please contact us in the following ways:
WeChat:
WeChat group: add the above WeChat, remark the WeChat group
QQ: 3217680847
QQ Group: 82695646
Recommended Posts