1. Overview

We sometimes have a need in Linux to perform DNS (Domain Name System) lookup operations. To achieve this, we can use the host command.

In essence, this command locates the IP address of a specific domain name. Additionally, it determines the domain name corresponding to a given IP address. The host command utilizes various query types such as NS (name server), SOA (start of authority), TXT (text), MX (mail exchange), and more. These query types aid in discovering hostnames or IP addresses.

In this tutorial, we’ll explore common options of the command. In addition, we’ll go into its basic examples and more advanced example utilization.

2. Common host Command Options

The syntax of the host command is simple:

host [OPTION] [HOSTNAME]

Let’s see the meanings of each of the components of the host command:

  • [OPTION]: represents the various command-line options of the host command
  • [FILE]: represents the name(s) of the file(s) to be processed

Furthermore, let’s explore various options associated with the host command:

Options

Description

name

Specifies the domain name to be looked up. It can also be an IPv4 address or an IPv6 address, in which case the host performs a reverse lookup by default

-a

This is equivalent to setting the -v option and instructing the host to make a query of type ANY

-C

Displays the SOA records for the specified zone name from all the authoritative name servers listed for that zone, when used

-c

Instructs the host to make a DNS query of the specified class. The default class is IN (Internet)

-d or -v

Generates verbose output

-l

Selects list mode, making the host perform a zone transfer for the specified zone name

-n

Specifies that reverse lookups of IPv6 addresses should use the IP6.INT domain and “nibble” labels as defined in RFC1886

-N

Sets the number of dots required in a name for it to be considered absolute

-R

Changes the number of UDP retries for a lookup

-r

Makes non-recursive queries, clearing the RD bit in the query

-T

Makes the host use a TCP connection when querying the name server

-t

Selects the query type

-W

Specifies the time to wait for a reply, in seconds

-w

Makes the host wait indefinitely for a reply

With these various options, we can tailor the behavior of the host command to suit specific needs.

3. Common host Command Examples

Let’s dive into some practical examples of using the host command.

3.1. Displaying the IP Address of a Host Machine

We can use the host command to display the IP address of a host machine. For example, let’s check the IP address of the hostname google.com:

$ host google.com
google.com has an address 142.250.200.78
google.com has IPv6 address 2a00:1450:4003:80d::200e
google.com mail is handled by 10 smtp.google.com.

The output shows the IP address (142.250.200.78) of the hostname google.com.

3.2. Displaying the Hostname of an IP Address

In addition, we can also use the command to find the hostname that corresponds to an IP address. For example, let’s check the hostname corresponding to the IP address 2001:4860:4860::8888:

$ host 2001:4860:4860::8888
8.8.8.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.6.8.4.0.6.8.4.1.0.0.2.ip6.arpa domain name pointer dns.google.

The command displays the hostname of the IP address.

3.3. Querying the DNS Server

We can also utilize the command to query a DNS for NS records. For instance, let’s query the DNS server for the NS records for google.com:

$ host -t ns google.com
google.com name server ns3.google.com.
google.com name server ns2.google.com.
google.com name server ns1.google.com.
google.com name server ns4.google.com.

The output shows the NS records associated with the hostname google.com.

We use the host command with the -t option to select the query type of the hostname. For example, let’s retrieve the SOA records for the hostname google.com:

$ host -t SOA google.com 
google.com has SOA record ns1.google.com. dns-admin.google.com. 629353541 900 900 1800 60

This displays the SOA record for the hostname google.com. In particular, the record includes the primary name server (ns1.google.com), the responsible party for the domain (dns-admin.google.com), and various timing parameters (refresh, retry, expire, and minimum TTL).

Similarly, we can use the host command with the -t option to query the DNS server for TXT (text) records associated with a hostname. TXT records are commonly used to store arbitrary textual information, such as SPF (Sender Policy Framework) records for email authentication or domain ownership verification.

For example, let’s retrieve the TXT records for the hostname google.com:

$ host -t TXT google.com
google.com descriptive text "globalsign-smime-dv=CDYX+XFHUw2wml6/Gb8+59BsH31KzUr6c1l2BPvqKX8="
google.com descriptive text "MS=E4A68B9AB2BB9670BCE15412F62916164C0B20BB"
google.com descriptive text "onetrust-domain-verification=de01ed21f2fa4d8781cbc3ffb89cf4ef"
google.com descriptive text "docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e"
...

The output displays any TXT records found, providing valuable textual information associated with the domain.

3.4. Showing Detailed Information of the Hostname

The host command, with the -a option, can provide detailed information beyond querying specific DNS records. In particular, this information includes the hostname, its IP address, domain name, and associated DNS records. This can be particularly useful for troubleshooting network connectivity issues or verifying domain configurations.

For example, let’s use the host command to display detailed information about the hostname google.com:

$ host -a google.com
...
;; ANSWER SECTION:
google.com.             300     IN      A       192.178.54.14
google.com.             300     IN      AAAA    2c0f:fb50:4002:812::200e
google.com.             300     IN      MX      10 smtp.google.com.
google.com.             21600   IN      HTTPS   1 . alpn="h2,h3"
...

This command helps us retrieve and display various information about the hostname google.com.

3.5. Finding the Hostname to an IP Address With a Timeout

We can use the host command with the -W option to find the hostname to an IP address with a timeout. For example, let’s attempt to resolve the hostname for www.google.com with a timeout of 5 seconds:

$ host -W 5 www.google.com
www.google.com has address 216.58.223.228
www.google.com has IPv6 address 2c0f:fb50:4003:802::2004

The command has found the IP address of the Hostname with a timeout of 5 seconds.

3.6. Finding Domain CNAME Record

The host command can find the Canonical Name (CNAME) record of a domain. CNAME records create aliases for domain names, enabling multiple domain names to resolve to the same IP address.

For example, let’s use the host command to find the CNAME record for mail.yahoo.com:

$ host -t cname mail.yahoo.com
mail.yahoo.com is an alias for edge.gycpi.b.yahoodns.net.

The output reveals that mail.yahoo.com is an alias for edge.gycpi.b.yahoodns.net. In particular, it indicates that when accessing mail.yahoo.com, the DNS resolution redirects to the canonical name edge.gycpi.b.yahoodns.net.

4. Conclusion

In this article, we’ve explored the versatility of the host command in Linux systems for DNS lookup operations.

Additionally, the host command proves indispensable for network diagnostics and domain management by retrieving IP addresses and querying DNS servers for specific records.