Skip to content

The dig command

The dig command asks a DNS server what a name points to…

The dig command asks a DNS server what a name points to: dig example.com prints the IP address with the details, and dig +short example.com just the address.

Taught in Advanced, lesson 46; more in Ninja

A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.

Usage

dig [@SERVER] NAME [TYPE]
dig +short NAME
dig -x ADDRESS

Ask a DNS server about a name. Asks the DNS server (from /etc/resolv.conf, or @SERVER) what it knows about NAME: its address (A), mail servers (MX), aliases (CNAME) and more. status: NXDOMAIN means the name doesn’t exist. dig asks the server directly, so it doesn’t see /etc/hosts; ping and ssh do. When dig and ping disagree, check /etc/hosts. The DNS server here is the router, 10.0.0.1. In this practice shell the network is a simulation inside your browser: the machines web, db and router and the website example.com are pretend, and nothing leaves your device.

In the practice terminal:A simulation: web, db, router and example.com are pretend machines inside your browser, and nothing leaves your device. The DNS server is the pretend router, 10.0.0.1.

How to use dig

Just the address: dig +short

An alias (a CNAME record) prints the name it points to, then the address.

dig +short example.com
203.0.113.80
dig +short www.example.com
example.com.
203.0.113.80

Only the answer: dig +noall +answer

The answer lines say the name, how many seconds it may be cached, the record type and the value. Plain dig adds the question, the server that answered and the time it took.

dig +noall +answer example.com
example.com.            300	IN	A	203.0.113.80

Other record types: MX, TXT, NS

Put the type after the name. MX names a domain’s mail servers.

dig +short example.com MX
10 mail.example.com.

Ask one server: dig @server

dig @10.0.0.1 +short example.com
203.0.113.80

Find the name for an address: dig -x

dig -x 10.0.0.10 +short
web.lan.

Why dig can’t find a name that ping can

ping also reads /etc/hosts, while dig asks only DNS. Here web is in /etc/hosts, and DNS knows it as web.lan.

grep web /etc/hosts
10.0.0.10	web
dig +short web.lan
10.0.0.10

Options

+shortjust the answer
@SERVERask this server
TYPEA, MX, NS, TXT, CNAME or ANY
-x ADDRESSreverse lookup: the name for an address

More examples

dig example.com
The full answer
; <<>> DiG 9.18.28-0ubuntu0.24.04.1-Ubuntu <<>> example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4383
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;example.com.           	IN	A

… and 8 more lines
dig -x 10.0.0.10
The name for an address
; <<>> DiG 9.18.28-0ubuntu0.24.04.1-Ubuntu <<>> -x 10.0.0.10
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4383
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;10.0.0.10.in-addr.arpa.	IN	PTR

… and 8 more lines

Questions about dig

What is the difference between dig and nslookup?

Both ask DNS. dig shows the answer the way DNS returns it and is easy to use in scripts with +short; nslookup is older and also on Windows.

What does NXDOMAIN mean?

The name doesn’t exist: the DNS server checked and found no such domain. Check the spelling, or whether the name is only in /etc/hosts.

How do I install dig?

On Ubuntu and Debian it comes with the bind9-dnsutils package (sudo apt install bind9-dnsutils); on Fedora, with bind-utils.

Learn it step by step

Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.