Cheatsheet & Examples: hostname
The hostname command is a utility used to display or set the system's hostname. The hostname is a name assigned to a computer on a network, used to identify it.
Displaying the Current Hostname
Example Usage:
hostname
What it does: Displays the current hostname of the system.
Command-line Arguments Explained:
- (None)
Setting the Hostname (Permanently)
Example Usage:
sudo hostnamectl set-hostname newhostname.example.com
What it does: Sets the system's hostname to the specified value. This change is often persistent across reboots, depending on the system configuration and init system (systemd, sysvinit, etc.)
Command-line Arguments Explained:
sudo: Executes the command with elevated privileges, necessary to modify system settings.hostnamectl: A command used to control the system hostname.set-hostname: The option of thehostnamectlcommand that modifies the hostname.newhostname.example.com: The new hostname to be set. This example uses a fully qualified domain name (FQDN).Setting the Hostname (Temporarily)
Example Usage:
sudo hostname newhostname
What it does: Sets the system's hostname to the specified value. This change is typically only effective until the next system reboot or network configuration change.
Command-line Arguments Explained:
sudo: Executes the command with elevated privileges, necessary to modify system settings.hostname: The command used to manipulate the hostname.newhostname: The new hostname to be set (e.g., a simple hostname or a short form of an FQDN).Viewing Related Hostname Information (using hostnamectl)
Example Usage:
hostnamectl
What it does: Displays information about the system's hostname, including the static, transient, and pretty hostnames. This provides a comprehensive view of how the hostname is configured.
Command-line Arguments Explained:
hostnamectl: The command to display hostname-related information usingsystemd.- (No arguments) When no arguments are given,
hostnamectldisplays the system's current hostname configuration.Getting the FQDN (Fully Qualified Domain Name)
Example Usage:
hostname -f
What it does: Displays the fully qualified domain name (FQDN) of the system, if one is configured.
Command-line Arguments Explained:
-f: Option for thehostnamecommand that specifies the retrieval of the FQDN.Getting the Short Hostname
Example Usage:
hostname -s
What it does: Displays the short hostname (the part of the hostname before the first dot).
Command-line Arguments Explained:
-s: Option for thehostnamecommand to display the short hostname.Checking the Kernel's Hostname
Example Usage:
hostname -k
What it does: Displays the kernel's hostname. This is usually the same as the current hostname but can differ in certain situations.
Command-line Arguments Explained:
-k: Option for thehostnamecommand to display the kernel's hostname.Displaying Hostname Information (Alternative Format)
Example Usage:
hostname -i
What it does: Displays the IP address(es) associated with the hostname. This is useful for quickly determining a server's IP.
Command-line Arguments Explained:
-i: Option forhostnamecommand to display the IP addresses associated with the hostname.

