Updated April 17, 2023
Introduction to hostname Command in Linux
In the Linux ecosystem, the hostname command is used to get the hostname of the Linux server. With the help of hostname command, we are able to know from which domain the current machine belongs to. The hostname of the individual machine or server should be unique in the single network. If we will get the two hostnames with a similar name then there is trust relationship error. Hence we need to make sure that, we need all the unique hostname in the network.
Syntax of hostname Command:
hostname [OPTION][-F file]
- hostname: We can use the hostname keyword in the syntax or command. It will take the argument as an option and hostname or file. It will provide the end result with the server or machine hostname with domain information.
- OPTION: We can provide the different flags as options that are compatible with the hostname command.
- file: We can use the test file as file input and ready the hostname from the same specified file.
How does Linux hostname Command work?
The hostname command is used to get the hostname of the server or machine. It is very important in terms of web servers or application deployment. Because without a valid hostname of the server, we are not able to access the webserver contain or data on the web browser (via hostname).
There are different ways to set the hostname, we can set the hostname at the booting process or we can set the hostname after the Linux server installation. To update the hostname entry, we can use the “/etc/hosts” file. In the same file, we can update the multiple hostname entry for the same server.
Below are the lists of options available in the hostname command:
Sr. No | File Name | Description |
1 | -a | It is useful to display the alias name of the current host (if it is useful) but this option was vanished and not in use. |
2 | -A | It will display all the full qualified domain name of the server. |
3 | -b | While booting process, it will set the hostname. But if we will not set the hostname at the booting process then by default it will set the hostname as localhost. |
4 | -d | It will print the name of the DNS Domain. |
5 | -f | It will print the full qualified domain name. |
6 | -F | It will read the hostname from the input file. |
7 | -i | It will display the IP address of the hostname associated with it. |
8 | -I | It will display all the IP address associated with the server or machine. |
9 | -s | It will help to display the short hostname of the server or machine. |
10 | -V | It will print the hostname command or utility version. |
11 | -y | IT will print the network information service (NIS) domain name. |
12 | -h | It will give the basic information of hostname command. What is the command and how we need to use it in the Linux environment? |
Examples of hostname Command in Linux
Given below are the examples mentioned :
Example #1
hostname Command
It is very simple and common hostname command in a Linux environment. It will print the hostname in the default format.
Code:
hostname
Explanation:
- As per the above command, we are able to get the Linux server hostname. The server hostname is “linux_machine.server.com”.
Output:
Example #2
hostname Command – With “-A” Option
We are able to get all the full qualified domain name address of the hostname. To get the same, we need to use the “-A” option with the hostname command.
Code:
hostname -A
Explanation:
- As per the above command, we are using the “-A” option. It will help to get all the full qualified domain name address of the current host. As per the below command, it is displaying the single FQDN hostname.
Output:
Example #3
hostname Command – With “-d” Option
In hostname command, we are having the functionality to print the server domain name. We need to use the “-d” option with hostname command.
Code:
hostname -d
Explanation:
- In some cases, we need to print only the domain name. Hence we need to use the “-d” option with the hostname command. So we will get only the domain name. Here the domain name is “server.com”
Output:
Example #4
hostname Command – With “-f” Option
In hostname command, we are able to get the hostname as well as the domain in the fully qualified domain name address. To get it, we need to use the “-f” option with hostname command.
Code:
hostname -f
Explanation:
- As per the above hostname command, we are able to get the information on hostname and domain.
Output:
Example #5
hostname Command – With “-i” Option
In hostname command, we are having the functionality to get the mapping IP address of the hostname. For that, we need to use the “-i” option with hostname command.
Code:
hostname -i
Explanation:
- In hostname command, we are able to get the IP address of the hostname. It is another way to get the IP address of the server. Here, the IP address of the server is “192.168.1.103”.
Output:
Example #6
hostname command – With “-s” Option
In hostname command, we are having the functionality get only the shorter host.
Code:
hostname -s
Explanation:
- As per the above command, we are able to get only host information i.e. the “linux_machine”. It will not display the domain information.
Output:
Example #7
hostname Command – With “-V” Option
In the hostname command, we are having the functionality to print the version information.
Code:
hostname -V
Explanation:
- In the hostname command, we are able to get the version information. It is always good to go with the new or latest version. As per the below output screenshot, we are using the “3.13” version.
Output:
Example #8
hostname Command – With “-h” Option
We are able to get the basic help about hostname command. We need to use the “-h”option with the hostname command.
Code:
hostname -h
Explanation:
- As per the below screenshot, we are getting the basic help about hostname command.
Output:
Conclusion
We have seen the uncut concept of “Linux hostname command” with the proper example, explanation, and command with different outputs. The need to set the proper hostname of the server. It is very important in terms of web servers or applications. We need to keep a unique hostname in a single network.
Recommended Articles
We hope that this EDUCBA information on “hostname Command in Linux” was beneficial to you. You can view EDUCBA’s recommended articles for more information.