System and processes

The hostname command

Shows or changes the machine name on the network.

Syntax

hostname [opciones] [nuevo-nombre]Try it in the terminal →

Most useful options

-sShort name: everything before the first dot
-fFull name (FQDN), with the domain included
-dOnly the DNS domain of the machine
-iThe IP addresses associated with the name
-IAll the IP addresses of the interfaces, without relying on DNS. It belongs to the Debian and Ubuntu hostname; other versions may lack it
-aThe machine's alias, if it has one
-F archivoReads the name from a file instead of the command line

Copy-ready examples

$ hostnameshows the machine name.

Example output

miportatil
$ hostname -sshows the short name, useful in scripts or in a prompt.
$ hostname -fshows the full name, with the domain if the machine has one.
$ hostname -ishows the IP address associated with the machine name.
$ hostname -Ishows the IP addresses of all interfaces, on Debian and Ubuntu. It is usually what you want to find your local IP.
$ sudo hostname servidor-pruebaschanges the name on the fly. Do it only on your own machine: services that read the name at startup will not see it until they restart.

You can try them in the browser terminal: it does not touch your computer.

Typical mistakes

  • Changing it with hostname does not save it: to make it persist across reboots, use hostnamectl.
  • hostname -f can fail if the machine has no full name that resolves. In that case use -s.
  • Some programs read the name only at startup: a change may not show up in them until they restart.
  • Do not mix up -s and -f: the first cuts at the first dot and the second shows the whole name.

Differences from similar commands

  • hostnamectl: shows and changes the name persistently, through systemd.
  • uname: uname -n shows the node name, but has no options for domain, alias or IP.
  • dnsdomainname: shows only the DNS domain, the same as hostname -d.

Related commands

FAQ

How do I rename the machine permanently?

With sudo hostnamectl set-hostname new-name, which keeps the name across reboots.

What is the difference between the short and full names?

The short name is everything before the first dot. The full name (FQDN) includes the domain, such as server.example.com. Use -s or -f depending on what you need.

Can I read the name without being root?

Yes. Showing it needs no privileges; only changing it requires root.

More System and processes commands

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course