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
tecmint
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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

2 Comments

Leave a Reply
  1. 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.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.