1. Introduction

We sometimes need to test the speed of DNS resolutions. There are several Linux tools available that can assist us in this matter.

Notably, the speed of a DNS server can affect the time it takes for a query to be resolved. On the other hand, the time also depends on other factors such as the distance between the client and server, the network conditions, and the complexity of the query. To sum up, measuring the response time might not always show the exact performance of a DNS server, but is usually a good estimate especially when it comes to the client’s needs.

In this tutorial, we’ll explore several tools and one Bash script to check the response time.

2. Using dig

dig is a utility to perform DNS lookup, i.e., query name servers. It’s part of the bind-utils package which contains other DNS utilities as well.

Firstly, let’s install bind-utils via yum:

$ yum install bind-utils

Now, we can pass a domain name to the dig command, so we can get information about the response time:

$ dig yahoo.com
; <<>> DiG 9.18.7 <<>> yahoo.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44430
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
yahoo.com. 36 IN A 74.6.143.26
yahoo.com. 36 IN A 98.137.11.164
yahoo.com. 36 IN A 74.6.143.25
yahoo.com. 36 IN A 74.6.231.21
yahoo.com. 36 IN A 74.6.231.20
yahoo.com. 36 IN A 98.137.11.163

;; Query time: 167 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Thu Mar 09 04:55:51 +0330 2023
;; MSG SIZE rcvd: 134

We can see plenty of information in the result such as the version of the dig command, used UDP port, DNS records, and others.

In this case, we check the query time. It shows the time (167 msec) that took for a response.

If we want to just see the query time in our result, we can use the grep command:

$ dig yahoo.com | grep time
;; Query time: 1 msec

We can also use a specific DNS server instead of the default one:

$ dig yahoo.com @8.8.8.8 | grep time
;; Query time: 860 msec

Moreover, if we want to check the speed of DNS resolutions continuously, we can use a while loop:

$ while true; do dig yahoo.com @8.8.8.8 | grep time; sleep 1; done
;; Query time: 143 msec
;; Query time: 171 msec
;; Query time: 98 msec

In this case, we’re checking the speed of a DNS query every second.

3. Using namebench

namebench is a tool to find the fastest DNS server relative to the current client. In fact, namebench tests the name servers that we’re currently using, plus some popular global DNS services. Moreover, the result suggests some additional name servers for us.

To get namebench, we first install snap. After that, we can install namebench via snap:

$ sudo snap install namebench

Now, we can run namebench:

$ namebench
namebench 1.3.1 - best source (automatic) on 2023-03-31 12:48:16.469198
threads=40/2 queries=250 runs=1 timeout=3.5 health_timeout=3.75 servers=11
------------------------------------------------------------------------------
...
Fastest individual response (in milliseconds):
----------------------------------------------
OpenDNS-2 ################################################ 17.92502
SYS-10.211.55.1 ################################################# 18.25213
UltraDNS #################################################### 19.34910
DynGuide #################################################### 19.48118
Google Public DN ##################################################### 19.87100
Mean response (in milliseconds):
--------------------------------
OpenDNS-2 ######## 181.90
Google Public DN ######### 193.20
UltraDNS ########## 217.37
SYS-10.211.55.1 ########## 239.29
DynGuide ##################################################### 1277.51
Recommended configuration (fastest + nearest):
----------------------------------------------
nameserver 208.67.222.222 # OpenDNS-2
nameserver 195.186.1.109 # BlueWin-3a CH
nameserver 195.186.4.111 # Bluewin-4 CH
********************************************************************************
In this test, OpenDNS-2 is 31.6%: Faster
********************************************************************************
- Saving report to /tmp/namebench_2023-03-31_1258.html
- Saving detailed results to /tmp/namebench_2023-03-31_1258.csv

In the output, firstly, we see summarized information such as the number of queries and tested servers. Secondly, we see the comparison of the response speed of the available servers. Thirdly, we can see the list of recommended servers with their names and IP addresses. Finally, the fastest server is selected. In this test, OpenDNS-2 is the fastest.

Moreover, we can get the full report as an HTML file. In addition, all results are saved in a CSV file.

If desired, we can test a specific name server:

$ namebench 8.8.8.8 1.1.1.1

The above command tests 8.8.8.8 and 1.1.1.1 as DNS servers.

4. Using dnsperf

dnsperf is a DNS server performance testing tool. We can use it for our needs.

First, let’s install dnsperf:

$ yum install dnsperf

Now, we prepare our input data file:

$ cat myData.txt
yahoo.com A
google.com A

In this query file, each line has a domain name and a record type, separated by whitespace.

At this point, we can run a query:

dnsperf -s 1.1.1.1 -d myData.txt -c 20 -l 15 -Q 100
DNS Performance Testing Tool
Version 2.11.0

[Status] Command line: dnsperf -s 1.1.1.1 -d myQuery -c 20 -l 15 -Q 100
[Status] Sending queries (to 1.1.1.1:53)
[Status] Started at: Fri Mar 31 15:17:53 2023
[Status] Stopping after 15.000000 seconds
...
[Timeout] Query timed out: msg id 772
[Status] Testing complete (time limit)

Statistics:

  Queries sent:         1500
  Queries completed:    1499 (99.93%)
  Queries lost:         1 (0.07%)

  Response codes:       NOERROR 1499 (100.00%)
  Average packet size:  request 27, response 83
  Run time (s):         15.080047
  Queries per second:   99.402873

  Average Latency (s):  0.113799 (min 0.015540, max 0.746716)
  Latency StdDev (s):   0.104904

In the result, we can see information about the sent, completed, and lost queries, average time, and others.

We’ve used several options:

  • s – the name or address of the server to send the request to, defaulting to the loopback address 127.0.0.1
  • d – read records from a data file, myData.txt
  • c – number of clients, 20
  • l – duration of time for the test, 15 seconds
  • Q – number of requests per second, 100

Notably, the clients are just requests that are sent from multiple sockets.

5. Using dnsdiag

dnsdiag is a set of tools to do a test on DNS servers to make sure they are working as we expect.

We’ll install it via pip3:

$ pip3 install dnsdiag

Actually, dnsdiag contains three tools:

  • dnsping
  • dnstraceroute
  • dnseval

In this case, we’ll use dnseval. First, let’s prepare a server list file:

$ cat serverList.txt
1.1.1.1
8.8.8.8
8.8.4.4
4.2.2.3
4.2.2.4

As we can see, each line contains an IP address of a DNS server.

At this time, we can run our command:

$ dnseval -t A -f serverList.txt -c10 yahoo.com
server      avg(ms)     min(ms)     max(ms)     stddev(ms)  lost(%)  ttl        flags                  response
---------------------------------------------------------------------------------------------------------------
1.1.1.1     111.909     96.946      119.949     8.139       %0       274        QR -- -- RD RA -- --   NOERROR             
8.8.8.8     62.194      57.070      70.913      4.754       %0       569        QR -- -- RD RA -- --   NOERROR             
8.8.4.4     70.644      53.689      98.957      13.136      %0       250        QR -- -- RD RA -- --   NOERROR             
4.2.2.3     124.611     110.018     135.135     9.470       %0       1          QR -- -- RD RA -- --   NOERROR             
4.2.2.4     124.178     112.808     149.100     10.883      %0       489        QR -- -- RD RA -- --   NOERROR

As can be seen, we compared using one command the response times of five different DNS servers. On average, 8.8.8.8 seems the fastest.

Let’s break down the command:

  • t – the type of DNS query (A, AAA, CNAME, NX, MX)
  • f – server list file, serverList.txt
  • c – number of queries, 10

If there’s no error, we’ll see the NOERROR message in the response column. On the other hand, we see No Response if a server isn’t responding.

6. Using Bash Script

Of course, we can also use a Bash script, but we still need the dig command:

#!/bin/bash
# Get domain name from the first argument
DOMAIN=$1
# Print table header
echo "IP address | Response time"
echo "---------- | -------------"
# Loop through IP addresses and run dig command
for IP in "${@:2}"
do
  # Run dig command and extract response time using awk
  result=$(dig $DOMAIN @$IP | awk '/time/ {print $4 " ms"}')
  # Print IP address and result 
  printf "%-10s | %s\n" "$IP" "$result"
done

The first argument is the domain to test. The rest of the arguments are IP addresses for DNS servers. We use the dig command in a loop to get the response time for each IP.

Here’s a sample result of this script:

./script.sh yahoo.com 1.1.1.1 8.8.8.8 4.2.2.4
IP address | Response time
---------- | -------------
1.1.1.1    | 127 ms
8.8.8.8    | 149 ms
4.2.2.4    | 158 ms

In this case, we used yahoo.com for the domain. Moreover, we used three different DNS IP addresses for checking the speed. 1.1.1.1 seems to be the fastest on average in this test.

7. Conclusion

In this article, we’ve looked at different tools for testing the speed of DNS resolutions.

To sum up, depending on whether we want to check one server or multiple servers, need specific details, need to compare at the same time, or need results in a file, we can choose different ways accordingly.