Read in 2 minutes
How to Set or Change Hostname in Linux
By default, the system hostname is set during the installation process, or if you are creating a virtual machine it is dynamically assigned to the instance at startup, but there are situations when you need to change it.
This tutorial will walk you through the process of changing the hostname in Linux without the need of restarting the system. The instructions should work on any modern Linux distribution that uses systemd.
What is Hostname
A hostname is a label assigned to a machine that identifies the machine on the network. Each device in the network should have a unique hostname.
The hostname can be a simple string containing alphanumeric characters, dots and hyphens. If the machine is connected to the Internet (such as web or mail server) it is recommended to use a fully qualified domain name (FQDN) as a system hostname. The FQDN consists of two parts, the hostname, and the domain name.
Displaying the Current Hostname
On Linux systems using systemd, the
hostnamectl command can be used to query and change the hostname and related settings on a given machine.
To view the current hostname, invoke the
hostnamectl command without any arguments:
The system hostname is highlighted in the image below:
Changing the Hostname
To change the hostname invoke the
hostnamectl command with the
set-hostname argument followed by the new hostname. Only the root or a user with sudo privileges can change the system hostname.
For example, to change the system hostname to
mail.linuxize.com, you would use the following command:
sudo hostnamectl set-hostname mail.linuxize.com
hostnamectl command does not produce output. On success,
0 is returned, a non-zero failure code otherwise.
Finally, to verify that the hostname was successfully changed, once again use the
The new system hostname and some additional system information such as the kernel version will be displayed on the terminal.
Static hostname: mail.linuxize.com Icon name: computer-vm Chassis: vm Machine ID: 70a3f06298014fd9ac42e5dc1de1034a Boot ID: 6d45a1a8d436418e97519da01ea61c1b Virtualization: oracle Operating System: Debian GNU/Linux 10 (buster) Kernel: Linux 4.19.0-5-amd64 Architecture: x86-64
In this tutorial, we have shown you how to change the hostname of your Linux system. Depending on your Linux distribution and the virtualization type, additional steps may be required to complete the process.
For distro-specific instructions, check the following articles:
Feel free to leave a comment if you have any questions.