Linux Command Line: hostname

Written by:  • Edited by: J. F. Amprimoz
Updated Jul 4, 2011
• Related Guides: Linux Command Line | Command Line Tool | Linux

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.

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 mail.yourdomain.com. Let's take a look at that sample FQDN. We'll break the name into two sections: mail and yourdomain.com. Say you are a business and you own the domain name "yourdomain.com". 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 mail.yourdomain.com, a machine called www.yourdomain.com, and a machine called ftp.yourdomain.com. Now you have mail, web, and ftp all on your single domain.

Basic Usage

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

localhost

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:

localdomain

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:

localhost.localdomain

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.

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.


Comments

Showing all 2 comments
 
george Nov 19, 2010 6:22 AM
What can i say?
Just my congratulations.
Learned a lot
Thank you
Dan Blevins Aug 24, 2009 4:55 PM
Malware
AVAST detected malware in the file "mkdir" .

I learned much here today.
Thank you for the gift of knowledge.
 
blog comments powered by Disqus
Email to a friend