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

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

The ‘hostnamectl’ command is used to get or set the hostname of the computer. The hostname is a unique name assigned to a computer in a network, which helps in identifying and accessing it.

Use case 1: Get the hostname of the computer

Code:

hostnamectl

Motivation: Getting the hostname of the computer is useful when you need to know the unique name assigned to the system. This can be helpful for various administrative tasks or when troubleshooting network connectivity issues.

Explanation: This command simply retrieves the current hostname of the computer.

Example output:

   Static hostname: my-computer
   Icon name: computer-laptop
   Chassis: laptop
   Machine ID: 1234567890abcdef
   Boot ID: 0987654321fedcba
   Operating System: Ubuntu 20.04.1 LTS
   Kernel: Linux 5.4.0-47-generic
   Architecture: x86-64

Use case 2: Set the hostname of the computer

Code:

sudo hostnamectl set-hostname "hostname"

Motivation: Setting the hostname of the computer can be necessary when you want to change the unique name assigned to the system. This is often done when configuring a new network or when making changes to the network topology.

Explanation: This command allows you to set a new hostname for the computer. The “hostname” argument should be replaced with the desired hostname.

Example output: No output is displayed if the command is successful.

Use case 3: Set a pretty hostname for the computer

Code:

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

Motivation: Setting a pretty hostname for the computer can be useful for display purposes, especially when working with graphical user interfaces or when accessing the system remotely.

Explanation: This command sets both the static hostname and the pretty hostname of the computer. The “hostname.example.com” argument should be replaced with the desired static hostname, and the “hostname” argument should be replaced with the desired pretty hostname.

Example output: No output is displayed if the command is successful.

Use case 4: Reset hostname to its default value

Code:

sudo hostnamectl set-hostname --pretty ""

Motivation: Resetting the hostname to its default value may be necessary when you want to revert to the original, pre-configured hostname.

Explanation: This command resets the pretty hostname of the computer to an empty value, effectively restoring the default hostname.

Example output: No output is displayed if the command is successful.

Conclusion:

The ‘hostnamectl’ command provides a simple way to get or set the hostname of the computer. Whether you need to retrieve the current hostname or make changes to it, this command offers convenient options for managing the system’s unique name.

Related Posts

How to use the command lpstat (with examples)

How to use the command lpstat (with examples)

The lpstat command is a tool used in Unix and Unix-like operating systems to display status information about the current classes, jobs, and printers.

Read More
How to use the command `diff` (with examples)

How to use the command `diff` (with examples)

The diff command in Linux is used to compare files and directories.

Read More
How to use the command `systemd-sysext` (with examples)

How to use the command `systemd-sysext` (with examples)

The systemd-sysext command is used to activate or deactivate system extension images.

Read More