Using dig (DNS Lookup) on Linux
dig is the CLI you reach for when something's off with DNS — checking records, debugging resolvers, or just confirming what a domain points to. Here's a tour of the bits I actually use day-to-day.
Installing dig
First, check whether dig is installed and what version you're on:
dig -vSample output:
root@linuxpedi:~# dig -vDiG 9.16.1-UbuntuIf it's missing, install it for your distro:
Ubuntu / Debian:
apt install dnsutilsCentOS / Fedora:
yum install bind-utilsReading dig output
Once it's installed, the simplest invocation is just the domain:
dig linuxpedi.com; <<>> DiG 9.16.1-Ubuntu <<>> linuxpedi.com;; global options: +cmd;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2684;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1;; OPT PSEUDOSECTION:; EDNS: version: 0, flags:; udp: 65494;; QUESTION SECTION:;linuxpedi.com. IN A;; ANSWER SECTION:linuxpedi.com. 271 IN A 34.141.144.143;; Query time: 0 msec;; SERVER: 127.0.0.53#53(127.0.0.53);; WHEN: Fri Sep 03 16:12:32 +03 2021;; MSG SIZE rcvd: 58Walking through the output section by section:
- The first line shows the
digversion and the domain being queried. The second line shows the global options (justcmdby default).
; <<>> DiG 9.16.1-Ubuntu <<>> linuxpedi.com;; global options: +cmdIf you don't want this in the output, add +nocmd — it has to be the first option after dig.
- The next block is the technical metadata about the response. The header shows the opcode (what
digdid) and the status of that operation. In this case, the status isNOERROR, meaning the DNS query came back without issues.
;; Got answer:;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37159;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 2, ADDITIONAL: 5You can hide this with +nocomments.
- The QUESTION SECTION echoes the query you sent. By default
digasks for the A record.
;; QUESTION SECTION:;linuxpedi.com. IN A+noquestion turns this off.
- The ANSWER SECTION is the actual response. Since we asked for the A record by default, that's what we get back. (An A record maps a domain to an IPv4 address.)
;; ANSWER SECTION:linuxpedi.com. 271 IN A 34.141.144.143You almost never want to hide this, but +noanswer is there if you do.
- The last block is query stats:
;; Query time: 0 msec;; SERVER: 127.0.0.53#53(127.0.0.53);; WHEN: Fri Sep 03 16:12:32 +03 2021;; MSG SIZE rcvd: 58Useful flags
+short — just the IP, nothing else. Great for scripting:
root@linuxpedi:~# dig linuxpedi.com +short34.141.144.143+noall +answer — keep just the ANSWER section, drop the rest:
root@linuxpedi:~# dig linuxpedi.com +noall +answerlinuxpedi.com. 19 IN A 34.141.144.143@<server> — query a specific resolver instead of the system default. Handy for sanity-checking against Google DNS or Cloudflare:
dig linuxpedi.com @8.8.8.8Querying different record types
CNAME:
dig +nocmd mail.google.com cname +noall +answerroot@linuxpedi:~# dig +nocmd mail.google.com cname +noall +answermail.google.com. 21600 IN CNAME googlemail.l.google.com.TXT:
dig +nocmd linuxpedi.com txt +noall +answerroot@linuxpedi:~# dig +nocmd linuxpedi.com txt +noall +answerlinuxpedi.com. 14400 IN TXT "v=spf1 redirect=_spf.yandex.net"linuxpedi.com. 14400 IN TXT "yandex-verification: 58dc73d576a09a54"linuxpedi.com. 14400 IN TXT "google-site-verification=uuG-tMzOMkBPCl4iWB38PqrCYBOyqwUmbFte1dQ7M8g"MX (mail):
dig +nocmd linuxpedi.com mx +noall +answerroot@linuxpedi:~# dig +nocmd linuxpedi.com mx +noall +answerlinuxpedi.com. 14400 IN MX 10 mx.yandex.net.NS (nameserver):
dig +nocmd linuxpedi.com ns +noall +answerroot@linuxpedi:~# dig +nocmd linuxpedi.com ns +noall +answerlinuxpedi.com. 21600 IN NS ns1.dns-parking.com.linuxpedi.com. 21600 IN NS ns2.dns-parking.com.All records at once with any:
dig +nocmd sametkum.com any +noall +answerroot@linuxpedi:~# dig +nocmd sametkum.com any +noall +answersametkum.com. 14400 IN A 185.224.138.16sametkum.com. 21600 IN NS ns1.hostinger.web.tr.sametkum.com. 21600 IN NS ns2.hostinger.web.tr.sametkum.com. 21600 IN NS ns3.hostinger.web.tr.sametkum.com. 21600 IN NS ns4.hostinger.web.tr.sametkum.com. 21600 IN SOA ns1.hostinger.web.tr. dns.hostinger.com. 2019031200 28800 7200 604800 86400sametkum.com. 14400 IN MX 10 mx1.hostinger.web.tr.sametkum.com. 900 IN TXT "google-site-verification=PxYATQZ-vM5fVP02aQ8VYPIBvnPjpGYRT8TUbVxWVj8"sametkum.com. 900 IN TXT "v=spf1 include:_spf.mail.hostinger.com ~all"sametkum.com. 14400 IN AAAA 2a02:4780:8:263:0:1b50:5833:5sametkum.com. 14400 IN CAA 0 issue "comodoca.com"sametkum.com. 14400 IN CAA 0 issue "digicert.com"sametkum.com. 14400 IN CAA 0 issue "globalsign.com"sametkum.com. 14400 IN CAA 0 issue "letsencrypt.org"sametkum.com. 14400 IN CAA 0 issue "sectigo.com"sametkum.com. 14400 IN CAA 0 issuewild "comodoca.com"sametkum.com. 14400 IN CAA 0 issuewild "digicert.com"sametkum.com. 14400 IN CAA 0 issuewild "globalsign.com"sametkum.com. 14400 IN CAA 0 issuewild "letsencrypt.org"sametkum.com. 14400 IN CAA 0 issuewild "sectigo.com"