Using host
Overview
host(1) is a useful utility for performing DNS lookup.
Usage
Forward DNS lookup
Suppose you want to find the IP address of example.com:
$ host example.com example.com has address 192.168.1.1 example.com has IPv6 address 2001:db8:: example.com mail is handled by 10 example.com.
host(1) will not only provide the name's IPv4 address, but it will also gives you the IPv6 address and mail servers for a name.
Reverse DNS (rDNS) Lookup
If you know an IP address, but don't know the name associated with it, you can perform rDNS lookup:
$ host 192.168.1.1 1.1.168.192.in-addr.arpa domain name pointer example.com.
Notice that when you perform reverse DNS lookup, the suffix in-addr.arpa is attached. Also, the four numbers end up reversed: the last number shows up first, and the first number shows up last. This happens because in an IP address, the least significant number is last; whereas in DNS, the least significant subdomain shows up first.
(Remember, a subdomain is a name that is grouped underneath another
name. For example, if the domain name is example.com
, one subdomain
might be mail.example.com
; another subdomain might be
www.example.com
)
$ host 2001:db8:: 0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa domain name pointer example.com.
The same happens for IPv6; the last digit shows up first, and the first digit shows up last.
Query types
host(1) can show a specific query
type with the -t
option. Query types include NS (NameServer
records), MX (Mail eXchange records), and TXT (TeXT records).
For example, to show all the nameservers of example.com, type:
$ host -t ns example.com example.com name server ns1.example.com. example.com name server ns2.example.com.
To show the mail servers, type:
$ host -t mx example.com example.com mail is handled by 10 mail.example.com.
To show the TXT records, type:
$ host -t txt example.com example.com descriptive text "v=spf1 a mx ip4:192.168.1.1 -all"
Here, we see the TXT record is used to provide SPF information for mail servers.
To check the version of the nameserver ns1.example.com
:
$ host -t txt -c chaos VERSION.BIND ns1.example.com
Troubleshooting
Using host(1), you can test if your DNS records have been set properly. If you recently added a DNS record, use host(1) on that name to see if it resolves properly.
For example, suppose you added a new TXT record for example.com
.
Rather than rely on the default nameservers from
resolv.conf (which may be holding stale DNS records),
you can explicitly query the nameserver ns1.example.com
by
providing it to host:
$ host -t txt example.com ns1.example.com example.com descriptive text "v=spf1 -all"
If your records have changed, make sure to flush your old DNS cache
before running host
. How to do this depends on how you have
configured resolv.conf, whether you have setup
resolvd, unwind, or
unbound.