Networking and remote
hostname
Print the system's host name, its short or fully qualified form, or its IP addresses.
hostname [-s | -f | -d | -I | -i] [NAME]
hostname prints the name the kernel knows this machine by, the name that appears in your
shell prompt, in logs and in ssh sessions. Options derive related values: -s the short
name (up to the first dot), -f the fully qualified domain name, -I every configured IP
address.
-f, -d and -i work by looking the name up through the resolver (/etc/hosts, then DNS),
so on a machine whose own name is not resolvable they fail even though plain hostname
works. That is the case in the sandbox the examples run in, whose name is demo.
Setting the name with hostname NAME needs root and lasts only until reboot. On systemd
systems, hostnamectl set-hostname NAME changes it permanently (it writes /etc/hostname).
Examples
Print the host name
hostname
demo
The kernel's host name. Scripts use it to tag log lines or pick per-host settings.
The short name
hostname -s
demo
-s cuts the name at the first dot, so web01.example.com would print web01. This name
has no dots, so it is unchanged.
Other places the name shows up
uname -n
echo "$HOSTNAME"
cat /etc/hostname
demo demo demo
uname -n reads the same kernel value; bash copies it into $HOSTNAME at startup;
/etc/hostname is the file the system reads at boot to set it.
All IP addresses of the machine
hostname -I
(no output)
-I lists every address on every interface except loopback, separated by spaces. The
sandbox has only loopback, so the line is empty; on a server you would see something like
10.0.0.11 fd00::11. For details per interface, use ip -br addr.
The fully qualified name needs name resolution
hostname -f
hostname: Temporary failure in name resolution
-f looks the host name up to find its full domain name. demo is not in /etc/hosts
and there is no DNS here, so it fails. On a correctly configured server it prints e.g.
web01.example.com.
Changing the name needs root
hostname web01
hostname: you must be root to change the host name
An ordinary user may not rename the machine. Even as root the change is temporary; use
hostnamectl set-hostname (or edit /etc/hostname) for a permanent one.
Set the host name permanently with systemd
sudo hostnamectl set-hostname web01
Not run in the sandbox: needs systemd and root, which the sandbox does not have. No output is shown because none was captured.
Sets the running host name and writes it to /etc/hostname in one step. Update
/etc/hosts as well, so that tools resolving the name (including sudo) do not stall.
Common pitfalls
Changing the name with
hostname NAMEis lost at reboot. Usehostnamectl set-hostnameor edit/etc/hostname.After renaming, add the new name to
/etc/hosts(for example on the127.0.1.1line on Debian); otherwisesudoand other tools that resolve it may print errors or pause.hostname -i(lowercase) resolves the name and can return127.0.1.1or fail;-I(uppercase) lists the real interface addresses.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
-Iand-iare Linux (Debian hostname) options. On macOS,hostname -sand-fexist,scutil --get ComputerNameandscutil --set HostNamemanage the names, and addresses come fromifconfig.
Options worth knowing
A selection, described in our own words. For the full list, see hostname(1) or hostname --help.
| Option | What it does |
|---|---|
-s, --short | Print the name up to the first dot. |
-f, --fqdn | Print the fully qualified domain name (needs name resolution). |
--long | Same as --fqdn. |
-d, --domain | Print the DNS domain part of the FQDN. |
-i, --ip-address | Print the addresses the host name resolves to. |
-I, --all-ip-addresses | Print all addresses of all interfaces except loopback. |
-A, --all-fqdns | Print the FQDNs of all interface addresses. |
-F, --file FILE | Set the host name from the first line of FILE (root). |
-y, --yp | Print the NIS domain name. |
Related
unamePrint the kernel name, release and version, the hostname, and the machine hardware type.ipShow and change network interfaces, addresses, routes and neighbors on Linux (iproute2).sshLog in to a remote machine or run a command on it over an encrypted connection; also forwards ports and jumps through hosts.pingSend ICMP echo requests to a host and report replies and round-trip times, to test reachability and latency.