hostname Command in Linux

Introduction to Linux Hostname Management

This tutorial dives into the Linux hostname command, a crucial tool for any systemadmin. We'll explore how to use it to display and configure your system's hostname. First, we'll cover the basic syntax and how to retrieve essential information about your system's hostname and network configuration. Then, you'll learn how to modify the hostname, both temporarily and permanently, which is a valuable skill for system administration, network troubleshooting, and server configuration. This guide provides practical examples and clear instructions to help you effectively manage hostnames on your Linux systems.

Understanding the hostname Command

In this section, we will delve into the intricacies of the hostname command within the Linux environment. This command is your primary interface for viewing or setting the system's hostname.

Let's begin by checking the current hostname:

hostname

Example output:

ubuntu

Invoking the hostname command without any arguments simply displays the system's current hostname.

The hostname command also allows you to retrieve more detailed system information by using different options:

hostname -f
hostname -i
hostname -I

Example output:

ubuntu.localdomain
172.17.0.2
172.17.0.2
  • hostname -f provides the fully qualified domain name (FQDN) of the system. This is useful when you need the complete domain-aware name.
  • hostname -i displays the primary IP address of the system.
  • hostname -I displays all IP addresses associated with the system. This is helpful when your system has multiple network interfaces.

These options empower you to obtain comprehensive information about your system's hostname and network setup.

Temporarily Changing the Hostname

This section explains the process of temporarily changing the hostname of your Linux system.

To temporarily modify the hostname, use the hostname command in conjunction with the desired new hostname as an argument. Remember to use sudo, as this requires elevated privileges:

sudo hostname new-hostname

Example output:

ubuntu

Executing this command changes the system's hostname to new-hostname. This change will only be in effect until the next system reboot. Upon reboot, the hostname will revert to its original configuration.

To confirm the temporary change, run:

hostname

Example output:

new-hostname

As demonstrated, the hostname has been successfully updated to new-hostname. This is a non-persistent change.

Permanently Changing the Hostname

This section outlines the steps to permanently alter the hostname of your Linux system.

To make a permanent change to the hostname, you must update the relevant configuration files. On Ubuntu 22.04 and similar systems, the hostname is typically stored in the /etc/hostname file.

Begin by editing the /etc/hostname file using your preferred text editor. We'll use nano in this example:

sudo nano /etc/hostname

Replace the existing hostname with the new hostname, such as new-hostname, and save the file.

Next, you need to modify the /etc/hosts file to reflect the new hostname. This file maps hostnames to IP addresses:

sudo nano /etc/hosts

Locate the line that begins with 127.0.0.1 and replace the existing hostname with the new one. This ensures that the system correctly resolves the new hostname to the loopback address.

Finally, a system reboot is necessary for the changes to be applied:

sudo reboot

After the system completes its reboot process, the new hostname will be permanently configured.

To verify the permanent change, execute the following command:

hostname

Example output:

new-hostname

As confirmed, the hostname has been permanently changed to new-hostname. This change will persist across system reboots.

Summary of Linux Hostname Management

In this tutorial, we explored the functionality of the hostname command in Linux and demonstrated how to effectively manage your system's hostname. We covered the fundamental usage of the hostname command to display the current hostname, the fully qualified domain name (FQDN), and the IP addresses associated with the system. We then learned how to temporarily change the hostname using the hostname command and how to make permanent changes by modifying the /etc/hostname and /etc/hosts files. These steps provide a thorough understanding of hostname management within a Linux environment, empowering you as a systemadmin.

400+ Linux Commands