1. Overview

Each instance of the Linux system is identified by its hostname. This name is not only used when networking but also improves the user experience.

In this tutorial, we’ll take a look at ways to get and set a hostname.

2. The hostname Command and the /etc/hostname File

The location for the hostname is file /etc/hostname. Let’s check it directly with cat:

$ cat /etc/hostname

So the name of our Linux system is ‘fedora35’.
In addition, we can use hostname for that:

$ hostname

And finally, we may put the hostname into the command line prompt, setting the PS1 variable to include the \h placeholder:

$export PS1='\u@\h:\w\$'

3. How to Set the Hostname

Let’s just edit the /etc/hostname file to change the hostname. To do that, we should be root or the sudo user. Then, the modification is immediately reflected by the hostname command and isn’t lost after rebooting.

Furthermore, we can use hostname to set the name:

$ sudo hostname fedorabox
$ hostname

However, we’re going to lose this setting after the system restart.

In addition, we need to put the same name into the /etc/hostname file. Otherwise, the discrepancies might lead to the system malfunction.

Finally, if we use the static hostname for networking in the /etc/hosts file, we need to update it accordingly as well.

4. Drawbacks of /etc/hostname

We should notice that this way of managing the hostname fails when the network configuration comes into play.

For instance, services providing DHCP may change the content of the /etc/hostname file or remove it altogether.

In such a case, hostname returns the name obtained from the service.

5. The hostnamectl Command

We should use hostnamectl for both instant and persistent changes of hostname. Additionally, we may overwrite the name obtained from the network configuration.

The command comes with the systemd package and is backed by an on-demand service.

So let’s check the hostname:

$ hostnamectl hostname


Now, let’s change the name with the set-hostname command to hostnamectl:

$ hostnamectl set-hostname fedora

Finally, let’s check it again:

$ hostnamectl hostname

$ cat /etc/hostname

$ hostname

6. More Hostnames With hostnamectl

With this command, we have more types of hostnames at our disposal. We can use additional switches to the set-hostname command:

  • static – refers to the hostname which resides in the /etc/hostname file
  • pretty – a user-friendly hostname, which may include special or locale characters
  • transient – alternative to the hostname obtained from network configuration when the static one is not available

So let’s try these hostnames:

$ hostnamectl set-hostname --static fedora35

$ hostnamectl set-hostname --pretty "Joe's fedora"

$ hostnamectl set-hostname --transient "fedora"
Hint: static hostname is already set, so the specified transient hostname will not be used.

Let’s notice the remark on setting the transient one. Now, let’s check the result using the same switches as before:

$ hostnamectl --static; hostnamectl --pretty; hostnamectl --transient
Joe's fedora

Finally, let’s notice that the set-hostname command, without any modifier, changes all three hostnames.

7. Conclusion

In this article, we learned to manage the system’s hostname.

First, we found the place where the hostname is stored. Then we edited this file to change the hostname. In addition, we retrieved and set the name with the hostname command. Finally, we examined the hostnamectl command, which manages the hostname in the systemd Linux distribution.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.