The “hostname” command in Linux is used to check the system name that is assigned to a Linux machine. However, this command can also be paired up with other different flags for getting different outputs. We will try to throw light on some examples of the “hostname” command in Linux in this article.

General Syntax of the “hostname” Command:

The general syntax of the “hostname” command in Linux is shown below:

Here, you can replace “parameter” with any flags or options used with the “hostname” command. Some of those flags or options are also described in the examples below. However, if you want to execute the simple “hostname” command, you can also do that without stating any parameter.

Examples of Linux “hostname” Command:

For learning the usage of the “hostname” command in Linux, you can go through the following examples:

Example # 1: Check the Hostname of your Linux System:

This command is most commonly used for checking the hostname of your Linux system by running it in the manner shown below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-591.png" data-lazy- height="26" src="data:image/svg xml,” width=”312″>

Our Linux system’s hostname is shown in the following image:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-592.png" data-lazy- height="57" src="data:image/svg xml,” width=”319″>

Example # 2: Check the IP Address against the Hostname of your Linux System:

You can also check the IP address that is assigned to your Linux machine against its hostname by using the “hostname” command with the “-i” parameter as follows:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-594.png" data-lazy- height="21" src="data:image/svg xml,” width=”336″>

The IP address against the hostname of our Linux system is shown in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-596.png" data-lazy- height="56" src="data:image/svg xml,” width=”336″>

Example # 3: Check the Fully Qualified Domain Name of your Linux System:

If you want to check the fully qualified domain name of your Linux system, then you can use the “-f” flag with the “hostname” command as follows:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-598.png" data-lazy- height="20" src="data:image/svg xml,” width=”338″>

The fully qualified domain name of our Linux system is shown in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-599.png" data-lazy- height="56" src="data:image/svg xml,” width=”340″>

Example # 4: Check the Alias of the Hostname of your Linux System:

At times, users also set aliases against the hostnames of their systems. If you want to check the alias against the hostname of your system (if it exists), then you can use the “-a” parameter with the “hostname” command as follows:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-601.png" data-lazy- height="25" src="data:image/svg xml,” width=”339″>

Since we had not set up an alias for our system’s hostname, therefore, we were presented with a blank output, as shown in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-603.png" data-lazy- height="56" src="data:image/svg xml,” width=”341″>

Example # 5: Check the Short Hostname of your Linux System:

If there is a dot in your hostname, then the short hostname refers to the portion of your hostname before that dot. You can display the short hostname of your Linux system by using the “-s” flag with the “hostname” command as follows:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-605.png" data-lazy- height="22" src="data:image/svg xml,” width=”335″>

Since we did not have a dot in our system’s hostname, therefore, the short hostname of our Linux system is the same as our actual hostname, which is shown in the image below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-606.png" data-lazy- height="58" src="data:image/svg xml,” width=”336″>

Example # 6: Change the Hostname of your Linux System:

If you want to change the hostname of your Linux system, then you can also do that while using the “hostname” command in the following manner:

$ sudo hostname NewHostName

Here, you will have to replace NewHostName with any desired new hostname you want to set up for your Linux system. This command will not show any output on the terminal upon successful execution.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-608.png" data-lazy- height="24" src="data:image/svg xml,” width=”410″>

You can confirm if a new hostname has been set up for your system or not by executing the command shown below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-610.png" data-lazy- height="24" src="data:image/svg xml,” width=”313″>

From the output of the above-mentioned command, it is quite evident that our hostname has been changed successfully, as shown in the following image:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-612.png" data-lazy- height="59" src="data:image/svg xml,” width=”322″>

Conclusion:

We discussed the six different examples of using the “hostname” command in Linux in this article. To learn more about the usage of this command in Linux, you can run the “hostname –help” command in your system’s terminal.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/karim.png" height="112" src="data:image/svg xml,” width=”112″>

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.