How to change hostname in Ubuntu using command line? Learn how to use Linux hostname command command to find hostname in Ubuntu or to change hostname in Ubuntu.
What is hostname
A hostname is a name of label which is assigned to a machine and this label is used to identify the machine on the network. Each device has a unique hostname in the network.
Displaying the current hostname
On Ubuntu Linux systems, the hostnamectl command is used to dipslay and edit the hostname and all its related settings and arguments. To display the current hostname run the hostnamectl command without any arguments:
Upon successful execution, the system hostname will be displayed as:
Changing the hostname
If you wish to change the hostname, you can do it using the hostnamectl command. Note that you will need the root (sudo) privileges to change the system hostname.
Using the command is very easy. For example, to change the system hostname to sourcedigit_2020, you can use the following command:
sudo hostnamectl set-hostname sourcedigit_2020
Once executed, verify that the hostname using the hostnamectl command:
Ubuntu users can also edit the /etc/hosts file or cloud.cfg file to change the old hostname to the new one.
hostnamectl may be used to query and change the system hostname and related settings. This tool distinguishes three different hostnames: the high-level “pretty” hostname which might include all kinds of special characters (e.g. “Lennart’s Laptop”), the static hostname which is used to initialize the kernel hostname at boot (e.g. “lennarts-laptop”), and the transient hostname which is a fallback value received from network configuration. If a static hostname is set, and is valid (something other than localhost), then the transient hostname is not used.
Note that the pretty hostname has little restrictions on the characters used, while the static and transient hostnames are limited to the usually accepted characters of Internet domain names. The static hostname is stored in /etc/hostname and the pretty hostname, chassis type, and icon name are stored in /etc/machine-info.
With no arguments, ‘hostname’ prints the name of the current host system. With one argument, it sets the current host name to the specified string. You must have appropriate privileges to set the host name.
hostnamectl command arguments
- –static, –transient, –pretty: If status is used (or no explicit command is given) and one of those fields is given, hostnamectl will print out just this selected hostname. If used with set-hostname, only the selected hostname(s) will be updated. When more than one of those options is used, all the specified hostnames will be updated.
- status: Show current system hostname and related information.
- set-hostname NAME: Set the system hostname to NAME. By default, this will alter the pretty, the static, and the transient hostname alike; however, if one or more of –static, –transient, –pretty are used, only the selected hostnames are changed. If the pretty hostname is being set, and static or transient are being set as well, the specified hostname will be simplified in regards to the character set used before the latter are updated. This is done by replacing spaces with “-” and removing special characters. This ensures that the pretty and the static hostname are always closely related while still following the validity rules of the specific name. This simplification of the hostname string is not done if only the transient and/or static host names are set, and the pretty host name is left untouched.
The three classes of hostname recognized by the hostnamectl command are:
- static – It is the traditional hostname and is stored in the /etc/hostname file. This can be set by the user.
- pretty – It is a free-form UTF8 hostname used for presentation to the user.
- transient – It is the dynamic hostname maintained by the kernel. By default, it is the same as the static hostname.