The host command in Linux is an easy-to-use command. This command is used to perform DNS lookups. The host command in Linux can be used to find the hostnames or IP addresses. In this article, you will learn the ins and outs of the host command in Linux.
A. Description
In Linux, the host command finds different kinds of information from the Domain Name System(DNS). The main purpose of the host command is to determine the hostnames to IP addresses and vice-versa. There are so many query types like NS(name server), SOA(start of authority), TXT(text), MX(mail exchange), and so on. These query types are used with the host command to find the hostnames or IP addresses.
B. Syntax
The syntax of the host command is simple in Linux. The syntax is as follows:
host [OPTION] [HOSTNAME]
OR,
host [OPTION] [IP ADDRESS]
C. Options
A few options are available for the host command in Linux. I have listed some of them here.
Even so, you can explore the man page to know more about the host command.
man host
You can also run the command below to view its information.
host
Useful Options
- -a, perform a query of all available information of the host.
- -t, finds hostname using different query types.
- -W, finds the hostname within a timeout.
Practical Examples of the “host” Command in Linux
The host command in Linux is a pretty simple command. Here you will discover some useful examples of the host command in Linux.
Example 1: Display the IP Address of a Host Machine
The host [Host_name]
is used to find the IP address associated with the hostname. Here I am using google.com as my hostname. To do the same you can follow the steps below:
Steps to Follow >
➊ Initially open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
host google.com
➌ Now, press the ENTER key.
Output >
In the image below, you can see the IP address of the hostname google.com.
Similar Readings
- The “nslookup” Command in Linux [12 Practical Examples]
- The “ufw” Command in Linux [12+ Practical Examples]
- The “ssh” Command in Linux [18 Practical Examples]
- The “traceroute” Command in Linux [13 Practical Examples]
Example 2: Display the Host name of an IP Address
The host command in Linux can find the hostname that corresponds to the IP address you provided. Here I am using the 2001:4860:4860::8888 as the IP address. To do this you can follow the process down below:
Steps to Follow >
➊ To start open the Terminal in Ubuntu.
➋ Copy the following command in the command prompt:
host 2001:4860:4860::8888
➌ After that, tap the ENTER key.
Output >
In the following image, you can see the hostname of the IP address 2001:4860:4860::8888.
Example 3: Shows Detailed Information of the Hostname
You can see the detailed information of the hostname by using the host -a [Host_name]
command. The -a
flag retrieved all the available information of the host. Here I want to use the hostname google.com. You can also do this by following the procedure:
Steps to Follow >
➊ First open the Ubuntu Terminal.
➋ Run the following command in the command prompt:
host -a google.com
➌ Then, hit the ENTER button.
Output >
In the image below, the command has shown all the information of the hostname from the DNS server.
Example 4: Query the DNS Server For NS (name server) Records
To specify the query type for a hostname, you can use the host command along with option-t in Linux. In this example, I will show you how to retrieve the name server records to find the DNS servers associated with the hostname google.com. You can also do this by following the instructions below:
Steps to Follow >
➊ At first launch the Ubuntu Terminal.
➋ Write the following command in the command prompt:
host -t ns google.com
➌ Finally, press the ENTER button.
Output >
In the following image, the names of the DNS servers have been shown that are associated with the hostname google.com.
Example 5: Query the DNS Server For SOA (start of authority) Records
The host command along with option -t can be used to select the query type of hostname in Linux. In this example, the command will retrieve the SOA records for the hostname google.com. You can also do the same by following the below steps:
Steps to Follow >
➊ At first launch the Terminal in Ubuntu.
➋ Type the following command in the command prompt:
host -t SOA google.com
➌ Finally, strike the ENTER button.
Output >
In the following image, you can see the command has shown the SOA records of the host google.com.
Similar Readings
- The “ dig” Command in Linux [10 Practical Examples]
- The “hostname” Command in Linux [12 Practical Examples]
- The “ip” Command in Linux [9+ Practical Examples]
- The “ifconfig” Command in Linux [6+ Practical Examples]
Example 6: Query the DNS Server For TXT (text) Records
You can use the host command along with option -t to select the query type of hostname in Linux. This command host -t TXT [host_name] will display the TXT record associated with the domain google.com. To do the same you can follow the steps below:
Steps to Follow >
➊ To initiate launch the Ubuntu Terminal.
➋ Type the following command in the command prompt:
host -t txt google.com
➌ After that, hit the ENTER key.
Output >
In the below image, you can see the output of the command has shown the TXT record which is found for the domain “google.com”.
Example 7: Find the Hostname to an IP Address With a Timeout
You can find the hostname to an IP address with timeout using the host command with option -W. Here I am setting the timeout of 2 seconds to resolve the hostname www.google.com. To do this do follow the process below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Copy the following command in the command prompt:
host -W 2 www.google.com
➌ Now, hit the ENTER button.
Output >
In the following image, the command has found the IP address of the Hostname with a timeout of 2 seconds using the host command in Linux.
Example 8: Query the DNS Server for MX (mail exchange) Records
If you want to find the mail servers of a domain then you can use the host command with option -t and MX followed by the hostname. To do the same you can follow the given instructions:
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Write the following command in the command prompt:
host -t MX gmail.com
➌ Finally, tap the ENTER button.
Output >
In the below image, you can see the command has retrieved the MX records that specify the mail servers which are responsible for email messages.
Example 9: Check the Hostname of a Server
The hostname command allows you to check the hostname of a server in Linux. To do this follow the procedure below:
Steps to Follow >
➊ Launch the Terminal in Ubuntu.
➋ Run the following command in the command prompt:
hostname
➌ Now, tap the ENTER key.
Output >
In the following image, the output displays the hostname which is Ubuntu in Linux.
Example 10: Check the IP Address of a Server Using the “host” Command in Linux
The hostname command with option -i helps you to check the IP address of a server in Linux. To do this do follow the instructions below:
Steps to Follow >
➊ Initially launch the Terminal in Ubuntu .
➋ Copy the following command in the command prompt:
hostname -i
➌ Now, press the ENTER key.
Output >
In the image below, the output displays the IP address which is 192.168.206.130 in Linux.
Conclusion
As shown in this article, the host command has some uses in Linux. You’ve also got to know the syntax, some functional options, and the practical applications of this command. To become a master in Linux, learn the command and its practical applications thoroughly.
Similar Readings
- The “rsync” Command in Linux [10 Practical Examples]
- The “screen” Command in Linux [13 Practical Examples]
- The “arp” Command in Linux [7 Practical Examples]
- The “ftp” Command in Linux [14 Practical Examples]
- The “scp” Command in Linux [4 Practical Examples]
- The “sftp” Command in Linux [6 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners