Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Networking

Linux hostname command

Reference for the Linux hostname command: show the short, fully qualified and IP forms of the host name, set it permanently with hostnamectl, and use it in scripts.

hostname prints the name of the machine, the label it uses on the network and in its shell prompt. With options it prints the short name, the fully qualified domain name (FQDN) or the machine's IP addresses; run as root with a name argument, it changes the name until the next reboot.

The name is stored in the kernel. To make a change permanent you write it to /etc/hostname (or use hostnamectl), and it is looked up through /etc/hosts and DNS when programs need to turn it into an address.

Terminal
hostname [OPTION]
sudo hostname NEWNAME

Try it

The sandbox picks its own host name, so these examples compare forms and never print the name itself, which keeps the output stable:

Bash Forms of the host name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Splitting an FQDN

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
hostname [-a|-A|-d|-f|-i|-I|-s|-y]
hostname [-b] {NAME | -F FILE}
Option Description
(none) Print the current host name.
-s, --short The short name: everything before the first dot.
-f, --fqdn The fully qualified name, resolved through the resolver.
-d, --domain The DNS domain name (the part after the first dot of the FQDN).
-i The address the host name resolves to.
-I All configured addresses of the host, space separated (not loopback).
-a Alias names, if any.
-A All FQDNs known for the host's addresses.
-y NIS domain name (rarely used).
-F FILE Set the name from the first line of a file.
-b Set a default name if the current one is empty at boot.

Returns: 0 on success; 1 with a message when the name cannot be resolved (for -f, -d, -i) or when you lack permission to set it.

Usage notes

  • Permanent change. sudo hostnamectl set-hostname web01 updates /etc/hostname and the running kernel in one step on systemd systems. Plain hostname NEWNAME lasts only until reboot.
  • /etc/hosts should list it. If hostname -f fails or sudo prints "unable to resolve host", add a line such as 127.0.1.1 web01 to /etc/hosts.
  • Valid names. Use letters, digits and hyphens; no underscores, at most 63 characters per label and 253 in total. Start with a letter or digit.
  • -i and -I differ. -i resolves the name (can return 127.0.1.1); -I lists real interface addresses. For scripts that need an IP, -I is usually closer to what you mean.
  • In containers the host name defaults to the container ID, and --hostname or --name options set it.
  • uname -n prints the same node name from the kernel; $HOSTNAME is a Bash variable that holds it for the shell session.
  • Scripts. Use $(hostname -s) for log file names, but do not rely on it being unique across a fleet.