5 ‘hostname’ Command Examples for Linux Newbies

A hostname command is used to view a computer’s hostname and domain name (DNS) (Domain Name Service), and to display or set a computer’s hostname or domain name.

A hostname is a name that is given to a computer that attached to the network that uniquely identifies over a network and thus allows it to be accessed without using its IP address.

The basic syntax for the hostname command is:

# hostname [options] [new_host_name]

In this short article, we will explain 5 useful hostname command examples for Linux beginners to view, set or change Linux system hostname from the Linux command-line interface.

If you run hostname command without any options, it will displays the current host name and domain name of your Linux system.

$ hostname
Show Linux Hostname

Show Linux Hostname

If the host name can be resolved, you can display the network address(es) (IP address) of the host name with the -i flag and the -I option establishes all configured network interfaces and shows all network addresses of the host.

$ hostname -i
$ hostname -I
Show Hostname IP Addresses

Show Hostname IP Addresses

To view the name of the DNS domain and FQDN (Fully Qualified Domain Name) of your machine, use the -f and -d switches respectively. And the -A enables you to see all the FQDNs of the machine.

$ hostname -d
$ hostname -f
$ hostname -A
Show Host DNS Names

Show Host DNS Names

To display the alias name (i.e., substitute names), if used for the host name, use the -a flag.

$ hostname -a

Last but not least, to change or set hostname of your Linux system, simply run the following command, remember to replace “NEW_HOSTNAME” with the actual hostname that you wish to set or change.

$ sudo hostname NEW_HOSTNAME
Set Linux System Hostname

Set Linux System Hostname

Note that the changes made using the above command will only last until the next reboot. Under systemd – system and service manager, you can use the hostnamectl command to permanently set or change your system hostname as explained in the following articles.

  1. How to Set or Change System Hostname in Linux
  2. How to Set or Change Hostname in CentOS 7

That’s It! In this short article, we explained 5 hostname command examples for Linux newbies. If you have any questions, use the feedback form below to reach us.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

1 Response

  1. Stuart Watt says:

    The bit about the switches for hostname, -d and -f, I think you have them switched over. -f is for the FQDN, and -d the domain.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.