hostname
Show or set the system's hostname and IP addresses
By CMD Script Team · 3 min read · Last updated
hostname [OPTIONS] [NEW_HOSTNAME]Options
| Flag | Description |
|---|---|
-I | Print all local IP addresses (IPv4 and IPv6) assigned to the host, space-separated |
-i | Print the IP address(es) associated with the hostname, as resolved via /etc/hosts or DNS |
-f | Print the fully qualified domain name (FQDN) |
-s | Print only the short hostname, stripped of any domain suffix |
-d | Print the DNS domain name portion only |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (supports plain hostname; -I not available, use ipconfig getifaddr)
What it does
hostname prints or sets the system's host name — the label a machine identifies itself
by on a network. Run with no arguments it just prints the current hostname; with flags
like -I or -i it can also report the machine's IP addresses, and given an argument it
can set a new (temporary) hostname.
Beginner examples
hostname— print the current hostnamehostname -I— print all local IP addresses assigned to the machinehostname -f— print the fully qualified domain namehostname -s— print just the short name, without any domain suffix
hostname -I
Advanced examples
- Grab just the first local IP for use in a script:
hostname -I | awk '{print $1}' - Compare short vs. fully qualified name:
hostname -svshostname -f - Temporarily rename a host for testing (until reboot):
sudo hostname tempname - Persist a hostname change across reboots:
sudo hostnamectl set-hostname web01 - Confirm DNS resolution of the hostname matches expectations:
hostname -i
sudo hostnamectl set-hostname web01
Common mistakes
- Running
hostname newnameand expecting it to survive a reboot — a plainhostnamecommand only changes the runtime kernel hostname, not the persisted value in/etc/hostname. - Using
hostname -ito find "my server's IP" and getting127.0.1.1back because/etc/hostsmaps the hostname to a loopback address —hostname -I(capital I) is usually what's actually wanted. - Forgetting that
hostname -frequires the hostname to actually resolve (via DNS or/etc/hosts) to a fully qualified name, or it will error out. - Assuming
hostnamealone reflects what other machines resolve for you on the network — DNS records can differ from the locally configured hostname.
Tips
- Prefer
hostnamectlover barehostnamefor making changes on systemd-based distros, since it updates/etc/hostname,/etc/hosts, and pretty/static/transient names consistently. - Use
hostname -I | awk '{print $1}'in shell scripts that need "the" primary IP without extra parsing logic. - On multi-homed servers (multiple NICs),
hostname -Ireturns every address — filter it down if you need a specific interface's IP.
Best practices
- Use
hostnamectl set-hostname(not plainhostname) whenever a hostname change should persist across reboots. - Don't rely on
hostname -ito determine a server's real network-facing IP in automation; preferhostname -Ior a dedicated tool likeip addr show. - Keep
/etc/hostsand DNS records in sync with the configured hostname to avoidhostname -ffailing or returning unexpected values.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Quickly identifying which server you're SSH'd into when working across many machines.
- Grabbing a server's local IP address inside a provisioning or deployment script.
- Setting a meaningful hostname (
db-primary,web01) during initial server setup. - Debugging why a service reports the wrong internal IP by checking
hostname -Iagainst actual interface addresses.
Common interview questions
- What's the difference between
hostname -Iandhostname -i?-Ilists every local IP address on the machine;-iresolves the configured hostname to an IP via/etc/hosts/DNS, which can differ or even return a loopback address. - How do you permanently change a Linux hostname? Use
hostnamectl set-hostname <name>(systemd), which updates/etc/hostname; a barehostname <name>only lasts until reboot. - Why might
hostname -ffail with an error? Because it requires the current hostname to resolve to a fully qualified domain name via DNS or/etc/hosts; if it doesn't resolve, the command errors out.
Frequently Asked Questions
What's the difference between hostname -I and hostname -i?
hostname -I lists every local IP address currently assigned to any network interface on the machine; hostname -i resolves the hostname (as it would appear in /etc/hosts or DNS) to an IP address, which may not match every interface IP, especially on multi-homed hosts.
How do I permanently change a Linux hostname?
Running hostname newname only changes it until reboot. To persist it, use hostnamectl set-hostname newname on systemd distros, which also updates /etc/hostname.
Why does hostname -i sometimes return 127.0.1.1 instead of a real network IP?
This happens when /etc/hosts maps the hostname to a loopback-range address (common on Debian/Ubuntu defaults). hostname -I is more reliable for getting the actual network-facing IP.
Cheat sheet
Download a quick-reference cheat sheet for hostname.