Linux host Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to manage the hostname of a Linux system using the hostname and hostnamectl commands. The lab covers exploring the basic usage of the hostname command, as well as the more comprehensive hostnamectl command for managing hostname settings. You will learn how to display the current hostname, as well as various options to show different aspects of the hostname information. Additionally, you will learn how to change the hostname of the system using the hostnamectl command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/RemoteAccessandNetworkingGroup -.-> linux/ifconfig("`Network Configuring`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/uname("`System Information Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/hostname("`Hostname Managing`") linux/UserandGroupManagementGroup -.-> linux/set("`Shell Setting`") subgraph Lab Skills linux/sudo -.-> lab-422721{{"`Linux host Command with Practical Examples`"}} linux/ifconfig -.-> lab-422721{{"`Linux host Command with Practical Examples`"}} linux/vim -.-> lab-422721{{"`Linux host Command with Practical Examples`"}} linux/nano -.-> lab-422721{{"`Linux host Command with Practical Examples`"}} linux/uname -.-> lab-422721{{"`Linux host Command with Practical Examples`"}} linux/hostname -.-> lab-422721{{"`Linux host Command with Practical Examples`"}} linux/set -.-> lab-422721{{"`Linux host Command with Practical Examples`"}} end

Explore the hostname Command

In this step, we will explore the hostname command, which is used to display or set the hostname of the system.

First, let's check the current hostname of the system:

hostname

Example output:

ubuntu

The hostname command without any arguments simply prints the current hostname of the system.

Next, let's use the hostname command to display more detailed information about the system:

hostname -a
hostname -d
hostname -f
hostname -i
hostname -s

Example output:

ubuntu
example.com
ubuntu.example.com
172.17.0.2
ubuntu
  • hostname -a: Displays the alias hostname.
  • hostname -d: Displays the DNS domain name.
  • hostname -f: Displays the fully qualified domain name (FQDN).
  • hostname -i: Displays the network addresses of the host.
  • hostname -s: Displays the short host name.

As you can see, the hostname command provides various options to display different aspects of the system's hostname.

Manage Hostnames with the hostnamectl Command

In this step, we will explore the hostnamectl command, which provides a more comprehensive way to manage the hostname of the system.

First, let's check the current hostname settings using hostnamectl:

hostnamectl

Example output:

   Static hostname: ubuntu
         Icon name: computer-vm
       Machine ID: 7b6d7b3f1d9d4c5a8d1a2b3c4d5e6f7
            Boot ID: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5
   Virtualization: docker
 Operating System: Ubuntu 22.04 LTS
          Kernel: Linux 5.15.0-1023-aws
    Architecture: x86-64

The hostnamectl command displays various information about the system, including the static hostname, icon name, machine ID, boot ID, virtualization type, operating system, kernel version, and architecture.

Next, let's change the hostname using hostnamectl:

sudo hostnamectl set-hostname new-hostname

After running this command, the hostname of the system will be updated to "new-hostname".

To verify the new hostname, run:

hostnamectl

Example output:

   Static hostname: new-hostname
         Icon name: computer-vm
       Machine ID: 7b6d7b3f1d9d4c5a8d1a2b3c4d5e6f7
            Boot ID: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5
   Virtualization: docker
 Operating System: Ubuntu 22.04 LTS
          Kernel: Linux 5.15.0-1023-aws
    Architecture: x86-64

As you can see, the static hostname has been updated to "new-hostname".

Customize the Hostname on Ubuntu 22.04

In this final step, we will learn how to manually customize the hostname on an Ubuntu 22.04 system.

First, let's check the current hostname again:

hostnamectl

Example output:

   Static hostname: new-hostname
         Icon name: computer-vm
       Machine ID: 7b6d7b3f1d9d4c5a8d1a2b3c4d5e6f7
            Boot ID: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5
   Virtualization: docker
 Operating System: Ubuntu 22.04 LTS
          Kernel: Linux 5.15.0-1023-aws
    Architecture: x86-64

To manually change the hostname, we need to edit the /etc/hostname file:

sudo nano /etc/hostname

In the file, replace the current hostname with the desired new hostname, for example, "my-custom-hostname":

my-custom-hostname

Save the file and exit the text editor.

Next, we need to update the /etc/hosts file to reflect the new hostname:

sudo nano /etc/hosts

Find the line that starts with "127.0.0.1" and replace the hostname with the new one:

127.0.0.1 my-custom-hostname localhost

Save the file and exit the text editor.

Finally, let's verify the new hostname:

hostnamectl

Example output:

   Static hostname: my-custom-hostname
         Icon name: computer-vm
       Machine ID: 7b6d7b3f1d9d4c5a8d1a2b3c4d5e6f7
            Boot ID: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5
   Virtualization: docker
 Operating System: Ubuntu 22.04 LTS
          Kernel: Linux 5.15.0-1023-aws
    Architecture: x86-64

The hostname has been successfully changed to "my-custom-hostname".

Summary

In this lab, we explored the hostname command, which is used to display or set the hostname of the system. We learned how to use various options with the hostname command to display different aspects of the system's hostname, such as the alias, DNS domain name, fully qualified domain name, network addresses, and short host name. We then explored the hostnamectl command, which provides a more comprehensive way to manage the hostname of the system. We learned how to use hostnamectl to display detailed information about the system, including the static hostname, icon name, machine ID, boot ID, virtualization type, operating system, kernel version, and architecture. Finally, we demonstrated how to change the hostname using the hostnamectl set-hostname command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like