Preface
The command dig is a tool for querying DNS nameservers for information about host addresses, mail exchanges, nameservers, and related information. This tool can be used from any Linux (Unix) or Macintosh OS X operating system. The most typical use of dig is to simply query a single host.
The dig command fundamentally used for the following purposes:
The basic syntax:
- dig Hostname
- dig DomaiNameHere
- dig @DNS-server-name Hostname
- dig @DNS-server-name IPAddress
- dig @DNS-server-name Hostname|IPAddress type
List of DNS record types
Before you use the dig command you should aware of common types of resource records of the DNS. Here are most common resource records:
By default, dig command looks for A records only.
dig command examples
To find the IP address of the host www.cyberciti.biz, type:
Sample outputs:
- ; <<>> DiG 9.9.4-RedHat-9.9.4-38.el7_3.2 <<>> www.cyberciti.biz
- ;; global options: +cmd
- ;; Got answer:
- ;; ->>HEADER<<- class="number" id:="" nbsp="" noerror="" opcode:="" query="" span="" status:="" style="background-color: inherit; border: none; color: #c00000; margin: 0px; padding: 0px;">40839->
Understanding dig command output
The above is simple query for a single host called www.cyberciti.biz. The dig command displays lots of information. Let us try to understand the default dig command output:
More Usage Examples
Here we will teach you how to use dig command through examples.
How do I query a different nameserver such as 8.8.8.8?
You can use @8.8.8.8 as below to query specific nameserver:
Sample output:
- ; <<>> DiG 9.9.4-RedHat-9.9.4-38.el7_3.2 <<>> @8.8.8.8 www.kimo.com.tw
- ; (1 server found)
- ;; global options: +cmd
- ;; Got answer:
- ;; ->>HEADER<<- class="number" id:="" nbsp="" noerror="" opcode:="" query="" span="" status:="" style="background-color: inherit; border: none; color: #c00000; margin: 0px; padding: 0px;">7741->
The -t option sets the query type to type. It can be any valid query type which is supported in BIND 9. The default query type is "A", unless the -x option is supplied to indicate a reverse lookup. Below is a simple example:
FIND THE IP ADDRESS (A) OF A HOSTNAME
You can use "+short" to show the query result only:
FIND THE HOSTNAME OF AN IP ADDRESS
You can use option -x to carry out the DNS reverse Look-up
FIND THE NS (NAME SERVERS) OF A DOMAIN NAME
You can use query type "NS" as below example:
Real World Case Study - Find Google CDN IP range
Below bash script is from here which is used to dig out all Google CDN IP range:
- #!/bin/bash
- # https://cloud.google.com/compute/docs/faq#ipranges
- #nslookup -q=TXT _cloud-netblocks.googleusercontent.com 8.8.8.8
- for LINE in `dig txt _cloud-netblocks.googleusercontent.com +short | tr " " "\n" | grep include | cut -f 2 -d :`
- do
- dig txt $LINE +short
- done | tr " " "\n" | grep ip4 | cut -f 2 -d : | sort -n
Supplement
* Linux and Unix dig Command Examples
* Dig 常用參數 與 DNS 偵錯追蹤
* DNS(二)通过dig命令理解DNS
* DNS資源紀錄 (Resource Record) 介紹
* 10 Linux DIG Command Examples for DNS Lookup
沒有留言:
張貼留言