Linux Common Commands: The hostname Command

Linux Common Commands: The hostname Command
Page content

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.

This post is part of the series: Linux Command Line

If you ever plan on doing any administration on a Linux machine, you would be well served to get to know the command line interface. In this Bright Hub series you will be introduced to various concepts surrounding one of the most powerful admin tools around.

  1. Linux Command Line: Introduction
  2. Linux Command Line: ls
  3. Linux Command Line: cd
  4. Linux Command Line: mkdir
  5. Linux Command Line: df
  6. Linux Command Line: ln
  7. Linux Command Line: top
  8. Linux Command Line: mount/umount
  9. Linux Command Line: Cron/Crontab
  10. Linux Command Line: chmod
  11. Linux Command Line: wget
  12. Linux Command Line: cat
  13. Linux Command Line: grep
  14. Linux Command Line: dd
  15. Linux Command Line: sudo
  16. Linux Command Line: startx
  17. Linux Command Line: adduser
  18. Linux Command Line: at
  19. Linux Command Line: aterm
  20. Linux Command Line: nano
  21. Linux Command Line: hostname