LinuxCommandLibrary

hostnamectl

Control or change the system's hostname

TLDR

Get the hostname of the computer

$ hostnamectl
copy

Set the hostname of the computer
$ sudo hostnamectl set-hostname "[hostname]"
copy

Set a pretty hostname for the computer
$ sudo hostnamectl set-hostname --static "[hostname.example.com]" && sudo hostnamectl set-hostname --pretty "[hostname]"
copy

Reset hostname to its default value
$ sudo hostnamectl set-hostname --pretty ""
copy

SYNOPSIS

hostnamectl [OPTIONS...] {COMMAND}
hostnamectl [OPTIONS...] {status}
hostnamectl [OPTIONS...] {set-hostname NAME}
hostnamectl [OPTIONS...] {set-pretty-hostname NAME}
hostnamectl [OPTIONS...] {set-chassis TYPE}
hostnamectl [OPTIONS...] {set-deployment ENVIRONMENT}
hostnamectl [OPTIONS...] {set-location LOCATION}

PARAMETERS

status
    Displays the current system hostname and other related machine information.

set-hostname NAME
    Sets the system's static hostname. By default, it also attempts to update the transient and pretty hostnames to match. Requires root privileges.

set-pretty-hostname NAME
    Sets the system's pretty hostname, a free-form, human-readable name for display purposes. Requires root privileges.

set-chassis TYPE
    Sets the chassis type of the machine (e.g., 'desktop', 'laptop', 'server', 'container', 'virtual'). Requires root privileges.

set-deployment ENVIRONMENT
    Sets the deployment environment (e.g., 'development', 'staging', 'production'). Requires root privileges.

set-location LOCATION
    Sets the physical location of the machine (e.g., 'server room 1', 'desk 5B'). Requires root privileges.

--static
    When used with 'set-hostname', specifically sets only the static hostname. When used with 'status', shows only the static hostname.

--transient
    When used with 'set-hostname', specifically sets only the transient hostname. When used with 'status', shows only the transient hostname.

--pretty
    When used with 'set-hostname', specifically sets only the pretty hostname. When used with 'status', shows only the pretty hostname.

-H, --host USERNAME@HOSTNAME
    Executes the command on a specified remote host via SSH.

-M, --machine CONTAINER
    Executes the command on a specified local container.

--no-ask-password
    Do not prompt for a password when communicating with the systemd hostnamed service.

--json=MODE
    Formats output as JSON. Modes include 'short', 'pretty', 'oneline', 'raw-data'.

--no-pager
    Do not pipe output into a pager.

-h, --help
    Displays a short help text and exits.

--version
    Prints a short version string and exits.

DESCRIPTION

hostnamectl is a utility included with the systemd init system, designed to query and change the system's hostname and related machine information. It provides a centralized and consistent interface for managing three distinct types of hostnames: static, pretty, and transient. The static hostname is the traditional hostname, stored persistently in /etc/hostname, and used by default when the system boots. The pretty hostname is a free-form, UTF-8 string intended for display to users, often more descriptive than the static hostname. The transient hostname is a dynamic hostname received from network configuration (e.g., DHCP or mDNS), which, if not available, defaults to the static hostname.
This command interacts with the systemd-hostnamed.service to apply changes. Unlike the older hostname command, hostnamectl ensures that changes to the static and pretty hostnames are persistent across reboots, simplifying system administration by consolidating hostname management into a single, modern tool. It also allows setting machine chassis type, deployment environment, and physical location, further categorizing the system for management purposes.

CAVEATS

The hostnamectl command is a part of the systemd ecosystem and requires systemd to be running on the system. All commands that modify hostname settings (e.g., set-hostname, set-pretty-hostname) require root privileges or equivalent permissions. Changes to the static hostname are stored persistently in the /etc/hostname file. Changes to other machine properties (chassis, deployment, location) are managed by the systemd-hostnamed.service and stored in specific machine information files under /etc/machine-info or /run/machine-info.

UNDERSTANDING HOSTNAME TYPES

hostnamectl manages three distinct hostname types:

  • Static Hostname: The traditional hostname, stored persistently in /etc/hostname. This is the primary hostname that the system uses at boot.
  • Pretty Hostname: A free-form, human-readable UTF-8 string for display purposes, e.g., 'My Awesome Laptop'. It doesn't need to conform to hostname conventions.
  • Transient Hostname: A hostname that is dynamically managed, typically by network configuration services like DHCP or mDNS. If no transient hostname is available, it defaults to the static hostname.

PERSISTENCE OF CHANGES

Unlike the older hostname command, which only sets the kernel hostname temporarily, hostnamectl commands such as set-hostname ensure that the changes to the static and pretty hostnames are written to persistent configuration files (/etc/hostname, /etc/machine-info) and are therefore retained across system reboots. This makes hostnamectl the preferred tool for making permanent hostname changes.

HISTORY

hostnamectl was introduced as part of the systemd init system project, which began development around 2010. Its primary goal was to modernize and unify system configuration management, including hostname settings, which traditionally involved disparate tools or direct file edits. By providing a single command-line interface, hostnamectl aimed to simplify persistent hostname configuration across reboots and to manage new concepts like 'pretty' and 'transient' hostnames, reflecting a broader shift towards more dynamic and discoverable system properties within the Linux ecosystem. It became a standard utility in distributions adopting systemd.

SEE ALSO

hostname(1), systemd(1), systemd-hostnamed.service(8), systemd-firstboot(1), machine-info(5), os-release(5)

Copied to clipboard