dig 명령어 사용법 – DNS 확인하기

dig란? 어떨 때 사용해야 할까?

예전에 host, nslookup 명령어만 써왔다면, 이제 dig 명령어로 host 및 DNS 관련 상태를 확인해 해봅시다. PC에서 혹은 서버간 접속이 원활한지 보기 위해 DNS에서 주소가 잘 변환되는지 등을 확인하고 싶을 때가 있죠? 이 때, dig를 쓰면 됩니다.

dig라는 유틸리티는 Domain Information Groper라는 이름의 약자입니다만, 작명에서 뭔가 깊이 판다는 dig이라는 어감이랑 잘 어울리는 의도가 엿보입니다.

dig 설치 방법

운영체제마다 설치 방법은 약간씩 다릅니다. 아래 예를 참고하세요.

  • Arch Linux
$ sudo paceman -S bind-tools
  • CentOS/ Fedora
$ sudo yum install bind-utils
  • Ubuntu
$ sudo apt install dnsutils

설치를 정상적으로 마쳤다면, dig 명령어를 실행해 봅니다.

$ dig -v

dig가 실행되면서, 설치한 dig 버전이 나타나야 정상입니다.

dig – man page

NAME
       dig - DNS lookup utility

SYNOPSIS
       dig [@server] [-b address] [-c class] [-f filename] [-k filename] [-m] [-p port#] [-q name] [-t type] [-v] [-x addr] [-y [hmac:]name:key] [-4] [-6] [name] [type]
           [class] [queryopt...]

       dig [-h]

       dig [global-queryopt...] [query...]

DESCRIPTION
       dig (domain information groper) is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the
       name server(s) that were queried. Most DNS administrators use dig to troubleshoot DNS problems because of its flexibility, ease of use and clarity of output.
       Other lookup tools tend to have less functionality than dig.

       Although dig is normally used with command-line arguments, it also has a batch mode of operation for reading lookup requests from a file. A brief summary of its
       command-line arguments and options is printed when the -h option is given. Unlike earlier versions, the BIND 9 implementation of dig allows multiple lookups to be
       issued from the command line.

       Unless it is told to query a specific name server, dig will try each of the servers listed in /etc/resolv.conf. If no usable server addresses are found, dig will
       send the query to the local host.

       When no command line arguments or options are given, dig will perform an NS query for "." (the root).

       It is possible to set per-user defaults for dig via ${HOME}/.digrc. This file is read and any options in it are applied before the command line arguments.

       The IN and CH class names overlap with the IN and CH top level domain names. Either use the -t and -c options to specify the type and class, use the -q the
       specify the domain name, or use "IN." and "CH." when looking up these top level domains.

dig에 대해 공식 설명은 역시 man page에 상세히 기술되어 있습니다. help 옵션이 있네요.

$ dig -h

명령어로 세부 설명을 확인합니다.

Usage:  dig [@global-server] [domain] [q-type] [q-class] {q-opt}
            {global-d-opt} host [@local-server] {local-d-opt}
            [ host [@local-server] {local-d-opt} [...]]
Where:  domain	  is in the Domain Name System
        q-class  is one of (in,hs,ch,...) [default: in]
        q-type   is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]
                 (Use ixfr=version for type ixfr)
        q-opt    is one of:
                 -4                  (use IPv4 query transport only)
                 -6                  (use IPv6 query transport only)
                 -b address[#port]   (bind to source address/port)
                 -c class            (specify query class)
                 -f filename         (batch mode)
                 -i                  (use IP6.INT for IPv6 reverse lookups)
                 -k keyfile          (specify tsig key file)
                 -m                  (enable memory usage debugging)
                 -p port             (specify port number)
                 -q name             (specify query name)
                 -t type             (specify query type)
                 -u                  (display times in usec instead of msec)
                 -x dot-notation     (shortcut for reverse lookups)
                 -y [hmac:]name:key  (specify named base64 tsig key)
        d-opt    is of the form +keyword[=value], where keyword is:
                 +[no]aaonly         (Set AA flag in query (+[no]aaflag))
                 +[no]additional     (Control display of additional section)
                 +[no]adflag         (Set AD flag in query (default on))
                 +[no]all            (Set or clear all display flags)
                 +[no]answer         (Control display of answer section)
                 +[no]authority      (Control display of authority section)
                 +[no]besteffort     (Try to parse even illegal messages)
                 +bufsize=###        (Set EDNS0 Max UDP packet size)
                 +[no]cdflag         (Set checking disabled flag in query)
                 +[no]cl             (Control display of class in records)
                 +[no]cmd            (Control display of command line)
                 +[no]comments       (Control display of comment lines)
                 +[no]crypto         (Control display of cryptographic fields in records)
                 +[no]defname        (Use search list (+[no]search))
                 +[no]dnssec         (Request DNSSEC records)
                 +domain=###         (Set default domainname)
                 +[no]edns[=###]     (Set EDNS version) [0]
                 +ednsflags=###      (Set EDNS flag bits)
                 +[no]ednsnegotiation (Set EDNS version negotiation)
                 +ednsopt=###[:value] (Send specified EDNS option)
                 +noednsopt          (Clear list of +ednsopt options)
                 +[no]expire         (Request time to expire)
                 +[no]fail           (Don't try next server on SERVFAIL)
                 +[no]identify       (ID responders in short answers)
                 +[no]idnout         (convert IDN response)
                 +[no]ignore         (Don't revert to TCP for TC responses.)
                 +[no]keepopen       (Keep the TCP socket open between queries)
                 +[no]multiline      (Print records in an expanded format)
                 +ndots=###          (Set search NDOTS value)
                 +[no]nsid           (Request Name Server ID)
                 +[no]nssearch       (Search all authoritative nameservers)
                 +[no]onesoa         (AXFR prints only one soa record)
                 +[no]opcode=###     (Set the opcode of the request)
                 +[no]qr             (Print question before sending)
                 +[no]question       (Control display of question section)
                 +[no]recurse        (Recursive mode)
                 +retry=###          (Set number of UDP retries) [2]
                 +[no]rrcomments     (Control display of per-record comments)
                 +[no]search         (Set whether to use searchlist)
                 +[no]short          (Display nothing except short
                                      form of answer)
                 +[no]showsearch     (Search with intermediate results)
                 +[no]split=##       (Split hex/base64 fields into chunks)
                 +[no]stats          (Control display of statistics)
                 +subnet=addr        (Set edns-client-subnet option)
                 +[no]tcp            (TCP mode (+[no]vc))
                 +time=###           (Set query timeout) [5]
                 +[no]trace          (Trace delegation down from root [+dnssec])
                 +tries=###          (Set number of UDP attempts) [3]
                 +[no]ttlid          (Control display of ttls in records)
                 +[no]vc             (TCP mode (+[no]tcp))
        global d-opts and servers (before host name) affect all queries.
        local d-opts and servers (after host name) affect only that lookup.
        -h                           (print help and exit)
        -v                           (print version and exit)

기본 형식 외에도 다양한 세부 컨트롤을 할 수 있음을 확인할 수 있습니다.

dig 기본 사용법

위의 긴 설명을 요약해서 꼭 필요한 옵션만 추려보자면 기본 형식은 아래와 같습니다.

$ dig [@server] [name] [type]
  • @server: 내가 도메인 관련 질의를 어디에 해야 하는지에 대한 옵션입니다. 반드시 @표시를 붙여줘야 합니다. 이 옵션은 생략할 수 있으며, 생략시에는 /etc/resolv.conf에 지정된 서버를 기본값으로 활용하여 질의합니다.
  • name: 어느 대상 (예: 도메인 네임)을 질의할 것인지에 대한 옵션입니다.
  • type: DNS 레코드의 여러 유형 중 하나를 구체적으로 지정할 수 있습니다. A, MX, SOA, CNAME 등의 값을 지정할 수 있으며, 아무 것도 지정하지 않는다면 기본값은 A 가 사용됩니다.

많이 사용하는 d-opt

help에서 나오는 d-opt 부분은 쿼리 결과를 걸러서 볼 때 주로 사용하는 옵션들입니다. 여러개의 옵션을 조합해서 사용할 수 있는데, 사람들이 주로 많이 사용하는 옵션들은 아래와 같습니다.

  • +[no]short
  • +[no]cmd
  • +[no]answer
  • +[no]all

등록된 레코드 조회하기 – ANY

$ dig naver.com ANY

; <<>> DiG 9.10.6 <<>> naver.com ANY
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48695
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;naver.com.			IN	ANY

;; ANSWER SECTION:
naver.com.		300	IN	SOA	ns1.naver.com. webmaster.naver.com. 2023010907 21600 1800 1209600 180
naver.com.		87	IN	MX	10 mx2.naver.com.
naver.com.		87	IN	MX	10 mx1.naver.com.
naver.com.		87	IN	MX	10 mx3.naver.com.
naver.com.		132126	IN	NS	ns1.naver.com.
naver.com.		132126	IN	NS	ns2.naver.com.

;; Query time: 5 msec
;; SERVER: 210.220.163.82#53(210.220.163.82)
;; WHEN: Mon Jan 09 23:24:09 KST 2023
;; MSG SIZE  rcvd: 180

DNS 서버에 등록된 레코드 종류들을 출력해 줍니다. SOA MX, NS 등의 정보들이 출력되는 것을 확인할 수 있습니다.

도메인 조회

$ dig naver.com

; <<>> DiG 9.10.6 <<>> naver.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6525
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
naver.com.		279	IN	A	223.130.195.95
naver.com.		279	IN	A	223.130.200.107
naver.com.		279	IN	A	223.130.200.104
naver.com.		279	IN	A	223.130.195.200

;; Query time: 148 msec
;; SERVER: 210.220.163.82#53(210.220.163.82)
;; WHEN: Mon Jan 09 23:17:00 KST 2023
;; MSG SIZE  rcvd: 102

기본적으로 /etc/resolv.conf에 지정된 DNS 서버에 질의하여 주소를 얻어옵니다.

별도의 옵션을 지정하지 않으면, 기본적으로 DNS A 레코드 값을 조회합니다.

요약형으로 조회하기: +short

$ dig naver.com +short

223.130.195.200
223.130.200.107
223.130.200.104
223.130.195.95

+short 옵션을 지정하면, 결과를 요약하여 출력해 줍니다.

응답 메시지만 걸러서 조회: +noall +answer

$ dig naver.com +noall +answer

; <<>> DiG 9.10.6 <<>> naver.com +noall +answer
;; global options: +cmd
naver.com.		45	IN	A	223.130.200.107
naver.com.		45	IN	A	223.130.195.95
naver.com.		45	IN	A	223.130.195.200
naver.com.		45	IN	A	223.130.200.104

특정 네임서버를 사용하여 도메인 조회

$ dig @8.8.8.8 naver.com

; <<>> DiG 9.10.6 <<>> @8.8.8.8 naver.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 47406
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
naver.com.		40	IN	A	223.130.200.104
naver.com.		40	IN	A	223.130.195.95
naver.com.		40	IN	A	223.130.195.200
naver.com.		40	IN	A	223.130.200.107

;; Query time: 83 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Jan 09 23:19:05 KST 2023
;; MSG SIZE  rcvd: 102

구글(8.8.8.8) DNS 서버를 사용하여 naver.com의 주소를 확인한 결과입니다.

특정 레코드형으로 조회하기: CNAME 조회하기

$ dig naver.com cname

; <<>> DiG 9.10.6 <<>> naver.com cname
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 3243
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;naver.com.			IN	CNAME

;; AUTHORITY SECTION:
naver.com.		180	IN	SOA	ns1.naver.com. webmaster.naver.com. 2023010907 21600 1800 1209600 180

;; Query time: 102 msec
;; SERVER: 210.220.163.82#53(210.220.163.82)
;; WHEN: Mon Jan 09 23:29:12 KST 2023
;; MSG SIZE  rcvd: 88

주소에 대한 CNAME 정보를 확인할 수 있습니다.

특정 레코드형으로 조회하기: NS 조회하기

$ dig naver.com ns

; <<>> DiG 9.10.6 <<>> naver.com ns
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29423
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;naver.com.			IN	NS

;; ANSWER SECTION:
naver.com.		61187	IN	NS	ns1.naver.com.
naver.com.		61187	IN	NS	ns2.naver.com.

;; Query time: 6 msec
;; SERVER: 210.220.163.82#53(210.220.163.82)
;; WHEN: Mon Jan 09 23:30:29 KST 2023
;; MSG SIZE  rcvd: 74


Leave a Reply