hostnamectl – Query or Change the System Hostname

The hostnamectl command is used to query or change the system hostname on a Linux system. The hostname is the name given to a computer or device on a network, and it is used to identify and communicate with other devices on the same network. The hostnamectl command is a part of the systemd system and service manager, which is the default init system used by most modern Linux distributions.

Overview

The hostnamectl command can be used to perform several operations related to the system hostname, including the following:

Display the current hostname

To display the current hostname of the system, run the following command:

hostnamectl

This will display output similar to the following:

Static hostname: mycomputer
Icon name: computer-desktop
Chassis: desktop
Machine ID: 2f4d8f3b8f314d5c9c4e7b3b4f6b5e27
Boot ID: 2c4d8f3b8f314d5c9c4e7b3b4f6b5e27
Operating System: Ubuntu 20.04.2 LTS
Kernel: Linux 5.4.0-80-generic
Architecture: x86-64

The Static hostname field displays the current hostname of the system.

Change the hostname

To change the hostname of the system, run the following command:

sudo hostnamectl set-hostname newhostname

Replace newhostname with the desired hostname. This will set the hostname to the new value immediately. However, this change will not persist after a reboot.

Set the hostname persistently

To set the hostname persistently, run the following command:

sudo hostnamectl set-hostname newhostname --pretty

Replace newhostname with the desired hostname. The --pretty option sets the pretty hostname, which is a more user-friendly version of the hostname that can contain spaces and special characters. This will update the /etc/hostname file with the new hostname, and the change will persist after a reboot.

Set the hostname for a remote system

To set the hostname for a remote Linux system, run the following command:

ssh user@remotehost 'sudo hostnamectl set-hostname newhostname'

Replace user with the username of the remote system, remotehost with the IP address or hostname of the remote system, and newhostname with the desired hostname. This will set the hostname of the remote system to the new value.

Options

The hostnamectl command supports the following options:

Option Description
--static Display or set the static hostname
--pretty Display or set the pretty hostname
--transient Display or set the transient hostname
--help Display help information
--version Display version information

Troubleshooting tips

If you encounter issues when using the hostnamectl command, try the following troubleshooting tips:

  • Make sure you have the necessary permissions to run the command. The hostnamectl command requires root privileges to change the hostname.
  • Check that the new hostname is a valid hostname. A hostname can contain letters, numbers, and hyphens, but it cannot start or end with a hyphen.
  • Check that the new hostname does not conflict with other hostnames on the network. Each hostname on a network must be unique.

Notes

  • The hostnamectl command is only available on systems that use the systemd system and service manager.
  • The hostnamectl command is the recommended way to set the hostname on a Linux system, as it is a standardized method that works across different distributions.