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.
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:
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.
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
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 web01updates/etc/hostnameand the running kernel in one step on systemd systems. Plainhostname NEWNAMElasts only until reboot. /etc/hostsshould list it. Ifhostname -ffails orsudoprints "unable to resolve host", add a line such as127.0.1.1 web01to/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.
-iand-Idiffer.-iresolves the name (can return127.0.1.1);-Ilists real interface addresses. For scripts that need an IP,-Iis usually closer to what you mean.- In containers the host name defaults to the container ID, and
--hostnameor--nameoptions set it. uname -nprints the same node name from the kernel;$HOSTNAMEis 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.
Related
ip,host,digandnslookup- Lessons: Network Configuration, Networking Basics