Skip to main content

Command Palette

Search for a command to run...

Cheatsheet & Examples: hostname

Updated
3 min read
H

I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)

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 the hostnamectl command 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).

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 using systemd.
  • (No arguments) When no arguments are given, hostnamectl displays 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 the hostname command 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 the hostname command 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 the hostname command 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 for hostname command to display the IP addresses associated with the hostname.

More from this blog

Hong's Tech Blog

110 posts

The blog is older than you know. I prefer counting from the emergence of one integral anomaly to the emergence of the next, in which case this is the forth version.