How to query DNS name servers – dig

close up photo of programming of codes
Reading Time: 3 minutes

Do you know that we can examine the DNS using our terminal . Lets talk about the same. dig – (Domain Information Groper) is used for such tasks. It is used to perform task like troubleshooting DNS problems. It also helps to gather DNS information and resolve the queried question from terminal.

How to use dig command.

Step 1 : dig command installation.

sudo apt update

sudo apt install dnsutils

rishivant@rishivant-Vostro-3590:~$ sudo apt install dnsutils
[sudo] password for rishivant: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  chromium-codecs-ffmpeg-extra gstreamer1.0-vaapi hplip-data libgstreamer-plugins-bad1.0-0 libimagequant0 libllvm11 libva-wayland2 mongodb-database-tools printer-driver-postscript-hp python3-olefile python3-pil
  python3-renderpm python3-reportlab python3-reportlab-accel
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  dnsutils
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Need to get 2,756 B of archives.
After this operation, 47.1 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu focal-updates/universe amd64 dnsutils all 1:9.16.1-0ubuntu2.9 [2,756 B]
Fetched 2,756 B in 0s (22.2 kB/s)   
Selecting previously unselected package dnsutils.
(Reading database ... 248786 files and directories currently installed.)
Preparing to unpack .../dnsutils_1%3a9.16.1-0ubuntu2.9_all.deb ...
Unpacking dnsutils (1:9.16.1-0ubuntu2.9) ...
Setting up dnsutils (1:9.16.1-0ubuntu2.9) ...

Step 2: Check for the version

rishivant@rishivant-Vostro-3590:~$ dig -v

DiG 9.16.1-Ubuntu

So the above two commands will install dig and will help you check the version.

The basic syntax of dig command is

dig @server name type  

Various use cases of dig command.

Now lets look at various uses of the above command. Now the below use cases will show up the day to day uses of command .These are the basic use cases now:-

1 – To display output of any dns.

rishivant@rishivant-Vostro-3590:~$ dig facebook.com 

; <<>> DiG 9.16.1-Ubuntu <<>> facebook.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20951
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;facebook.com.			IN	A

;; ANSWER SECTION:
facebook.com.		243	IN	A	157.240.239.35

;; Query time: 3 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Fri Dec 31 11:53:16 IST 2021
;; MSG SIZE  rcvd: 57

2 – How to trace DNS Path

dig facebook.com +trace

Use the above command to get the trace of particular website.

output will be as shown below.

3 – How to query multiple domain names

  • First make a file which will contain all the domain names.
  • Use the below command to perform

The above image shows the output of querying multiple doamins.

4- To view DNS Reverse Look-up.

To view DNS reverse look up use the below command

dig -x 209.132.183.81 +short

output 
rishivant@rishivant-Vostro-3590:~$ dig -x 209.132.183.81 +short

www.redhat.com.

5 – To find out TTL (Time to live ) value of DNS records

The time to live limits the lifetime of dns records in DNS.

rishivant@rishivant-Vostro-3590:~$ dig +nocmd +noall +answer +ttlid A www.knoldus.com

www.knoldus.com.	600	IN	CNAME	ghs.googlehosted.com.
ghs.googlehosted.com.	225	IN	A	142.250.192.19

So the above command lets us speed up dns name resolution.

Conclusion

Now you should be familiar with the above dig command .Further you can use this command with its other flag to find more about it.This command will be really helpful whenever you are stuck with DNS resolutions. This will provide you with a terminal approach of fixing issues of DNS.

For more information about the command you can always use the below command.

dig -h

Reference

https://www.javatpoint.com/linux-dig

Written by 

Rishivant is a enthusiastic devops learner at Knoldus. He believes in going 1% up everyday and showcases his learning in his work.