Alex Williams  â€”

Last updated:

Share this Post

A hostname is basically a label that's assigned to a device, computer or server. The hostname typically, in the reference of the internet, is the domain name. That points to the IP address, which is the numerical address or location of the Web server. For example, if your organization domain name is example.com and a computer name in that domain is linux, then the hostname of the computer is linux.example.com.

In this tutorial, we will explain how to use hostname command using the different examples in Linux.


How to Use Linux Hostname Command

Lets begin, shall we? 🎬

Basic Syntax 🧮

The basic syntax of the hostname command is shown below:

hostname [options] [file]

A brief explanation of each option is shown below:

  • ☑️ -h : This option shows you help about hostname command.
  • ☑️ -s : This option display the short hostname.
  • ☑️ -V : This option displays the version information of hostname command.
  • ☑️ -a : This option displays the alias name of the host.
  • ☑️ -f : This option display the fully qualified domain name of the host.
  • ☑️ -d : This option displays the domain name associated with the host.
  • ☑️ --all-ip-addresses : This option display the network address of the host.
  • ☑️ -y : This option display the NIS domain name.

You can see the help information about hostname command using the following command:

hostname -h

You should see the following screen:

Hostingname Command


Hostname Command Examples

Try It for Yourself 📚

Practicing Hostname Commands 🎖️

In this section, we will show you how to use hostname command with several examples.

1. To display the hostname of your system, open your terminal and run the following command:

hostname

You should see the following output:

linux.example.com

run hostname command

2. To display the IP address of your system, run the hostname command with -i option as shown below:

hostname -i

You should see the following output:

2a06:1301:4050:45:148:29:37:0 45.148.29.37 fe80::216:3eff:fea6:3035

hostname with i

3. To display the domain name associated with your system, run the hostname command with -d option as shown below:

hostname -d

You should see the following output:

example.com

hostname with d

4. To display the short hostname of your system, run the hostname command with -s option as shown below:

hostname -s

You should see the following output:

linux

hostname s

5. To display the fully qualified domain name of your system, run the hostname command with -f option:

hostname -f

You should see the following output:

linux.example.com

hostname f

6. To display all the IP addresses of your system, run the hostname command with -I option:

hostname -I

You should see the following output:

45.148.29.37 2a06:1301:4050:45:148:29:37:0

hostname I

7. To display the version number of the hostname command, run the hostname command with -V as shown below:

hostname -V

You should see the following output:

hostname 3.20

hostname V

8. To change the hostname of your system, run the following command:

hostname test.com

You can verify your new hostname with the following command:

hostname

You should see the following output:

test.com

hostname test com

Please remember, the above command changes the hostname temporary. If you want to change your system hostname permanently, run the following command:

hostnamectl set-hostname test.com

9. To display the manual page of the hostname command, run the following command:

man hostname

You should see the following screen:

man hostname


Conclusion

A Brief Summary Before You Go 👋

In the above article, we learned how to use hostname command in Linux with several examples. I hope you have now enough knowledge to work around with the hostname command.

Top Tip ⭐

Don't forget the examples. Practice makes perfect.