Get the machine hostname
Print the current machine's hostname (short or FQDN).
How to get the machine hostname in each shell
hostnameBare `hostname` prints the short form (the kernel's `nodename`). `hostname -f` resolves the FQDN via DNS (slow + can FAIL if DNS misconfigured — falls back to `localhost.localdomain`). `hostname -s` forces short. `$HOSTNAME` (bash builtin) is the short form, no DNS lookup. `hostnamectl` on systemd hosts shows the persistent + transient + pretty names plus chassis/icon.
hostnameSame external `hostname`. macOS BSD `hostname` does NOT support `-f`/`-s`/`-d` flags — output is whatever was set by `scutil --set HostName` / `--set LocalHostName` / `--set ComputerName` (three separate names on macOS — `HostName` is /etc/hosts-ish, `LocalHostName` is Bonjour, `ComputerName` is the Finder/UI name). `scutil --get ComputerName` shows the user-visible name.
hostnameSame external. Fish variable: `$hostname` (lowercase) auto-populated. For scripts use the variable to avoid forking `hostname`: `set -l host $hostname` (no subshell).
$env:COMPUTERNAME`$env:COMPUTERNAME` returns the NetBIOS name (≤ 15 chars uppercase). `[System.Net.Dns]::GetHostName()` is the same but cross-OS — works on pwsh 7 Linux/macOS where `COMPUTERNAME` env var is unset. For FQDN: `[System.Net.Dns]::GetHostEntry($env:COMPUTERNAME).HostName` (DNS-dependent, can hang). `hostname.exe` also exists on Windows (a C binary, same as Unix).
hostname`hostname.exe` (in System32) — shipped on all Windows since NT. `%COMPUTERNAME%` env var (`echo %COMPUTERNAME%`) is faster (no process spawn). For FQDN: `ipconfig /all | find "Host Name"` shows the short name; `ipconfig /all | find "Primary Dns Suffix"` gives the domain to append.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- **Short vs FQDN vs NetBIOS — three different things**: `hostname` (short) = kernel `nodename`, set at boot from `/etc/hostname` (Linux) or `scutil` (macOS) or registry (Windows). `hostname -f` (FQDN) = short + DNS-resolved domain suffix; depends on `/etc/resolv.conf` `search` directives + the reverse-DNS record. NetBIOS name (Windows) = uppercase ≤ 15-char machine name visible on Windows networks; usually equals the short hostname but TRUNCATED if > 15 chars. They can all be different on a single host.
- **`hostname -f` can hang**: a DNS-misconfigured host or air-gapped lab machine will block on `hostname -f` for the resolver timeout (typically 5–30 seconds). For scripts that just want to identify the machine, prefer `hostname -s` or `$HOSTNAME` — both are DNS-free.
- **Persisting changes — distro differences**: `hostname new-name` sets the RUNTIME hostname (lost on reboot). To persist: systemd Linux uses `hostnamectl set-hostname new-name`; non-systemd Linux edits `/etc/hostname` + `/etc/hosts`; macOS uses `sudo scutil --set HostName / --set LocalHostName / --set ComputerName` (three commands for three names); Windows uses `Rename-Computer -NewName new-name -Restart` (requires reboot — the only one of the four that does).
- **Container hostname**: inside Docker `hostname` is the container ID short hash unless overridden with `docker run --hostname my-host`. Kubernetes pods get the pod name as hostname. Inside the container, `hostname -f` may fail (no DNS-zone entry) — set `hostname` and `subdomain` in the pod spec + use a headless Service to get a stable FQDN like `my-pod.my-svc.my-ns.svc.cluster.local`.