hostname: Usage, Options & Common CI Errors
hostname prints (or sets) the machine name and can show its IP addresses.
hostname seems trivial until a container's own name does not resolve. The FQDN flag (-f) needs a working resolver, and -I vs -i behave very differently - a frequent source of confusing CI failures.
What it does
hostname prints or sets the system's network name. With flags it also reports the FQDN (-f) or the host's IP addresses (-I, -i). In containers the hostname is usually the container ID, which may not be resolvable.
Common usage
hostname # the short hostname
hostname -f # fully qualified domain name
hostname -I # all IP addresses (space separated)
hostname -i # IP from name resolution (can fail)
hostname new-name # set the hostname (needs privilege)Options
| Flag | What it does |
|---|---|
| (none) | Print the short hostname |
| -f / --fqdn | Print the fully qualified domain name |
| -I / --all-ip-addresses | All addresses, no name resolution |
| -i / --ip-address | Resolve the hostname to an IP (uses DNS) |
| -s / --short | Short name (up to the first dot) |
Common errors in CI
hostname -f / hostname -i fail with "hostname: Name or service not known" when the container's own name is not in /etc/hosts and DNS cannot resolve it - common with random container IDs. Prefer hostname -I, which reads addresses from the interfaces and does NOT need resolution. -I returns a space-separated list (possibly several IPs) - do not assume a single value. Setting the hostname needs privilege ("hostname: you must be root to change the host name").