Using host

(redirected from Openbsd.Host)

Overview

host is a useful utility for performing DNS lookup.

Examples

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.

Not only will it provide the IPv4 address, it also gives you the IPv6 address and mail servers for a domain.

Reverse DNS (rDNS) Lookup

If you know the IPv4/IPv6 address but don't know the domain 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 four numbers get reversed in the in-addr.arpa domain -- 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 domain shows up first.

$ 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 ircnow.org.

The same happens for IPv6; the last digit shows up first, and the first digit shows up last.

Query types

host can show a specific query type with the -t option. Query types include NS, MX, and TXT.

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.

Troubleshooting

Using host, you can test if your DNS records have been set properly. If you just added a DNS record, use host on that name to see if it resolves properly.

If your records have changed, make sure to flush your old DNS cache before running host. By default, host uses the nameservers you put in /etc/resolv.conf. For example:

nameserver 8.8.8.8
lookup file bind

resolv.conf tells your server to ask 8.8.8.8 (Google) for domain name lookup.

It's recommended to run your own local caching DNS server with unbound instead of using Google or the nameserver provided by your ISP. By running a local caching DNS server, you have more freedom to pick nameservers, which can help prevent censorship and privacy issues.

If you decide to use unbound, you will want to resolv.conf to point to localhost:

nameserver 127.0.0.1
lookup file bind

Then, if you have updated your DNS records, you can simply restart the nameserver to flush the cache of DNS records:

$ doas rcctl restart unbound

Then, run host again.