Linux “hostname” Command Examples

28

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:

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

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:

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

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:

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

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:

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:

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:

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:

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.

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

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:

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More