Pin Me

Linux Command Line: hostname

written by: jlwallen•edited by: J. F. Amprimoz•updated: 7/4/2011

For Linux servers you will have to make sure you know your hostnames. Fortunately there is a very handy command line tool for this purpose. That tool is called hostname. In this entry to the Linux Command Line series you will be introduced to the hostname command.

  • slide 1 of 4

    What is a hostname?

    A hostname is the name given to a server that is part of a network. But a server name must follow certain rules. Most will recognize the FQDN - Fully Qualified Domain Name - such as Let's take a look at that sample FQDN. We'll break the name into two sections: mail and Say you are a business and you own the domain name "". You want to set up various servers to serve up web, mail, ftp, etc. To have all of these servers on one domain you might have a machine called, a machine called, and a machine called Now you have mail, web, and ftp all on your single domain.

  • slide 2 of 4

    Basic Usage

    If you issue the command hostname -a, if you haven't already set your hostname, you might see something like this:


    The -a switch instructs hostname to report the alias name of the host (if one is used.) If you issue the command hostname -d you might see something like this:


    The -d switch instructs hostname to report the DNS domain name of the server. If you issue the command hostname -f you might see something like this:


  • slide 3 of 4

    Setting the hostname

    You can also set the hostname of the server with the command hostname YOURHOSTNAME (Where YOURHOSTNAME is the hostname you want to use.) Now a problem can arise. If the hostname you set with the command differs from the hostname in the /etc/hosts file you will have some problems with your machine. If you need to set your hostname with the command you will want to edit the /etc/fstab file to match what you have entered with hostname.

  • slide 4 of 4

    Final Thoughts

    The most useful aspect of the hostname command is, without a doubt, the ability to check hostnames on servers. Being able to set that hostname with this command is a definite plus. Just make sure you don't create a discrepancy between the hostname command and /etc/hosts.