A brief tutorial on the Dig command

A brief tutorial on the Dig command

Hello everyone, I am Xianyu.

I don’t know how often you use the dig command in your daily study or work.

dig is the abbreviation of Domain Information Groper. It is a very common and useful tool for network administrators and friends working in the field of Domain Name System (DNS).

Whether it's a simple DNS resolution lookup or more advanced troubleshooting and analysis, dig can do it.

So today, Xianyu will share with you some common uses and examples of the dig command.

In addition to the dig command, there is another command nslookup that has similar functions to dig.

Install

If your Linux distribution does not have the dig command installed, you can install it using the following command.

Debian and Debian-based distributions:

Red Hat and RHEL-based distributions:

Arch Linux and Arch-based distributions:

Take my test environment as an example (CentOS 7.9):

Common usage

  • @server: The host name or IP address of the DNS server to which the request is directed. If no DNS server is specified, the DNS server currently configured on the system is used.
  • name: The domain name to search
  • type: The type of DNS record to retrieve, the default is A record type

For example, if I want to resolve the domain name www.baidu.com, I would specify to use Google's DNS server (8.8.8.8):

To find your MX records:

As we mentioned earlier, the dig command will display the A record in the ANSWER section by default. Let's take a look at the command output:

You can see that there are five columns in total. Let's take the last row as an example:

  • The first column www.wshifen.com: the domain name to be searched
  • Second column 192: Lifetime, tells the DNS resolver how long to cache the query before requesting a new one
  • The third column IN: the type of query, IN refers to the Internet
  • Column 4 A: DNS query type (A, AAAA, CNAME, MX, NS, PTR, CERT, SRV, TXT, SOA, etc.)
  • Column 5 104.193.88.77: IP address associated with the domain name

As we can see from the previous output, there is quite a lot of content. At this time, some friends may wonder: How to reduce the content output by the dig command? I only want the most useful ones, and I am dizzy when reading the rest.

You can add the +nocomments option:

As you can see, some comments are removed from the returned content after adding the +nocomments option.

At this time, some friends will say: No, no, there is still too much information, I only want the ANSWER part. How can I make the command output only the ANSWER part?

As you can see, adding the +noall and +answer options results in a clean and easy-to-read display.

What if I only want to return the parsed result? I can use the +short option:

Advanced Usage

(1) Specify return type information

As we mentioned earlier, there are many types of DNS records, such as A, AAAA, CNAME, MX, NS, PTR, CERT, SRV, TXT, SOA, etc.

We can add the corresponding type name at the end of the command to return the parsed specific type information:

Or use the special keyword ALL to put all type records together:

(2) View the DNS resolution process

If we are not sure where the problem is in the DNS configuration, we can use the dig command to view each process of DNS resolution by adding the +trace option.

(3) DNS reverse lookup

DNS forward lookup is to find the resolved IP through the domain name; reverse lookup is to find the domain name through the IP address:

(4) Batch query

Some friends may think: It is too slow to query one domain name at a time. Can I query multiple domain names at a time?

Yes, dig supports batch query.

First, we put the domain names to be queried in a file, one per line:

Then add the -f option when executing the dig command:

Modify dig default options

When we used the dig command earlier, in order to simplify the output information, we had to include the +nocomments option each time we typed the command. So can we set this option to the default value so that we don't need to add this option when typing the command?

The answer is yes, we need to create a ~/.digrc file in the home directory and add the options:

In this way, even if we don't add them when we execute the dig command, these two options will be used by default.

<<:  Where does the strength of 5G factories that “take over” 5G+Industrial Internet come from?

>>:  Essential for operation and maintenance: 20 common service ports and their corresponding service information

Recommend

What are virtual networks and why they are here to stay

The computer networks we typically imagine involv...

The battle between local deployment and cloud-managed WLAN architecture

Enterprises that need to upgrade their traditiona...

TCP SYN Queue and Accept Queue

First we must understand that a TCP socket in the...

Learn about routers, switches, and network hardware

Today we're taking a look at home network har...

C++ Programming Practice: IP Hash Load Balancing Algorithm

Today we are going to learn about NGINX. Nginx is...

Huawei's Meng Wanzhou: 5.5G is the inevitable path for 5G network evolution

On June 28, 2023 MWC Shanghai opened, and Huawei ...

How many optical modules does a GPU need?

1. Network card model There are mainly two types ...

IoT Observation: Seven benefits of LoRaWAN technology application in one article

IoT connection environment In addition to smart h...