dig
command is a very useful DNS lookup utility. It can be used to retrieve DNS records information of any domain name by querying specifc DNS servers. It is also a great troubleshooting tool for any admin configuring or troubleshooting existing DNS server.
To retrieve domain MX records simply use MX
option in combination to domain name you wish to query. For example the below command will query MX records for google.com:
dig google.com MX ; <<>> DiG 9.9.4-RedHat-9.9.4-18.el7_1.5 <<>> google.com MX ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27957 ;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 4, ADDITIONAL: 5 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;google.com. IN MX ;; ANSWER SECTION: google.com. 600 IN MX 50 alt4.aspmx.l.google.com. google.com. 600 IN MX 10 aspmx.l.google.com. google.com. 600 IN MX 20 alt1.aspmx.l.google.com. google.com. 600 IN MX 30 alt2.aspmx.l.google.com. google.com. 600 IN MX 40 alt3.aspmx.l.google.com. ;; AUTHORITY SECTION: google.com. 91534 IN NS ns4.google.com. google.com. 91534 IN NS ns3.google.com. google.com. 91534 IN NS ns1.google.com. google.com. 91534 IN NS ns2.google.com. ;; ADDITIONAL SECTION: ns1.google.com. 91534 IN A 216.239.32.10 ns2.google.com. 91534 IN A 216.239.34.10 ns3.google.com. 91534 IN A 216.239.36.10 ns4.google.com. 91534 IN A 216.239.38.10 ;; Query time: 312 msec ;; SERVER: 105.21.113.131#53(107.21.213.181) ;; WHEN: Thu Oct 29 15:14:55 AEDT 2015 ;; MSG SIZE rcvd: 283
The above command will however list all the other available records such as A
, NS
etc. Use the +short
option to retrieve only mail exchange (MX) records:
$ dig google.com MX +short 10 aspmx.l.google.com. 20 alt1.aspmx.l.google.com. 30 alt2.aspmx.l.google.com. 40 alt3.aspmx.l.google.com. 50 alt4.aspmx.l.google.com.
If you need to troubleshoot your own DNS server locally while the domain's name server is not yet set, you can point dig
to any local or remote DNS server you wish to query by using @HOST/IP
syntax. For example the next dig command will query localhost DNS for google.com MX records:
$ dig @localhost google.com MX +short 10 aspmx.l.google.com. 40 alt3.aspmx.l.google.com. 30 alt2.aspmx.l.google.com. 50 alt4.aspmx.l.google.com. 20 alt1.aspmx.l.google.com.
alternatively we can use DNS server's IP address:
$ dig @8.8.8.8 google.com MX +short 10 aspmx.l.google.com. 40 alt3.aspmx.l.google.com. 30 alt2.aspmx.l.google.com. 50 alt4.aspmx.l.google.com. 20 alt1.aspmx.l.google.com.