Linux hostnamectl Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux hostnamectl command, which is used to display and manage the system hostname. The lab covers the introduction to the hostnamectl command, how to display various system hostname information, and how to change the system hostname temporarily and permanently. The hostnamectl command is part of the systemd suite of utilities and provides a convenient way to interact with the system's hostname settings.

The lab includes step-by-step instructions and practical examples to help you understand the usage of the hostnamectl command and its different options. By the end of this lab, you will be able to effectively manage the system hostname using the hostnamectl command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/uname("`System Information Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/hostname("`Hostname Managing`") subgraph Lab Skills linux/cat -.-> lab-422724{{"`Linux hostnamectl Command with Practical Examples`"}} linux/grep -.-> lab-422724{{"`Linux hostnamectl Command with Practical Examples`"}} linux/sudo -.-> lab-422724{{"`Linux hostnamectl Command with Practical Examples`"}} linux/uname -.-> lab-422724{{"`Linux hostnamectl Command with Practical Examples`"}} linux/hostname -.-> lab-422724{{"`Linux hostnamectl Command with Practical Examples`"}} end

Introduction to hostnamectl Command

In this step, we will explore the hostnamectl command, which is a Linux command-line tool used to display and manage the system hostname. The hostnamectl command is part of the systemd suite of utilities, and it provides a convenient way to interact with the system's hostname settings.

Let's start by running the hostnamectl command without any arguments to see the current system hostname information:

hostnamectl

Example output:

   Static hostname: ubuntu
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 9a5c7f4a4f2f4d9c9d9a5c7f4a4f2f4
           Boot ID: 9a5c7f4a4f2f4d9c9d9a5c7f4a4f2f4
    Virtualization: docker
  Operating System: Ubuntu 22.04.1 LTS
            Kernel: Linux 5.15.0-52-generic
      Architecture: x86-64

The hostnamectl command provides various options to display different aspects of the system hostname, such as the static hostname, icon name, chassis type, machine ID, boot ID, virtualization type, and operating system information.

In the next steps, we will learn how to change the system hostname temporarily and permanently using the hostnamectl command.

Displaying System Hostname Information

In this step, we will learn how to display various system hostname information using the hostnamectl command.

First, let's display the static hostname of the system:

hostnamectl status | grep "Static hostname"

Example output:

Static hostname: ubuntu

The static hostname is the default hostname assigned to the system, which persists across reboots.

Next, let's display the transient hostname, which is the current hostname of the system:

hostnamectl status | grep "Transient hostname"

Example output:

Transient hostname: ubuntu

In most cases, the static and transient hostnames will be the same, but the transient hostname can be changed temporarily without affecting the static hostname.

You can also display additional system information using the hostnamectl command:

hostnamectl status

This will show you the complete system information, including the icon name, chassis type, machine ID, boot ID, virtualization type, operating system, kernel, and architecture.

Changing System Hostname Temporarily and Permanently

In this step, we will learn how to change the system hostname both temporarily and permanently using the hostnamectl command.

To change the hostname temporarily, use the following command:

sudo hostnamectl set-hostname new-hostname

Replace new-hostname with the desired hostname. This will change the transient hostname immediately, but the change will not persist after a reboot.

To verify the temporary hostname change:

hostnamectl status | grep "Transient hostname"

Example output:

Transient hostname: new-hostname

To change the hostname permanently, we need to update both the static and transient hostnames:

sudo hostnamectl set-hostname permanent-hostname

This will update the static hostname, which will be used as the default hostname after a reboot.

To verify the permanent hostname change:

hostnamectl status | grep "Static hostname"
hostnamectl status | grep "Transient hostname"

Example output:

Static hostname: permanent-hostname
Transient hostname: permanent-hostname

Now, the system hostname has been changed both temporarily and permanently.

Summary

In this lab, we explored the hostnamectl command, a Linux tool used to display and manage the system hostname. We learned how to view the static hostname, transient hostname, and other system information provided by hostnamectl. We also discovered how to temporarily and permanently change the system hostname using this command. The key takeaways from this lab are the ability to efficiently retrieve and modify the hostname settings on a Linux system through the hostnamectl utility.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like