How to Use DNS Utility Programs in Linux

how to use DNS utility programs in Linux

When something goes wrong with name resolution — a website won’t load, an email server can’t be reached, or an internal hostname isn’t working — the first thing any experienced Linux administrator reaches for is a DNS utility program. These small command-line tools let you query DNS servers directly, inspect exactly what they return, and pinpoint precisely where a problem lies.

This article covers the essential DNS utilities available on Linux: dig, nslookup, host, whois, and a few supporting tools, along with practical examples for each.


Why Learn DNS Utilities?

Applications like web browsers hide the DNS resolution process from you — you just see “page not found” without knowing whether the problem was DNS, routing, or the destination server itself. DNS utility tools let you separate these possibilities by testing name resolution independently of the application.

flowchart TD
    A[Problem: Website Won't Load] --> B{Is it DNS?}
    B -->|Test with dig/nslookup| C[DNS resolves correctly]
    B -->|Test with dig/nslookup| D[DNS fails or wrong IP]
    C --> E[Problem is likely routing/firewall/server]
    D --> F[Problem is DNS configuration]

Installing the Tools

Most DNS utilities come bundled in a package. On Debian/Ubuntu:

sudo apt install dnsutils -y

On RHEL/CentOS/Rocky:

sudo dnf install bind-utils -y

This installs dig, nslookup, and host.


dig — Domain Information Groper

dig is the most powerful and detailed DNS query tool available on Linux. It’s the go-to tool for professionals.

Basic Usage

dig www.example.com

Sample output (trimmed):

;; ANSWER SECTION:
www.example.com.    3600  IN  A  93.184.216.34

;; Query time: 24 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)

Querying a Specific DNS Server

dig @8.8.8.8 www.example.com

Querying Specific Record Types

dig www.example.com MX
dig www.example.com NS
dig www.example.com TXT
dig www.example.com AAAA

Short Output (Just the Answer)

dig +short www.example.com

Reverse DNS Lookup (IP to Name)

dig -x 93.184.216.34

Tracing the Full Resolution Path

dig +trace www.example.com

This shows every step of the DNS hierarchy — from root servers, to TLD servers, to the authoritative server — which is invaluable for debugging delegation issues.


nslookup — The Classic Query Tool

nslookup is older than dig and slightly less detailed, but still widely used and available on nearly every operating system (including Windows), making it useful when you need a cross-platform-familiar command.

Basic Usage

nslookup www.example.com

Querying a Specific Server

nslookup www.example.com 8.8.8.8

Querying a Specific Record Type

nslookup -type=MX example.com
nslookup -type=NS example.com

Interactive Mode

nslookup
> server 8.8.8.8
> set type=TXT
> example.com
> exit

host — Simple and Script-Friendly

host gives clean, easily parsed output — great for shell scripts.

host www.example.com

Output:

www.example.com has address 93.184.216.34

Specific Record Types

host -t MX example.com
host -t NS example.com
host -t TXT example.com

Reverse Lookup

host 93.184.216.34

Verbose Mode

host -v www.example.com

Comparison Table: dig vs nslookup vs host

Featuredignslookuphost
Detail levelVery highMediumLow (concise)
Script-friendly outputYes (+short)NoYes
Cross-platform (Windows too)NoYesNo
Trace full DNS pathYes (+trace)NoNo
Interactive modeNoYesNo
Recommended forProfessionals, deep debuggingQuick familiar checksScripting, quick answers

whois — Domain Registration Lookup

While not strictly a DNS resolution tool, whois is essential for DNS-related troubleshooting — it tells you who owns a domain, when it expires, and which nameservers are officially delegated to it.

sudo apt install whois -y
whois example.com

Sample fields you’ll see:

Domain Name: EXAMPLE.COM
Registrar: ...
Name Server: A.IANA-SERVERS.NET
Name Server: B.IANA-SERVERS.NET
Expiration Date: ...

This is especially useful when troubleshooting “the domain suddenly stopped resolving” issues — sometimes the answer is simply that the domain registration expired!


dnsmasq --test and resolvectl

If you’re running systemd-resolved, resolvectl doubles as a diagnostic tool:

resolvectl query www.example.com
resolvectl status
resolvectl statistics

resolvectl statistics shows cache hits/misses — useful for verifying local caching behavior.


Real-World Troubleshooting Walkthrough

Scenario: A user reports that internal-app.corp.local isn’t resolving on their workstation, but works fine for everyone else.

Step 1 — Check basic resolution:

dig internal-app.corp.local

Result: NXDOMAIN (name doesn’t exist, according to whichever server answered).

Step 2 — Check which DNS server is being used:

cat /etc/resolv.conf

Result: The workstation is pointing to a public resolver (8.8.8.8) instead of the internal DNS server — a public resolver has never heard of corp.local.

Step 3 — Confirm using the correct internal server directly:

dig @10.0.0.5 internal-app.corp.local

Result: Returns the correct internal IP — confirming the internal DNS server is fine, and the workstation’s misconfigured resolver settings were the actual root cause.

This is a textbook example of how using these utilities in sequence isolates a problem quickly rather than guessing.


Cisco Example: DNS Diagnostics on IOS

Cisco devices offer built-in DNS diagnostics as well:

ping www.example.com
telnet 8.8.8.8 53
show hosts

show hosts displays the router’s local DNS cache — similar in spirit to checking a Linux resolver’s cache state.


Python Example: Building a Simple DNS Diagnostic Script

import dns.resolver
import dns.reversename

def check_domain(domain):
    resolver = dns.resolver.Resolver()
    try:
        answer = resolver.resolve(domain, 'A')
        print(f"{domain} resolves to:")
        for rdata in answer:
            print(f"  {rdata.address}")
    except dns.resolver.NXDOMAIN:
        print(f"{domain} does not exist (NXDOMAIN)")
    except dns.resolver.NoAnswer:
        print(f"{domain} has no A record")
    except dns.exception.Timeout:
        print(f"Query for {domain} timed out")

check_domain("www.example.com")

This kind of script is handy for monitoring dashboards that periodically verify critical hostnames are resolving correctly.


Best Practices

  • Use dig +short in scripts — it’s the cleanest, most reliable machine-readable output.
  • Always test against multiple DNS servers (@8.8.8.8, @1.1.1.1, and your internal server) to isolate whether a problem is server-specific.
  • Use +trace when debugging delegation issues between registrars, TLDs, and authoritative servers.
  • Don’t forget whois — many “DNS problems” are actually expired domain registrations.
  • Combine tools — dig for deep detail, host for quick scripting, nslookup when working across mixed OS environments.
  • Check TTLs in dig output before assuming a DNS change has propagated — old cached records may still be valid.

Troubleshooting Reference Table

Tool OutputMeaningNext Step
NXDOMAINDomain/record does not existVerify spelling, check zone file, check registration with whois
SERVFAILDNS server had an internal errorTry a different DNS server; check server logs
REFUSEDServer refused the query (ACL)Check allow-query settings on the DNS server
connection timed outServer unreachableCheck firewall rules, port 53 (UDP/TCP)
Empty answer sectionNo record of that type existsQuery a different record type (e.g., AAAA vs A)

Summary

DNS utility programs are the essential toolkit for any Linux user working with networks. dig gives deep, detailed insight for professionals; nslookup offers familiar cross-platform functionality; host is perfect for clean scripting output; and whois fills in the registration-level picture these tools can’t see. Mastering all four means you can diagnose almost any DNS-related problem quickly and confidently.


Further Reading

Total
2
Shares

Leave a Reply

Previous Post
how to configure BIND in Linux

How to Configure BIND in Linux

Next Post
how to configure the DNS resolver in Linux

How to Configure the DNS Resolver in Linux

Related Posts