Linux hostname Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we explore the Linux hostname command, which is used to display or set the system's host name. We first understand the basic usage of the hostname command and how to retrieve various information about the system's hostname and network configuration. Then, we learn how to change the hostname temporarily and permanently, which is useful for system administration and troubleshooting tasks. The lab provides practical examples and step-by-step instructions to help users effectively manage the hostname on their Linux systems.

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/ip("`IP Managing`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/uname("`System Information Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/hostname("`Hostname Managing`") subgraph Lab Skills linux/sudo -.-> lab-422723{{"`Linux hostname Command with Practical Examples`"}} linux/ip -.-> lab-422723{{"`Linux hostname Command with Practical Examples`"}} linux/nano -.-> lab-422723{{"`Linux hostname Command with Practical Examples`"}} linux/uname -.-> lab-422723{{"`Linux hostname Command with Practical Examples`"}} linux/hostname -.-> lab-422723{{"`Linux hostname Command with Practical Examples`"}} end

Understand the hostname Command

In this step, we will explore the hostname command in Linux, which is used to display or set the system's host name.

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

hostname

Example output:

ubuntu

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

You can also use the hostname command to get more detailed information about the system:

hostname -f
hostname -i
hostname -I

Example output:

ubuntu.localdomain
172.17.0.2
172.17.0.2
  • hostname -f displays the fully qualified domain name (FQDN) of the system.
  • hostname -i displays the IP address of the system.
  • hostname -I displays all the IP addresses of the system.

These options provide additional information about the system's hostname and network configuration.

Change the Hostname Temporarily

In this step, we will learn how to change the hostname of the system temporarily.

To change the hostname temporarily, we can use the hostname command with the new hostname as an argument:

sudo hostname new-hostname

Example output:

ubuntu

After running this command, the hostname of the system will be changed to new-hostname. However, this change will only last until the system is rebooted. The next time the system boots up, the hostname will revert back to the original value.

Let's verify the new hostname:

hostname

Example output:

new-hostname

As you can see, the hostname has been successfully changed to new-hostname.

Change the Hostname Permanently

In this step, we will learn how to change the hostname of the system permanently.

To change the hostname permanently, we need to update the configuration files that store the hostname information. In Ubuntu 22.04, the hostname is stored in the /etc/hostname file.

First, let's change the contents of the /etc/hostname file to the new hostname:

sudo nano /etc/hostname

Replace the current hostname with the new one, for example, new-hostname, and save the file.

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.

Finally, we need to reboot the system for the changes to take effect:

sudo reboot

After the system reboots, the new hostname will be permanently set.

Let's verify the new hostname:

hostname

Example output:

new-hostname

As you can see, the hostname has been changed to new-hostname and this change will persist even after rebooting the system.

Summary

In this lab, we learned how to use the hostname command in Linux to display and change the system's hostname. We first explored the basic usage of the hostname command, which can display the current hostname, the fully qualified domain name (FQDN), and the IP addresses of the system. We then learned how to change the hostname temporarily using the hostname command, and how to change the hostname permanently by modifying the /etc/hostname file. These steps provide a comprehensive understanding of managing the hostname on a Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like