How to Use the Linux hostnamectl Command to Set the System Hostname

The Identity of Your Server

Every device connected to a network needs an identifier. While IP addresses provide the mathematical routing information necessary for computers to talk to each other, humans prefer names. The “hostname” is the human-readable label assigned to a device on a network.

When you install a fresh Linux distribution, it usually prompts you to choose a hostname (e.g., ubuntu-desktop or webserver-01). You will see this name every time you open the terminal, usually formatted as username@hostname:~$.

However, what happens when you clone a virtual machine to create a second web server? Or when you repurpose a database server for a different task? You need to change the hostname so the machine can be uniquely identified on the corporate network.

In modern Linux distributions that use systemd (which includes Ubuntu, Debian, CentOS, RHEL, and Fedora), the most robust way to view and change this identity is using the hostnamectl command.

Step 1: Viewing the Current System Identity

To see detailed information about your current hostname and operating system, simply type the command by itself:

hostnamectl

The terminal will output a block of system information:

 Static hostname: webserver-old
       Icon name: computer-vm
         Chassis: vm
      Machine ID: a1b2c3d4e5f6...
         Boot ID: f1e2d3c4b5a6...
  Virtualization: kvm
Operating System: Ubuntu 22.04 LTS
          Kernel: Linux 5.15.0-generic
    Architecture: x86-64

This output is incredibly useful for instantly checking exactly which version of the Linux kernel and OS you are running without having to dig through /etc/os-release files.

Step 2: Changing the Static Hostname

The “Static hostname” is the traditional, permanent name of the machine stored in the /etc/hostname file. It is the name used by the system at boot.

Let’s say you want to change this machine’s name from webserver-old to database-primary.

Because you are modifying a core system configuration, you must use sudo to execute the command as the root user. The syntax is set-hostname followed by the new name.

sudo hostnamectl set-hostname database-primary

Unlike older methods of changing the hostname (which required manually editing text files with nano or vi and then rebooting the server), hostnamectl applies the change instantly across the system.

Note on terminal prompts: Even though the hostname has been changed instantly in the background, your current open terminal window might still show username@webserver-old:~$. To see the new name in your prompt, simply close the terminal window and open a new one, or type bash to start a fresh shell session.

Step 3: Understanding Hostname Restrictions

When setting a static hostname, you must follow strict internet standards (RFC 1123).

  • The name must consist only of lowercase letters (a-z), numbers (0-9), and hyphens (-).
  • It cannot contain spaces, underscores, or special characters (like @ or !).
  • It cannot begin or end with a hyphen.
  • It is usually restricted to a maximum of 64 characters.

If you attempt to run sudo hostnamectl set-hostname "My Cool Server!", the command will fail or automatically strip out the illegal characters.

Step 4: Setting a “Pretty” Hostname

If you absolutely need a human-readable name with spaces and capital letters (perhaps for a desktop computer that will be visible to Mac or Windows machines on a local network via mDNS/Bonjour), hostnamectl supports a secondary “Pretty” hostname.

This does not replace the strict static hostname used by core networking services; it acts as an additional descriptive label.

To set a pretty hostname, add the --pretty flag and enclose your desired name in quotes:

sudo hostnamectl set-hostname --pretty "John's Ubuntu Workstation"

If you run the basic hostnamectl command again, you will now see both the strict static hostname and the new descriptive pretty hostname listed in the output.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.