How to use the command 'hostnamectl' (with examples)

How to use the command 'hostnamectl' (with examples)

The hostnamectl command is a versatile tool used in UNIX-like operating systems to manage the hostname of a computer. A hostname is a unique label assigned to a computer within a network, and it serves various administrative functions and network communications. The command not only allows users to retrieve the current hostname but also enables them to set or modify the hostname. Additionally, it provides more detailed information about the system’s operating environment, including the kernel and architecture type. This command is particularly useful in systems administration and network management, where modifications to the hostname may be necessary due to changes in network configurations or administrative policies.

Use case 1: Get the hostname of the computer

Code:

hostnamectl

Motivation:

There are various circumstances where knowing the hostname of a machine is essential. For instance, if you’re managing multiple servers, each with different roles or located in separate networks, identifying them by their respective hostnames can streamline administrative tasks. Whether you are troubleshooting, configuring a new server, or performing routine audits, quickly retrieving the hostname using hostnamectl can enhance these processes’ efficiency and accuracy.

Explanation:

Running hostnamectl without any additional arguments will display the current settings of the host, including its hostname. This command retrieves information such as:

  • Static hostname: The traditional network name of the machine.
  • Pretty hostname: A user-friendly version of the hostname that may include spaces and special characters.
  • Transient hostname: The temporary name the computer uses while it’s running.

Example output:

   Static hostname: my-computer
         Icon name: computer-desktop
           Chassis: desktop
        Machine ID: abcd1234efgh5678ijkl9012mnop3456
           Boot ID: 1234abcd5678efgh9012ijkl3456mnop
  Operating System: Ubuntu 20.04.3 LTS
            Kernel: Linux 5.4.0-81-generic
      Architecture: x86-64

Use case 2: Set the hostname of the computer

Code:

sudo hostnamectl set-hostname "new-hostname"

Motivation:

In some cases, you need to reconfigure a machine’s network identity. This necessity might arise when redeploying a machine within a different network segment, repurposing the machine for a new function, or simply complying with new naming standards your organization implements. Setting a new hostname ensures that the machine is easily identifiable and manageable within the network, preventing conflicts and maintaining clear communication paths.

Explanation:

  • sudo: Executes the command as a superuser, which is required for modifying system configurations such as hostnames.
  • hostnamectl: Invokes the command responsible for handling hostname configurations.
  • set-hostname: Specifies that you intend to change the existing hostname.
  • "new-hostname": The desired new name for your computer. Ensure it adheres to your network’s naming conventions and is unique within your environment.

Example output:

After running this command, there will be no direct output, but you can verify the change by rerunning hostnamectl to see the updated hostname reflected:

Static hostname: new-hostname

Use case 3: Set a pretty hostname for the computer

Code:

sudo hostnamectl set-hostname --static "hostname.example.com" && sudo hostnamectl set-hostname --pretty "Pretty Name"

Motivation:

Assigning a pretty hostname can enhance system administration by allowing for more human-readable identifiers compared to static hostnames. This is especially beneficial in environments involving interaction with non-technical stakeholders or when maintaining visual dashboards where this human-friendly identifier is displayed. It allows for simplicity without affecting technical operations.

Explanation:

  • sudo: Authorizes the execution of these commands with elevated privileges.
  • hostnamectl: The command facilitating hostname management.
  • set-hostname: Initiates a change to the hostname configuration.
  • --static: Switch specifying that the following hostname is to be considered the static identifier, used by the system for routine networking tasks.
  • "hostname.example.com": The static references, typically fully-qualified domain names, aligning with DNS configurations.
  • &&: A shell logical operator chaining two commands, ensuring the second part is executed only if the first part succeeds.
  • --pretty: Specifies that the intended use of the following hostname string is for non-technical, display, or documentation purposes.
  • "Pretty Name": The descriptive, easily recognizable identifier for the system, free from regular hostname constraints like character limitations.

Example output:

There won’t be any immediate command-line output after execution, but querying with hostnamectl will reveal both static and pretty hostnames set as specified:

Static hostname: hostname.example.com
Pretty hostname: Pretty Name

Use case 4: Reset hostname to its default value

Code:

sudo hostnamectl set-hostname --pretty ""

Motivation:

There may be scenarios where it is necessary to revert the pretty hostname to its default (unset) value. This could be required if the administrative policy changes to exclude non-technical descriptors or after completing temporary configurations for special initiatives. By resetting, you maintain consistency with the current naming policies or revert to a more technical identifier suitable for automated processes.

Explanation:

  • sudo: Executes the following command with the required administrative permissions.
  • Hostnamectl: The command called to effect hostname changes.
  • set-hostname: Initiates command parameters to change the computer’s name configuration.
  • --pretty: Suggests the action is targeted towards altering the presentation-aligned hostname.
  • "": An empty string argument that indicates resetting or removal of the pretty hostname, falling back to the default state.

Example output:

Upon executing, no visual confirmation is returned immediately, but invoking hostnamectl later will reflect the absence of a pretty hostname, thereby reverting to its implicit system default setting:

Pretty hostname: (none)

Conclusion:

The hostnamectl command is an essential tool for managing computer identity within a networked environment. Its ability to quickly display or modify hostnames—with varying levels of formality and presentation—makes it invaluable in distinct administrative tasks. By understanding these use cases, you can effectively utilize hostnamectl to ensure your system remains properly identified and adaptable to evolving network standards and organizational needs.

Related Posts

How to use the command 'git sizer' (with examples)

How to use the command 'git sizer' (with examples)

The git sizer tool is an invaluable utility for developers working with Git repositories.

Read More
How to Use the Command 'devenv' (with Examples)

How to Use the Command 'devenv' (with Examples)

Devenv is a powerful tool designed to enhance the development process by providing fast, declarative, reproducible, and composable developer environments using Nix.

Read More
How to Use the Command 'xcodes' (with Examples)

How to Use the Command 'xcodes' (with Examples)

The ‘xcodes’ command-line tool is an invaluable resource for macOS developers, especially those who need to manage multiple versions of Xcode efficiently.

Read More