Contents

Useful examples of dig command

Contents

This recipe is about the dig command in Unix and some useful examples, which you can find interesting when scripting or when need to check your address from inside the machine that you are accessing by the private network.

dig is an acronym for Domain Information Groper and is a command-line utility that performs DNS lookup by querying name servers and displaying the result to you. It is part of the dnsutils package in Debian, make sure you have that installed.

The syntax is:

1
dig [@server] [-b address] [-c class] [-f filename] [-k filename] [-m] [-p port#] [-q name] [-t type] [-x addr] [-y [hmac:]name:key] [-4] [-6] [name] [type] [class] [queryopt...] 

Some examples:

To get you ip adress, using the server resolver1.opendns.com:

1
dig @resolver1.opendns.com ANY myip.opendns.com +short

Get the address for bigg.blog

1
dig bigg.blog A +noall +answer

Command to get a list of the mailservers for bigg.blog

1
dig bigg.blog MX +noall +answer

The following command is to get a list of authoritative DNS servers for bigg.blog

1
dig bigg.blog NS +noall +answer

To get a list of all the above (MX and NS) at once

1
dig bigg.blog ANY +noall +answer 

Trace the path taken to resolve the name

1
dig bigg.blog +trace

Reverse lookup

1
dig +answer -x 104.24.116.215

Query names from a file

1
dig -f domain_list.txt +short

To customise permanently the output, you need to create a file ~/.digrc like

1
echo "+noall +answer" > ~/.digrc

Now, when dig is executed it will show only answer section will output.

Hope it helps you
Have a great day

Buy me a Coffee
Hope you find this useful, if you have any question please visit my twitter @bigg_blog and if you have a couple of pounds buy me a coffee.
G