Networking

DNS Lookup: How to Use nslookup and dig (Windows, macOS, Linux)

A DNS lookup asks a DNS server which records exist for a name. On Windows, run nslookup example.com; on macOS and Linux, run dig example.com; both print the IP address the name resolves to.

Replace example.com with the name you want to check in every command below. The output shown is example output with made-up addresses from the documentation range, so yours will differ.

Quick DNS lookup with nslookup

nslookup is built into Windows and is also available on many macOS and Linux systems. Microsoft's documentation says that if you look up a single piece of data, or use nslookup in scripts, command lines or PowerShell, you should use the noninteractive mode. In that mode the first parameter is the name or IP address to look up, and the second is the DNS server to ask.

nslookup example.com

Example output:

Server:  dns.example.net
Address:  192.0.2.53

Non-authoritative answer:
Name:    example.com
Addresses:  2001:db8::10
          203.0.113.10

The first two lines show which DNS server answered. That matters: it is the server your computer is configured to use, not necessarily the one that owns the domain. "Non-authoritative answer" means the server replied from its own cache or from another server rather than being the source of the records for that domain.

Look up a specific record type

By default you get address records. To ask for something else, use the -type option:

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

Microsoft's example for -type=AAAA returns only IPv6 records. You can combine options in one command and pick a server at the end, for example nslookup -debug -type=A+AAAA -nosearch -recurse example.com 1.1.1.1, which turns on debug output, asks for both IPv4 and IPv6 addresses, skips the search domain and queries 1.1.1.1. For what each record type means, see DNS record types explained.

Ask a specific DNS server

Put the server last. This sends the query to Cloudflare's resolver instead of your default:

nslookup example.com 1.1.1.1

Comparing answers from two servers is the quickest way to tell whether a problem is with your configured resolver or with the domain itself. If your default server fails and 1.1.1.1 works, the fault is in your resolver or the path to it. Public options are compared in public DNS servers compared.

Interactive mode

Type nslookup alone, or use a hyphen as the first parameter (nslookup - 1.1.1.1), and the prompt changes to >. Then you can type several lookups in a row and change settings between them:

nslookup
> set type=MX
> example.com
> server 4.4.4.4
> example.com
> set all
> exit

set type= changes the record type, server changes the default DNS server, set all prints the current settings, and exit returns you to the normal prompt. A built-in command that you want to treat as a computer name needs a backslash in front of it, and any unrecognized command is treated as a name to look up.

DNS lookup with dig on macOS and Linux

The dig manual describes the typical form as dig @server name type. All three parts are optional except the name:

dig example.com
dig example.com MX
dig @1.1.1.1 example.com AAAA

If you give no type, dig looks up an A record. If you give no @server, it uses the servers listed in /etc/resolv.conf. Unlike nslookup, dig prints the whole response with a header, the question, the answer and some statistics, which is useful when you need to see the status of the reply.

For a short answer, add +short:

dig +short example.com

Example output:

203.0.113.10

Other options from the manual page that are worth knowing:

  • -t type sets the query type explicitly, the same as putting the type after the name.
  • -p port sends queries to a port other than the standard DNS port 53.
  • +trace traces the delegation path from the root name servers for the name. Tracing is off by default.
  • -x address does a reverse lookup, mapping an address to a name. See reverse DNS lookup for how that works.

DNS lookup in PowerShell

PowerShell has its own cmdlet, Resolve-DnsName. Microsoft describes it as functionally similar to nslookup. It returns objects, so you can filter the result:

Resolve-DnsName example.com
Resolve-DnsName example.com -Type MX
Resolve-DnsName example.com -Server 1.1.1.1
Resolve-DnsName example.com -DnsOnly

Two details come from the cmdlet's documentation. If you leave out -Server, it queries the DNS servers configured on the interface. If you leave out -Type, it queries both A and AAAA records. -DnsOnly restricts the query to the DNS protocol, so LLMNR and NetBIOS queries are not sent. That is useful when you want to know what DNS says rather than what Windows can find by other means.

What the nslookup errors mean

When a lookup fails, nslookup prints one of the messages below. These descriptions are from Microsoft's documentation:

MessageWhat it means
timed outThe server did not respond after a certain time and a certain number of retries.
No response from serverNo DNS name server is running on the server computer.
No recordsThe name is valid, but the server has no records of the type you asked for.
Nonexistent domainThe computer or DNS domain name does not exist.
Server failureThe server found an internal inconsistency in its database and could not return a valid answer.
RefusedThe server refused to service the request.

"No records" and "Nonexistent domain" are easy to confuse. The first means the name exists but has nothing of that type, for example asking for MX on a name that only has an A record. The second means the name itself is not there. If you see the browser error DNS_PROBE_FINISHED_NXDOMAIN instead, it is the same situation seen from the browser's side.

If the answer looks wrong

A lookup shows what the server you asked believes right now. If you just changed a record and still see the old value, the server may be returning a cached copy until the record's time to live runs out. Ask a different server, such as nslookup example.com 1.1.1.1, and compare. Also note that the lookup tools above ask a DNS server directly, while your browser may use its own resolver settings. If a lookup is right but a site still fails on your computer, clear the local cache using how to flush the DNS cache. For the full path a query takes from your computer to the authoritative server, read how DNS works.

Frequently asked questions

What is the difference between nslookup and dig?
Both send DNS queries and print the answer. nslookup ships with Windows and has an interactive mode. dig is the usual tool on macOS and Linux and prints the full response, including the answer section and query details. On Windows, the PowerShell cmdlet Resolve-DnsName does a similar job.
How do I do a DNS lookup for a specific record type?
With nslookup, add -type=, for example nslookup -type=MX example.com. With dig, put the type after the name, for example dig example.com MX. With PowerShell, use Resolve-DnsName example.com -Type MX.
Why does nslookup say Non-existent domain?
The DNS server answered that the name does not exist. Check the spelling first, then query a different server to see whether the answer differs. A name that was only just created can also be missing from a server that cached the earlier negative answer.
Does a DNS lookup change anything on my computer or the domain?
No. A lookup only asks a DNS server a question and prints the reply. It does not modify the domain's records or your network settings.

Sources