dig Command in Linux: Query DNS, A, MX, NS Records and More
The dig command on Linux (domain information groper) is a powerful and flexible tool for querying DNS name resolution servers. It is commonly used to diagnose DNS server configuration issues.
To install dig
$ apt-get install dnsutils
On CentOS 7:
$ yum install bind-utils
After installing, check the version to ensure the configuration was completed successfully:
$ dig -v
DIG Syntax
In its simplest form, the dig syntax will be:
dig [server] [name] [type]
[server] – the IP address or hostname of the DNS server to query. If the server argument is not supplied, dig will use the address listed in /etc/resolv.conf.
[name] – the name of the resource record to look up. It can be a host or a domain.
[type] – the type of query requested by dig. For example: A, MX, or SOA record (see the DNS record list), etc. By default, dig queries for an A record.
dig usage examples
In this example, dig returns the IP address corresponding to the certificacaolinux.com.br website, as well as which DNS server was responsible (authority) for answering the query:
$ dig certificacaolinux.com.br;; QUESTION SECTION:;certificacaolinux.com.br. IN A;; ANSWER SECTION:certificacaolinux.com.br. 86400 IN A 192.168.1.5;; AUTHORITY SECTION:certificacaolinux.com.br. 86400 IN NS ns2.host.br.certificacaolinux.com.br. 86400 IN NS ns.host.br.
Get MX record of a domain
The dig command can also be used to query specific records on the nameserver. In the example below, it will return only the MX type records for the certificacaolinux.com.br domain:
$ dig MX certificacaolinux.com.br;; QUESTION SECTION:;certificacaolinux.com.br. IN MX;; ANSWER SECTION:certificacaolinux.com.br. 35 IN MX 3 alt2.aspmx.l.google.com.certificacaolinux.com.br. 35 IN MX 10 web.certificacaolinux.com.br.certificacaolinux.com.br. 35 IN MX 1 aspmx.l.google.com.certificacaolinux.com.br. 35 IN MX 2 alt1.aspmx.l.google.com.
Get maximum information about a domain with dig
The "any" option makes dig attempt to retrieve all information about a given domain:
$ dig example.com any
; <<>> DiG 9.6.1 <<>> example.com any
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4016
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;example.com. IN ANY
;; ANSWER SECTION:
example.com. 172719 IN NS a.iana-servers.net.
example.com. 172719 IN NS b.iana-servers.net.
example.com. 172719 IN A 208.77.188.166
example.com. 172719 IN SOA dns1.icann.org. hostmaster.icann.org. 2007051703 7200 3600 1209600 86400
;; Query time: 1 msec
;; SERVER: ::1#53(::1)
;; WHEN: Wed Aug 12 11:40:43 2009
;; MSG SIZE rcvd: 154
Specify the query server in dig
To specify the query server, you can also use the "@" option followed by a DNS server address:
$ dig wikimedia.org MX @ns0.wikimedia.org
; <<>> DiG 9.11.3 <<>> wikimedia.org MX @ns0.wikimedia.org
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39041
;; flags: qr aa rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1024
; COOKIE: c9735311d2d2fa6e3b334ab01b67960d (good)
;; QUESTION SECTION:
;wikimedia.org. IN MX
;; ANSWER SECTION:
wikimedia.org. 3600 IN MX 10 mx1001.wikimedia.org.
wikimedia.org. 3600 IN MX 50 mx2001.wikimedia.org.
;; Query time: 1 msec
;; SERVER: 208.80.154.238#53(208.80.154.238)
;; WHEN: Sat Sep 18 21:33:24 PDT 2021
;; MSG SIZE rcvd: 108
To get a summarized output
The "+noall +answer +multiline" option can be used to get a concise version suitable for a DNS zone configuration file:
$ dig +noall +answer +multiline wikimedia.org MX
wikimedia.org. 3600 IN MX 10 mx1001.wikimedia.org.
wikimedia.org. 3600 IN MX 50 mx2001.wikimedia.org.
To get only the address of a record:
The "+short" option nicely summarizes the dig command output:
$ dig godaddy.com +short208.109.192.70
You can specify the record type along with the +short option:
$ dig godaddy.com +short MX
0 godaddy-com.mail.protection.outlook.com.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just sign in or create your user here.
Liked it? Share it
That was just one piece. The course teaches the whole path — from your first command to exam day.