How to set the hostname of a machine

In this article you will learn how to set and check the hostname of a machine.

Note: These instructions will work with almost all Linux / Unix distributions.

What is a hostname?

A hostname is name of your machine or computer. Generally referred by layman users of “Microsoft Windows” as a Computer name which they actually see over the network and other services. Hostnames are used primarily for administrative tasks, application related, identification, IP resolution and even more.

If you’re talking about Linux / Unix then you either need to set up your hostname manually or during installation of the operating system. If you did not choose or enter any hostname during installation most likely your hostname will be “localhost.localdomain”

To find out what is your current hostname we use following command: # hostname

Most likely, if you did not choose or enter any hostname during installation you will get output as “localhost.localdomain”

You can also easily identify whether your hostname has been changed when you login to get prompt, something like below:

[root@localhost ~]#

In above prompt you can see that “localhost” tells you that you have the default hostname.

Hostname command

Now let’s come back to the “hostname” command. Hostname command has a lot of switches each of them have different usage and outputs which you can study by using the “man” command which means manual.

# man hostname

Administration

Now let’s talk about some of the important hostname switches that are used in the system administration world:

# hostname –a

Above the hostname command displays the complete hostname that is currently set. In some cases the “hostname” command may or may not display the complete hostname; “-a” switch forces the hostname command to display complete hostname.