How to change the hostname on Linux

Linux uses three types of hostnames. These types are pretty, static, and transient.

The pretty hostname is a name the user uses to refer to the local system. A user can choose and configure any name as the pretty hostname. Linux uses this name only to interact with the user. It does not put any formatting-related restrictions on this name.
A static hostname is a name the services running on the local system use to refer to the local system. Linux restricts this name in some formatting ways.
A transient hostname is a name that other computers on the network use to refer to the local system. Linux dynamically picks this name from the network configuration and applies the same formatting-related restrictions.

Restrictions for the static and transient hostnames

Linux sets the following rules and recommendations for the static and transient hostnames.

  • A hostname can be a UTF8 string up to 64 characters in length.
  • It can contain only letters (a-z, A-Z), numbers (0-9), dashes (-), underscores (_), and dots (.).
  • It cannot start and end with a dot.
  • It cannot have two dots immediately following each other.

Hostname configuration files

Linux stores the static and pretty hostnames in the /etc/hostname and /etc/machine-info files. Unlike static and pretty hostnames, it does not use a configuration file to store the transient hostname. It stores the transient hostname in a kernel variable. The name of this variable is the kernel.hostname. The default value of this variable is localhost.localdomain.

Hostname priority

In the priority order, Linux puts the pretty hostname at the top, the static hostname in the middle, and the transient hostname at the bottom. If one hostname is absent, Linux automatically picks the following available hostname in the priority order.

If the pretty hostname is available, Linux always uses it to interact with the user. If the pretty hostname is unavailable, it uses the static hostname. If the static hostname is also unavailable, it uses the transient hostname. If the static hostname is available, services running on the local system always use it to represent the local system. If the static hostname is unavailable, they use the transient hostname.

Services on other network computers use the transient hostname to interact with the local system. A transient hostname always has a value. If the network configuration is available, Linux sets the transient hostname from the network configuration. If the network configuration is absent, it uses the default value (localhost.localdomain) for the transient hostname.

Commands and utilities for the hostname management

There are multiple ways to view, edit, and manage the hostname. The following are the most preferred and recommended methods.

  • Editing the /etc/hostname file
  • Editing the /etc/machine-info file
  • Changing the kernel.hostname option
  • Using the sysctl command
  • Using the hostname command
  • Using the nmtui utility
  • Using the hostnamectl command
  • Using the settings tool

Let's understand these methods in detail.

The /etc/hostname is a regular configuration file. It stores only the static hostname. It does not use any configuration directive for the static hostname. It saves the static hostname as a single word or text string in the first line. We can change the static hostname by changing this word.

The /etc/machine-info is a dynamic configuration file. Linux generates it only when we configure the pretty hostname. If we delete the pretty hostname, it automatically deletes this file. This file uses the PRETTY_HOSTNAME directive to store the pretty hostname.

Both are regular text files. We can use any text editor to edit these files.

The systctl command

This command allows us to view and change kernel parameters. The kernel stores the transient hostname in a variable and updates its value from the network configuration. To change the transient hostname, we can manually update the value of this variable or use the sysctl command to update the value of this variable.

The hostname command

By default, this command works with the transient hostname. But if we configure the static hostname, it prioritizes the static name over the transient name. Without any option, if the static hostname is available, it displays it. If not, it shows the transient hostname.

How the hostname command works

We can also use this command to update the transient hostname.

The nmtui utility

NetworkManager provides this utility to manage essential network settings. It includes an option to set the static hostname and works only with it. We can use it to view and update the static hostname.

The hostnamectl command

It is the default command for managing hostnames. We can use this command to view, delete, update, and manage all three types of hostnames.

The settings tool

The GNOME desktop package includes the Settings tool. It has an option to manage the hostname. By default, it displays the pretty hostname. If the pretty hostname is not available, it shows the static hostname. If both are absent, it displays the transient hostname.

The Setting tool

This tool allows us to update the pretty and static hostnames. It provides an input box to type the name. It uses the typed name to set the pretty hostname. To set the static hostname, it removes all formatting from the name. It replaces all symbols and spaces with dashes.

Generating the static hostname from the pretty hostname

Viewing and changing hostnames

Access a terminal and open the /etc/hostname file. Delete the static hostname and save the file.

Deleting static and pretty hostnames

Remove the /etc/machine-info file.

Removing pretty hostname

Restart the system.

Rebooting the system

We deleted the pretty and static hostnames and did not configure the transient hostname. In such a case, it uses the default transient hostname localhost.localdomain.

To verify it, we can check configuration files.

#cat /etc/hostname
#cat /etc/machine-info

To view the transient hostname, we can use the following command.

#sysctl kernel.hostname

We can also use the hostname command to view the static and transient hostname. If the static hostname is absent, it displays the transient hostname.

#hostname

If we need more confirmation, we can use the hostnameclt command to view each hostname separately.

The following command shows the above commands with output.

Default hostname

Managing the pretty hostname

The easiest way to manage the pretty hostname is to use the Settings tool. Click Activities, search for settings, and click the Settings icon.

Settings tool

The Settings tool opens in a window. The About section provides information about the system, including the hostname.

Settings tool

When we use the Settings tool to update the pretty hostname, it checks the /etc/machine-info file. If the file exists, it updates it. If not, it creates the file and updates it. It also generates the static hostname from the pretty name and updates the /etc/hostname file. To generate the static hostname from the pretty hostname, it removes all formatting and replaces all symbols and spaces with dashes.

To verify it, set the pretty hostname to 'This is My #### Linux & System####'.

Updating pretty hostname

Check the /etc/machine-info and /etc/hostname files.

Verifying pretty hostname

As you can see in the above output, it saved the pretty name as we typed but removed all formatting from the static hostname.

Managing the static hostname

We can edit the /etc/hostname file to update the static hostname. Open the /etc/hostname file and set the static hostname to example.com.

Updating static hostname

NetworkManager does not actively monitor the /etc/hostname file. It reads this file only when it starts. We need to restart the NetworkManager service to force it to reread this file.

Restart the NetworkManager service and recheck the static hostname. Use the hostname command to check the static hostname. If the static hostname is available, it displays that name. If not, it shows the transient name.

#systemctl restart NetworkManager
#hostname

Verifying static hostname

We can also use the nmtui utility to update the static hostname. The following command starts it.

#nmtui

Select the third option (Set system hostname) and press Enter.

Setting the hostname

We can use the arrow keys or the tab key to navigate between options.

Change the hostname to test.com.

Changing hostname

Select OK and press the Enter key to confirm the change. Select Quit and press the Enter key to exit the nmtui utility.

Exit the nmtui

The nmtui command updates the /etc/hostname file. To verify it, recheck this file or use the hostname command.

Verifying static hostname

Managing the transient hostname

As we know, Linux does not use a configuration file to store the transient hostname. It stores the transient hostname in the kernel.hostname variable. We can use the sysctl command to view and update the value of this variable.

The following commands change the transient hostname to sample.com.

#sysctl kernel.hostname
#systct kernel.hostname=sample.com

We can also use the hostname command to update this value.

#hostname
#hostname linux.com
#hostname

When we use the kernel.hostname variable or the hostname command to change the transient hostname, Linux updates the transient hostname only in the current session. When we restart the system, it resets the transient hostname from the network configuration.

To verify it, restart the system and recheck the transient hostname.

#reboot -f

The following image shows the above exercise.

Updating transient name

Using the hostnamectl command

The hostnamectl command is versatile. It can be used to view, edit, and manage all three types of hostnames. Without any options, it displays all configured hostnames with system information.

#hostnamectl

We can also use the status option to see the same information.

#hostnamectl status

The hostnamectl command example

To view and manage a specific hostname, we need to use the hostname-specific option with this command. Alternatively, you can view only the pretty hostname from the --pretty option.

#hostnamectl --pretty

Use the --static option to view only the static hostname.

#hostnamectl --static

Use the --transient option to view only the transient hostname.

#hostnamectl --transient

We can use the set-hostname option to update hostnames. The following command sets the static hostname to static.com.

#hostnamectl set-hostname --static static.com

The following command sets the transient hostname to transient.com

#hostnamectl set-hostname --transient transient.com

The following command sets the pretty hostname to pretty.com.

#hostnamectl set-hostname --pretty pretty.com

The following image shows the above commands with output.

The hostnamectl command

Do not specify the hostname-specific option if you want to set the same name for the static and transient hostnames. Use only the set-hostname option. Without a hostname-specific option, this option sets the static and transient hostnames to the specified name and erases the pretty hostname.

The following command sets the static and transient hostnames to Server1 and removes the pretty hostname.

#hostnamectl set-hostname Server1

The following image shows this exercise.

hostnameclt pretty hostname

Conclusion

A hostname is a crucial configuration setting that plays a vital role in identifying a device within a network. In this tutorial, we explored the various types of hostnames utilized by Linux systems, detailing their functions and significance. We also covered comprehensive methods for viewing, modifying, and updating hostnames, ensuring users can effectively manage their system's identity.

ComputerNetworkingNotes Linux Tutorials How to change the hostname on Linux

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com