>_cmd.script

hostname

Show or set the system's hostname and IP addresses

System

By CMD Script Team · 3 min read · Last updated

SYNTAX
hostname [OPTIONS] [NEW_HOSTNAME]

Options

Command options and flags
FlagDescription
-IPrint all local IP addresses (IPv4 and IPv6) assigned to the host, space-separated
-iPrint the IP address(es) associated with the hostname, as resolved via /etc/hosts or DNS
-fPrint the fully qualified domain name (FQDN)
-sPrint only the short hostname, stripped of any domain suffix
-dPrint 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 hostname
  • hostname -I — print all local IP addresses assigned to the machine
  • hostname -f — print the fully qualified domain name
  • hostname -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 -s vs hostname -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 newname and expecting it to survive a reboot — a plain hostname command only changes the runtime kernel hostname, not the persisted value in /etc/hostname.
  • Using hostname -i to find "my server's IP" and getting 127.0.1.1 back because /etc/hosts maps the hostname to a loopback address — hostname -I (capital I) is usually what's actually wanted.
  • Forgetting that hostname -f requires the hostname to actually resolve (via DNS or /etc/hosts) to a fully qualified name, or it will error out.
  • Assuming hostname alone reflects what other machines resolve for you on the network — DNS records can differ from the locally configured hostname.

Tips

  • Prefer hostnamectl over bare hostname for 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 -I returns every address — filter it down if you need a specific interface's IP.

Best practices

  • Use hostnamectl set-hostname (not plain hostname) whenever a hostname change should persist across reboots.
  • Don't rely on hostname -i to determine a server's real network-facing IP in automation; prefer hostname -I or a dedicated tool like ip addr show.
  • Keep /etc/hosts and DNS records in sync with the configured hostname to avoid hostname -f failing 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 -I against actual interface addresses.

Common interview questions

  • What's the difference between hostname -I and hostname -i? -I lists every local IP address on the machine; -i resolves 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 bare hostname <name> only lasts until reboot.
  • Why might hostname -f fail 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.