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 puts some formatting-related restrictions on this name.

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 network configuration. Linux applies the same formatting-related restrictions on this name that it applies to the static hostname.

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 respectively. 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 not present, Linux automatically picks the next 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 not available, then it uses the static hostname. If the static hostname is also not available, then 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 not available, then they use the transient hostname.

Services running on other computers of the network 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 most preferred and recommended methods are the following.

  • 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. To change the static hostname, you can change this word.

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

Both the /etc/hostname and /etc/machine-info are regular text files. You can use any text editor to edit these files.

The systctl command

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

The hostname command

By default, this command works with the transient hostname. But if the static hostname is configured, it gives priority to the static name over the transient name. Without any option, if the static hostname is configured, it displays the static hostname. If the static hostname is not configured, it displays the transient hostname.

how hostname command work

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

The nmtui utility

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

The hostnamectl command

This is the default command to manage hostnames. You can use this command to view, delete, update, and manage all three types of hostnames.

The settings tool

The GNOME desktop package includes the setting tool. The setting tool includes an option to manage the hostname. By default, it displays the pretty hostname. If the pretty hostname is not available, it displays the static hostname. If both the pretty and static hostnames are not available, it displays the transient hostname.

setting tool

You can also use this tool to update the pretty and static hostnames. It provides an input box to type the name. It uses the name you type in the box as it is 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

Setting up a system for practice

To learn how hostnames work and how to manage them, we will take some examples. If you want to repeat these examples, use a system that is not part of the production environment or use a virtual system.

To learn how to set up a virtual lab for practice, you can check the following tutorial.

RHCE Practice Lab Setup in VMWare and in VirtualBox

Viewing and changing hostnames

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

delete static and pretty hostname

Remove the /etc/machine-info file.

remove pretty hostname

Restart the system.

reboot the system

We have deleted the pretty and static hostnames. We did not configure the transient hostname. In a situation where the pretty and static hostnames are absent and the transient hostname is not configured, Linux 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 not configured, 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.

If the GNOME desktop package is installed, you can change the hostname by using the settings tool. Click Activities, search for settings, and click the Settings icon.

starting the setting tool

The settings tool opens in a window. The About section of the window provides information about the system. This information includes the hostname.

setting tool hostname

When we use the setting tool to update the pretty hostname, it checks the /etc/machine-info file. If the file exists, it updates the file. If the file does not exist, 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####'.

update pretty hostname

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

verify pretty hostname

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

Managing the static hostname

To update the static hostname, you can edit the /etc/hostname file. 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. To force it to reread this file, we need to restart the NetworkManager service.

Restart the NetworkManager service and check the static hostname again. To check the static hostname, you can use the hostname command. If the static hostname is configured, it displays that name. It displays the transient name only if the static hostname is not configured.

#systemctl restart NetworkManager
#hostname

verify static hostname

You can also use the nmtui utility to update the static hostname. Use the following command to start it.

#nmtui

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

nmtui set description

To navigate between options, you can use the arrow keys or the tab key.

Change the hostname to test.com.

change hostname

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

exit nmtui

To learn more about the nmtui utility, you can check the following tutorial.

The nmcli command on Linux Examples and Usages

The nmtui command updates the /etc/hostname file. To verify it, you can check this file again or can use the hostname command.

verifying static hostname

Managing the transient hostname

As I mentioned earlier, 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.

Let's view and update 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. It will reset the transient hostname from the network configuration when we will restart the system.

To verify it, restart the system and check the transient hostname again.

#reboot -f

The following image shows the above exercise.

updating transient name

Using the hostnamectl command

The hostnamectl command is versatile. We can use it to view, edit, and manage all three types of hostnames. Without any option, it displays all configured hostnames with the system information.

#hostnamectl

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

#hostnamectl status

hostnamectl command examples

To view and manage a specific hostname, we need to use the hostname-specific option with this command.

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

#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 --transient transient.com

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

#hostnamectl --pretty pretty.com

The following image shows the above commands with output.

hostnamectl command

If you want to set the same name for the static and transient hostnames, do not specify the hostname-specific option. 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

In this tutorial, we discussed the types of the hostname Linux uses. Later, we learned how to view, change, and update each via all available methods. By using these methods, you can easily manage the hostname of a Linux.

ComputerNetworkingNotes Linux Tutorials How to change the hostname on Linux