Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 60

DNS Lookups with `nslookup` and `dig`

Recommended resources

Introduction

Hello! In our last lesson, we traced the entire journey of a DNS query, from your computer to a recursive resolver, and then through the iterative steps of querying root, TLD, and authoritative servers. We saw how dig +trace can peel back the curtain and show us this process in action.

Today, we're moving from tracing the path to interrogating the destination. Our goal is to master the command-line tools nslookup and dig to perform and interpret various DNS lookups. These are fundamental tools for any developer or system administrator. You'll learn how to ask the DNS not just "What is the IP address?" but also "Who handles the email for this domain?" or "Is this name just an alias for another?".

This lesson will give you the practical, hands-on skills to diagnose DNS issues, verify domain configurations, and gain a much deeper understanding of the data that underpins web and email services.


Understanding What to Ask For: Core DNS Record Types

Before you can effectively query DNS, you need to know the "questions" you can ask. These questions correspond to different DNS record types. In the last lesson, we primarily dealt with A (address) and NS (name server) records. Let's formalize our understanding of the most common types.

The Ultimate Guide to the nslookup Command

The article 'The Ultimate Guide to the nslookup Command' provides a fantastic, clear breakdown of the core DNS record types. Understanding these is essential before we start querying them.

Please read the section titled 'Understanding the Core DNS Record Types' and review the 'Quick Reference: DNS Record Types' table. Focus on the purpose of each record: A, AAAA, CNAME, MX, NS, and TXT.

To summarize, you'll most frequently encounter:

  • A Record: Maps a domain name to an IPv4 address.
  • AAAA Record: Maps a domain name to an IPv6 address.
  • CNAME Record: Creates an alias, mapping one domain name to another (the "canonical name").
  • MX Record: Specifies the mail servers responsible for a domain.
  • NS Record: Identifies the authoritative name servers for a domain.
  • TXT Record: Holds text information, often used for verification or email security policies.
  • PTR Record: The reverse of an A record; maps an IP address back to a domain name.

nslookup: The Universal DNS Tool

nslookup (Name Server Lookup) is your first tool of choice because it's available on virtually all operating systems (Windows, macOS, Linux) by default. It's perfect for quick checks and is easy to guide others through for remote troubleshooting.

nslookup can be used in two ways:

  1. Non-Interactive Mode: For single, quick lookups.
  2. Interactive Mode: For more complex troubleshooting where you might run multiple queries.

The Ultimate Guide to the nslookup Command

Let's dive into the practical usage of nslookup. The same article we just used has excellent, example-driven sections on both modes.

First, read 'Using nslookup: A Practical Guide (Non-Interactive Mode)'. Try running the example commands for A, NS, MX, and PTR records on your own terminal. Then, read 'Advanced Troubleshooting with Interactive Mode' to understand how to change query types and target specific DNS servers, which is a powerful debugging technique.

Hands-on with nslookup

Let's try a few commands. Open your terminal or command prompt.

1. Find the IP address for google.com:

nslookup google.com

You'll see the DNS server your computer used for the query (likely your router or ISP's resolver) and then the "Non-authoritative answer," which is the IP address for google.com. "Non-authoritative" simply means the answer came from a resolver's cache, not directly from Google's own name server, which is normal and fast.

2. Find the mail servers for google.com:

nslookup -type=mx google.com

The output shows a list of mail servers with priority numbers. A lower number indicates a higher priority.

3. Find the name of an IP address (Reverse Lookup):

nslookup 8.8.8.8

This performs a reverse lookup using a PTR record, telling you that 8.8.8.8 is associated with dns.google.


dig: The Detailed DNS Tool

While nslookup is universal, dig (Domain Information Groper) is the preferred tool for many network professionals, especially on Linux and macOS. It provides much more detailed output and has more powerful options. You already saw a glimpse of it with dig +trace.

A basic dig query returns a verbose output broken into sections.

Using `dig` to find a domain's IP address
The output of `dig archlinux.org`. The most important part for a simple lookup is the `ANSWER SECTION`, which shows the domain, the record's Time-To-Live (TTL), the record type (`A`), and the IP address.

Let's learn how to use dig and interpret its structured output.

Dig Command in Linux (DNS Lookup)

The article 'Dig Command in Linux (DNS Lookup)' is a comprehensive guide that will walk you through interpreting dig's output and using its most common options.

Please read the following sections: 'Understanding the dig Output': This is crucial. It breaks down each section of the output (QUESTION, ANSWER, AUTHORITY, etc.). 'Print Only the Answer': Learn how to use the extremely useful +short and +noall +answer options. 'Query Record Types': See examples of how to query for A, MX, NS, CNAME, and other records. 'Reverse DNS Lookup': See how to do a reverse lookup with the -x option. Finally, review the 'Quick Reference' table at the end for a great summary of common commands.

Hands-on with dig

Let's replicate our nslookup queries using dig. If you're on Windows, you may need to install it first (it's part of the BIND tools).

1. Find the IP address for google.com (with clean output):

dig google.com +noall +answer

This is much cleaner than the default output. For an even quicker check, ideal for scripting:

dig google.com +short

2. Find the mail servers for google.com:

dig google.com MX +short

Notice how you specify the record type (MX) after the domain name.

3. Find the name of an IP address (Reverse Lookup):

dig -x 8.8.8.8 +short

The -x flag tells dig to perform a reverse lookup.

4. Querying a specific DNS server:
Just like with nslookup, you can tell dig which server to ask. This is useful for checking if a DNS change has propagated.




# Ask Google's public DNS server
dig @8.8.8.8 github.com +short




# Ask Cloudflare's public DNS server
dig @1.1.1.1 github.com +short

Your Turn: A Practical Investigation

Let's put this into practice. Imagine you are a developer tasked with verifying the DNS configuration for wikipedia.org. Use either dig or nslookup to answer the following questions.

  1. What is the IPv4 address for wikipedia.org?
    • dig wikipedia.org A +short or nslookup wikipedia.org
  2. What is the IPv6 address for wikipedia.org?
    • dig wikipedia.org AAAA +short or nslookup -type=aaaa wikipedia.org
  3. Who are the authoritative name servers for wikipedia.org?
    • dig wikipedia.org NS +short or nslookup -type=ns wikipedia.org
  4. Does www.wikipedia.org point to an IP directly, or is it an alias?
    • dig www.wikipedia.org +short
    • Interpretation: If the result is another domain name, it's a CNAME (alias). If it's an IP address, it's an A record. You'll see that www.wikipedia.org is an alias for dyna.wikimedia.org. You can then dig dyna.wikimedia.org +short to find the ultimate IP address.

By running these queries, you've performed a basic but essential DNS audit, a task you'll encounter frequently.


Conclusion

In this lesson, we moved from theory to practice, equipping you with the skills to actively query the Domain Name System. You are no longer just a passive user of DNS; you can now inspect its records and diagnose its behavior.

Key Takeaways:

  • nslookup is the universally available tool for DNS queries, perfect for quick checks on any system.
  • dig is the more powerful, detailed tool preferred by network professionals, offering structured output and advanced options.
  • You can query for many different record types (A, AAAA, MX, NS, TXT, CNAME, PTR) to get a complete picture of a domain's configuration.
  • The ability to query specific DNS servers (e.g., dig @8.8.8.8 ...) is a critical skill for troubleshooting DNS propagation issues.
  • Using options like +short in dig allows for clean, script-friendly output.

Preview of the Next Lesson:

Now that you can reliably translate a domain name into an IP address, what's next? In the following lesson, "Describe the client-server model in the context of the web," we will explore how your browser (the client) uses that IP address to establish a connection with a web server and request a webpage using the HTTP protocol. This is the very next step in our journey to understand how loading a website works.