CLI Tool — dig

· 1 min read · 314 Words · -Views -Comments

I saw someone mention the dig command in the Surge community, so I took some time to learn it.

Overview

From Wikipedia: dig is a network administration CLI tool for querying the Domain Name System (DNS). Easy to remember — “dig” means searching for domain info.

Usage

$ dig 1991421.cn @119.29.29.29

; <<>> DiG 9.10.6 <<>> 1991421.cn @119.29.29.29
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45826
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;1991421.cn.			IN	A

;; ANSWER SECTION:
1991421.cn.		600	IN	A	108.160.132.49

;; Query time: 91 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: Tue Jan 18 22:53:26 CST 2022
;; MSG SIZE  rcvd: 55

The above queries the DNS record for my blog domain.

  1. 119.29.29.29 is Tencent Cloud DNS; I use it because my domain was purchased in China.
  2. I explicitly specified the DNS server because on macOS with Surge in enhanced mode, the default DNS is handled by Surge, which can yield an “unreal” A record.

Which protocol does dig use?

UDP by default; you can force TCP by adding a parameter, e.g., dig 1991421.cn @119.29.29.29 tcp.

dig vs. ping

After learning dig, I found it can often replace ping — DNS info is detailed and you can choose the DNS server. So prefer dig over ping in many cases.

Installation

  1. macOS includes dig out of the box.
  2. On Linux, install via sudo yum install bind-utils (or your distro’s equivalent).

Final Thoughts

Mastering common CLI tools makes everyday troubleshooting easier. Besides dig, telnet and curl are also very useful.

References

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover