Skip to content

hostname Command: Display or Set System Hostname

Summary

The hostname command is a fundamental utility used to display or set the hostname of a Linux system. The hostname is a name given to a computer on a network, used to identify it in various network communications.

Introduction

The hostname command is a simple yet crucial tool for system administrators and users alike. It allows you to quickly determine the current hostname or change it (with sufficient privileges). The hostname is utilized by many network services and applications to identify the machine. Proper configuration of the hostname is essential for network functionality and administration.

Use case and Examples

Display the current hostname

hostname
This command, without any arguments, simply prints the current hostname to the terminal.

Set the hostname (requires root privileges)

sudo hostname newhostname
This command sets the hostname to "newhostname". Note that this change is typically only temporary (until the next reboot). You'll need to modify the /etc/hostname file and network configuration files (like /etc/hosts) for a persistent change.

Set the hostname and FQDN (Fully Qualified Domain Name)

sudo hostname -F /etc/hostname
This command reads the new hostname from the file /etc/hostname and sets both the hostname and potentially the FQDN, depending on your network configuration and the contents of /etc/hostname.

Display the FQDN (Fully Qualified Domain Name)

hostname -f
This command displays the Fully Qualified Domain Name (FQDN) of the system. This includes the hostname and the domain name.

Display the short hostname (first part of FQDN)

hostname -s
This command displays the short hostname, which is the first part of the FQDN, before the first dot.

Display the domain name

hostname -d
This command displays the domain name of the system. It will only display the domain if the FQDN is properly configured.

Display the IP address(es) associated with the hostname

hostname -i
This command attempts to resolve the hostname to IP address(es) and displays the IP addresses. It's useful for checking if the hostname is correctly resolving to a network address.

Commonly used flags

Flag Description Example
-f Displays the Fully Qualified Domain Name (FQDN). hostname -f
-s Displays the short hostname (the part before the first dot in the FQDN). hostname -s
-d Displays the domain name. hostname -d
-i Displays the IP address(es) associated with the hostname. hostname -i
-A Displays all network addresses of the host. hostname -A
-F <file> Sets the hostname by reading it from the specified file. sudo hostname -F /etc/hostname


Share on Share on

Comments