Example Uses of the hostname Command

Make your computer easy to spot on your network

It is likely that you set up your computer's name when installing Linux in the first place, but if you are using a computer set up by somebody else you might not know its name.

You can find and set the name for your computer to make it easier for people to discover you on a network by using the hostname command.

Linux screenshot
The Spruce

How to Determine Your Computer's Name

Open a terminal window and type the following command:


You will receive a result telling you the name of your computer and in our case, it simply said 'localhost.localdomain'.

The first part of the result is the computer's name and the second part is the name of the domain.

To return just the computer name you can run the following command:

hostname -s

The result this time will simply be 'localhost'.

Similarly, if you just want to find out which domain you are on use the following command.

hostname -d

You can find the IP address for the hostname by using the following command:

hostname -i

A host name can be given an alias and you can find out all the aliases for the computer you are using by typing the following command into the terminal:

hostname -a

If there are no aliases set up your actual hostname will be returned.

How to Change the Hostname

You can change the computer's hostname by simply typing the following command:


For example:

hostname gary

Now when you run the hostname command it will simply display 'gary'.

This change is temporary and isn't particularly useful.

To permanently change your hostname use the nano editor to open the /etc/hosts file.

sudo nano /etc/hosts

You will need elevated privileges to edit the hosts file and so you can either use the sudo command as shown above or you can switch users to the root account using the su command.

On many Linux systems, you'll need to edit /etc/hostname to change your computer's hostname. If the file exists on your system, it will only contain the current hostname.

The /etc/hosts file has details about your computer and other machines on your network or on other networks.

By default your /etc/hosts file will contain something like this: localhost.localdomain localhost

The first item is the IP address to resolve for the computer. The second item is the name and domain for the computer and every subsequent field provides an alias for the computer.

To change your hostname you can simply replace localhost.localdomain with the name of the computer and the domain name.

For example: gary.mydomain localhost

After you have saved the file you will get the following result when you run the hostname command:


Similarly the hostname -d command will show as mydomain and hostname -s will show as gary.

The alias command (hostname -a) however will still show as localhost because we haven't changed that in the /etc/hosts file.

You can add any number of aliases to the /etc/hosts file as shown below: gary.mydomain garysmachine everydaylinuxuser

Now when you run the hostname -a command the result will be as follows:

garysmachine everydaylinuxuser

More About Hostnames

A host name must be no more than 253 characters and it can be split up into different labels. 

For example:


The above host name has three labels:

  • en
  • Wikipedia
  • Org

The label can be a maximum of 63 characters long and the labels are separated by a single dot.


There isn't much else to say about the hostname command. You can find out about all of the available switches by reading the Linux main page for a hostname.

man hostname

Everything you really need to know has been covered in this guide, but there are a few other switches such as hostname -f which shows the fully qualified domain name, the ability to read the hostname from a file by using the hostname -f switch and the ability to show the NIS/YP domain name by using the hostname -y switch.