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.
How to install the Dig command?
Let’s make sure that the dig utility is installed on your machine.
$ whereis dig 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 /usr/bin/dig
Dig command Examples
- 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 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 512 ;; QUESTION SECTION: ;justgeek.io. IN A ;; ANSWER SECTION: justgeek.io. 300 IN A 184.108.40.206 ;; Query time: 14 msec ;; SERVER: 220.127.116.11#53(18.104.22.168) ;; 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 22.214.171.124
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 jerome.ns.cloudflare.com. kia.ns.cloudflare.com.
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 126.96.36.199 188.8.131.52.vultrusercontent.com.
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 @184.108.40.206 jerome.ns.cloudflare.com. kia.ns.cloudflare.com.
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
$ dig -f search.txt ns1.yahoo.com. ns4.yahoo.com. ns2.yahoo.com. ns3.yahoo.com. ns5.yahoo.com. ns1-204.azure-dns.com. ns2-204.azure-dns.net. ns3-204.azure-dns.org. ns4-204.azure-dns.info. dns1.p09.nsone.net. dns2.p09.nsone.net. dns3.p09.nsone.net. dns4.p09.nsone.net. ns4.google.com. ns2.google.com. ns1.google.com. ns3.google.com.
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.