opscape

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]

6 of 7 examples run in Debian 13.7 on 2026-10-31 · manual: hostname(1)

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
explain
output
demo

The kernel's host name. Scripts use it to tag log lines or pick per-host settings.

The short name

hostname -s
explain
output
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
output
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
explain
output

(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
explain
outputexit 1
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
explain
outputexit 1
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
explain

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 NAME is lost at reboot. Use hostnamectl set-hostname or edit /etc/hostname.

  • After renaming, add the new name to /etc/hosts (for example on the 127.0.1.1 line on Debian); otherwise sudo and other tools that resolve it may print errors or pause.

  • hostname -i (lowercase) resolves the name and can return 127.0.1.1 or 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).

  • -I and -i are Linux (Debian hostname) options. On macOS, hostname -s and -f exist, scutil --get ComputerName and scutil --set HostName manage the names, and addresses come from ifconfig.

Options worth knowing

A selection, described in our own words. For the full list, see hostname(1) or hostname --help.

OptionWhat it does
-s, --shortPrint the name up to the first dot.
-f, --fqdnPrint the fully qualified domain name (needs name resolution).
--longSame as --fqdn.
-d, --domainPrint the DNS domain part of the FQDN.
-i, --ip-addressPrint the addresses the host name resolves to.
-I, --all-ip-addressesPrint all addresses of all interfaces except loopback.
-A, --all-fqdnsPrint the FQDNs of all interface addresses.
-F, --file FILESet the host name from the first line of FILE (root).
-y, --ypPrint the NIS domain name.

Related