Dig Command Examples

Before we see the Dig command examples, Let’s see what exactly is dig command is.

So basically, the Dig command is used for DNS lookup in Unix-like systems. It is generally used by administrators to troubleshoot DNS and network problems.

Dig Command Examples

How to install the Dig command?

Let’s make sure that the dig utility is installed on your machine.

$ whereis dig

The above command clearly shows that Dig is not present on your machine, If you are on Centos 7, you can use the fire below command to install the dig.

$ sudo yum install bind-utils -y

For centos, 8 systems use the command below.

$ sudo dnf install bind-utils -y

Since the utility is installed, let’s proceed further. You may again check if it’s installed.

$ which dig

Dig command Examples

  1. Basic Dig Command

As you would see below, just passing the website name to the Dig command will show some basic information, like in the example below it’s showing an A record.

$ dig justgeek.io

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.9 <<>> justgeek.io
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24610
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

; EDNS: version: 0, flags:; udp: 512
;justgeek.io.                   IN      A

justgeek.io.            300     IN      A

;; Query time: 14 msec
;; WHEN: Wed Jun 22 13:16:18 EDT 2022
;; MSG SIZE  rcvd: 56

2. Checking A record of the website using the dig command

The above command does show A record of the domain, but it comes with a lot of information that we don’t need. So you can just check the A record of the domain using the command below.

$ dig justgeek.io +noall +answer

3. Checking MX record using Dig Command.

So, if want to just check the MX records of the server, you can use the command below. For those of you who don’t know, MX records are for the emails. It tells where email should be routed.

$ dig justgeek.io MX +noall +answer +short
30 mx3.zoho.in.
20 mx2.zoho.in.
10 mx.zoho.in.

4. Checking the nameserver of the domain.

Just like we checked the MX record of a domain, we can also check the nameservers of the domain.

$ dig justgeek.io NS +noall +answer +short

Reverse DNS lookups

Yes, you can also perform reverse DNS lookups using the Dig command. Let’s see some of the examples.

As you can see in the below example, you can see that the IP address is resolving to Vultr servers.

$ dig +short -x

Looking for records on specific DNS servers

Let’s say you want to see if your domain is resolving specifically on google DNS? You can mention using @ in the command.

$ dig justgeek.io NS +noall +answer +short @

Searching Multiple Domains

All the commands we saw above were for a single domain, what if we have too many domains and you want to run the Dig command on all of them? Let’s explore it with some examples.

First, let’s create a text file with the list of domains and what you want to look up.

Let’s create a file called search.txt and add the below content.

$ cat search.txt
+short yahoo.com NS
+short bing.com NS
+short google.com NS

As it’s self-explanatory, we are looking up the NS records on the domains mentioned. Now let’s run the dig command with the -f option.

$ dig -f search.txt

As you see in the command above it’s showing NS records for all the domains mentioned. You may be wondering what is the meaning of the options that we used in the commands shown in the example?

Let’s see a few of them

+short It will give you shortened output. It omits the information which is not required.
+noall this will print details information but specific
+answer this will print only the answer section

Setting Options by default

Is there a way to set all the options as default?

Yes, what you can do is create a file called .digrc in your home directory with all the options.

$ cat ~/.digrc
+short +noall +answer

Now, just run the short command as below.

$ dig justgeek.io MX
30 mx3.zoho.in.
20 mx2.zoho.in.
10 mx.zoho.in.

As you saw in the command above, we haven’t specified any options but it’s using them by default.

Some people also ask me, if I have provided vital information about my domain through the command above. But the answer is no, that information is already public.

Leave a Comment