Hostname Command in Linux – 12 Examples with All Options

Last updated: June 15, 2022

A hostname is a name that is given to a computer and used to identify the computer in a network. In this tutorial, we learn about hostname command in Linux with practical examples.

Prerequisites

  • A Linux machine.
  • Access to the terminal.
  • Basic knowledge of using Linux command line.

Hostname Command

The hostname command is commonly used to display Linux computer name or its DNS domain name. You can use it to set a hostname for the computer. This only set the hostname temporarily until the next reboot.

Historically /etc/hostname file was supposed to only contain the hostname and not the full canonical FQDN. Nowadays most software is able to cope with a full FQDN here. This file is read at boot time by the system initialization scripts to set the hostname. /etc/hosts Usually, this is where one sets the domain name by aliasing the host name to the FQDN.

The basic syntax for the Linux hostname command:

hostname [options] [File] [new_hostname]

Hostname command options

Some of the useful options for Linux hostname command.

OptionsDescription
-aDisplays alias names of the host system.
-Aenumerates all configured network addresses on all configured network interfaces, and translates them to DNS domain names. (fqdn fully qualified domain)
-bset default hostname if none is available
-dDisplay DNS domain name.
-fDisplays long hostname (FQDN). A FQDN consists of a short host name and the DNS domain name
-Fread hostname or NIS domain name from the given file
-iDisplays all configured addresses for the hostname
-Iestablishes all configured network interfaces and displays all addresses for the host
-sDisplays short hostname.
-VPrint version information on standard output and exit successfully 
-yDisplays the Network Information System NIS/YP domain name

Hostname Command Examples

The following examples will show how you can use the hostname command with the options.

1. Display Linux Hostname

To display hostname for your Linux machine, the following program displays it:

hostname
hostname  command displays computer hostname host1.example.com

2. Display IP address

Situations for needing your Linux machine IP address might arise, to display your IP, use the following command:

hostname -i
Using hostname command show ip

And if you need to see all your IP addresses for the machine, use the following command:

hostname -I

Alternatively use ip addr command which gives more information for all addresses.

3. Display DNS domain name

For displaying your current host DNS domain name, the following hostname command gets the job done:

hostname -d
hostname command dns

In this case, the machine didn’t return any value because a local domain is not set

And if it's the NIS domain name you would like to see, use the following command:

hostname -y
hostname command showing Network Information Services (NIS)

The NIS also returned an error value because a local domain name is not set.

4. Display short hostname

To display your Linux machine short hostname, use the following command:

hostname -s
command short hostname

5. Display all Fully Qualified Domain Names (FQDNs)

To display all your Linux machine FQDNs, the following command will achieve that:

hostname -A

Nevertheless, if you want to only display the machine FQDN, use the following command:

hostname -f
hostname command shows fqdn

6. Change Linux hostname

Maybe you already have a hostname set for your linux machine, but you wish to change it to a new hostname, the following command can be used:

sudo hostname new-hostname
hostname change hostname temporarily

Only the super-user can change a hostname, which explains the “sudo” command appended to the hostname command. Remember changing hostname using hostname will only keep it temporary.

7. Set a default hostname

To set a default system hostname (default hostname localhost) for your linux machine if you don't have any, the following hostname command will get the job done:

hostname -b

The machine returned a hostname because a hostname has been set.

If you need your machine to read a hostname or a NIS domain name from a specified file, use the following command:

hostname -F

8. Display Linux alias name

If an alias name is set for your Linux machine, the following hostname command will display it:

hostname -a

Command returns a blank line, which means no alias name is set for this system.

9. Hostname help

If you need help understanding more about the hostname command, you can use the help command to figure it out. To use the help command, type the following:

hostname --help
hostname help page

Conclusion

In this tutorial, we learned about Linux hostname command and its options.

Thanks for reading please leave your feedback and suggestions in the comment section.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment