Example Uses Of The hostname Command

The Linux Hostname
The Linux Hostname.


Your computer has a name and it is likely that you set it up 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.

This guide teaches you everything you need to know about the hostname command.

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 my 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.

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:

  • Linux
  • About
  • Com

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

You can find out more about hostnames by visiting this Wikipedia page. You can also visit this page at pcsupport.about.com.


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 man 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.