LinuxCommandLibrary

hostname

Display or set system hostname

TLDR

Show current host name

$ hostname
copy

Show the network address of the host name
$ hostname [[-i|--ip-address]]
copy

Show the FQDN (Fully Qualified Domain Name)
$ hostname [[-f|--fqdn]]
copy

Set current host name
$ hostname [new_hostname]
copy

SYNOPSIS

hostname [OPTIONS...]
hostname [NAME]
hostname [OPTIONS...] -F

PARAMETERS

--alias, -a
    Display the alias name of the host (if any).

--all-fqdns, -A
    Display all FQDNs of the machine. This option enumerates all configured network interfaces and displays their associated FQDNs.

--domain, -d
    Display the DNS domain name.

--fqdn, --long, -f
    Display the Fully Qualified Domain Name (FQDN). This is usually `hostname.domainname`.

--ip-address, -i
    Display the IP address(es) of the hostname. Note that this works by looking up the current hostname, not directly querying network interfaces.

--all-ip-addresses, -I
    Display all network interface addresses of the host.

--short, -s
    Display the short host name (the part before the first dot).

--yp, --nis, -y
    Display the NIS/YP domain name.

--file , -F
    Read the hostname from the specified file and set it.

[NAME]
    Set the system's hostname to the specified NAME. This change is typically not persistent across reboots.

--version, -v
    Display version information and exit.

--help, -h
    Display a help message and exit.

DESCRIPTION

The `hostname` command in Linux is a fundamental utility used to display or set the system's hostname, domain name, or NIS/YP domain name. It interacts with the kernel's hostname settings. When executed without any arguments, it prints the current hostname. With a single argument, it attempts to set the system's hostname to that value. However, direct setting via `hostname NAME` is often temporary and does not persist across reboots on modern systems using `systemd`. For persistent changes, one typically modifies `/etc/hostname` or uses the `hostnamectl` command. The `hostname` command is crucial for network identification, as the hostname is used by various network services to identify the machine. It can also provide different formats of the hostname, such as the Fully Qualified Domain Name (FQDN) or IP addresses, depending on the options provided.

CAVEATS

Setting the hostname using `hostname NAME` is usually not persistent across reboots on modern Linux distributions (especially those using `systemd`). For persistence, modify `/etc/hostname` or use `hostnamectl`.
The hostname lookup for options like `-f` or `-i` relies on local DNS/hosts file configuration (e.g., `/etc/hosts` and `/etc/resolv.conf`). If these are misconfigured, the results might be inaccurate.
Changing the hostname can affect running services that rely on the old hostname for identification.

<B>PERSISTENT HOSTNAME CONFIGURATION</B>

On `systemd`-based systems, the static hostname is typically stored in the `/etc/hostname` file. Directly editing this file and then rebooting (or using `hostnamectl set-hostname`) is the recommended way to make hostname changes permanent.

<B>HOSTNAME AND /ETC/HOSTS</B>

The system's hostname should usually have a corresponding entry in the `/etc/hosts` file, typically mapping `127.0.0.1` to `localhost` and the system's hostname, to ensure proper local name resolution. Example: `127.0.0.1 localhost.localdomain localhost`.

HISTORY

The `hostname` command has been a standard Unix utility for many years, dating back to early Unix systems. Its primary function has remained consistent: to query and set the system's identification name. In the context of modern Linux distributions, particularly with the advent of `systemd`, the role of `hostname` for setting the hostname has somewhat shifted. While it still allows temporary changes, `systemd` introduced `hostnamectl` as the preferred and persistent method for managing the system's hostname, static hostname, pretty hostname, and transient hostname, often interacting with the `systemd-hostnamed` service. Despite this, `hostname` remains widely used for quickly displaying the current hostname.

SEE ALSO

hostnamectl(1), sysctl(8), network(7), hosts(5), dnsdomainname(1), ypdomainname(1)

Copied to clipboard