10 Linux DIG Command Examples for DNS Lookup--reference
by RAMESH NATARAJAN on FEBRUARY 29, 2012
http://www.thegeekstuff.com/2012/02/dig-command-examples/
Dig stands for domain information groper.
Using dig command you can query DNS name servers for your DNS lookup related tasks. This article explains 10 examples on how to use dig command.
1. Simple dig Command Usage (Understand dig Output)
When you pass a domain name to the dig command, by default it displays the A record (the ip-address of the site that is queried) as shown below.
In this example, it displays the A record of redhat.com in the “ANSWER SECTION” of the dig command output.
$ dig redhat.com ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62863
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 3 ;; QUESTION SECTION:
;redhat.com. IN A ;; ANSWER SECTION:
redhat.com. 37 IN A 209.132.183.81 ;; AUTHORITY SECTION:
redhat.com. 73 IN NS ns4.redhat.com.
redhat.com. 73 IN NS ns3.redhat.com.
redhat.com. 73 IN NS ns2.redhat.com.
redhat.com. 73 IN NS ns1.redhat.com. ;; ADDITIONAL SECTION:
ns1.redhat.com. 73 IN A 209.132.186.218
ns2.redhat.com. 73 IN A 209.132.183.2
ns3.redhat.com. 73 IN A 209.132.176.100 ;; Query time: 13 msec
;; SERVER: 209.144.50.138#53(209.144.50.138)
;; WHEN: Thu Jan 12 10:09:49 2012
;; MSG SIZE rcvd: 164
The dig command output has the following sections:
- Header: This displays the dig command version number, the global options used by the dig command, and few additional header information.
- QUESTION SECTION: This displays the question it asked the DNS. i.e This is your input. Since we said ‘dig redhat.com’, and the default type dig command uses is A record, it indicates in this section that we asked for the A record of the redhat.com website
- ANSWER SECTION: This displays the answer it receives from the DNS. i.e This is your output. This displays the A record of redhat.com
- AUTHORITY SECTION: This displays the DNS name server that has the authority to respond to this query. Basically this displays available name servers of redhat.com
- ADDITIONAL SECTION: This displays the ip address of the name servers listed in the AUTHORITY SECTION.
- Stats section at the bottom displays few dig command statistics including how much time it took to execute this query
2. Display Only the ANSWER SECTION of the Dig command Output
For most part, all you need to look at is the “ANSWER SECTION” of the dig command. So, we can turn off all other sections as shown below.
- +nocomments – Turn off the comment lines
- +noauthority – Turn off the authority section
- +noadditional – Turn off the additional section
- +nostats – Turn off the stats section
- +noanswer – Turn off the answer section (Of course, you wouldn’t want to turn off the answer section)
The following dig command displays only the ANSWER SECTION.
$ dig redhat.com +nocomments +noquestion +noauthority +noadditional +nostats ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com +nocomments +noquestion +noauthority +noadditional +nostats
;; global options: +cmd
redhat.com. 9 IN A 209.132.183.81
Instead of disabling all the sections that we don’t want one by one, we can disable all sections using +noall (this turns off answer section also), and add the +answer which will show only the answer section.
The above command can also be written in a short form as shown below, which displays only the ANSWER SECTION.
$ dig redhat.com +noall +answer ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com +noall +answer
;; global options: +cmd
redhat.com. 60 IN A 209.132.183.81
3. Query MX Records Using dig -t MX
To query MX records, pass MX as an argument to the dig command as shown below.
$ dig redhat.com MX +noall +answer ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com MX +noall +answer
;; global options: +cmd
redhat.com. 513 IN MX 5 mx1.redhat.com.
redhat.com. 513 IN MX 10 mx2.redhat.com.
You can also use option -t to pass the query type (for example: MX) as shown below.
$ dig -t MX redhat.com +noall +answer ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> -t MX redhat.com +noall +answer
;; global options: +cmd
redhat.com. 489 IN MX 10 mx2.redhat.com.
redhat.com. 489 IN MX 5 mx1.redhat.com.
4. Query NS Records Using dig -t NS
To query the NS record use the type NS as shown below.
$ dig redhat.com NS +noall +answer ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com NS +noall +answer
;; global options: +cmd
redhat.com. 558 IN NS ns2.redhat.com.
redhat.com. 558 IN NS ns1.redhat.com.
redhat.com. 558 IN NS ns3.redhat.com.
redhat.com. 558 IN NS ns4.redhat.com.
You can also use option -t to pass the query type (for example: NS) as shown below.
$ dig -t NS redhat.com +noall +answer ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> -t NS redhat.com +noall +answer
;; global options: +cmd
redhat.com. 543 IN NS ns4.redhat.com.
redhat.com. 543 IN NS ns1.redhat.com.
redhat.com. 543 IN NS ns3.redhat.com.
redhat.com. 543 IN NS ns2.redhat.com.
5. View ALL DNS Records Types Using dig -t ANY
To view all the record types (A, MX, NS, etc.), use ANY as the record type as shown below.
$ dig redhat.com ANY +noall +answer ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com ANY +noall +answer
;; global options: +cmd
redhat.com. 430 IN MX 5 mx1.redhat.com.
redhat.com. 430 IN MX 10 mx2.redhat.com.
redhat.com. 521 IN NS ns3.redhat.com.
redhat.com. 521 IN NS ns1.redhat.com.
redhat.com. 521 IN NS ns4.redhat.com.
redhat.com. 521 IN NS ns2.redhat.com.
(or) Use -t ANY
$ dig -t ANY redhat.com +noall +answer ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> -t ANY redhat.com +noall +answer
;; global options: +cmd
redhat.com. 367 IN MX 10 mx2.redhat.com.
redhat.com. 367 IN MX 5 mx1.redhat.com.
redhat.com. 458 IN NS ns4.redhat.com.
redhat.com. 458 IN NS ns1.redhat.com.
redhat.com. 458 IN NS ns2.redhat.com.
redhat.com. 458 IN NS ns3.redhat.com.
6. View Short Output Using dig +short
To view just the ip-address of a web site (i.e the A record), use the short form option as shown below.
$ dig redhat.com +short
209.132.183.81
You can also specify a record type that you want to view with the +short option.
$ dig redhat.com ns +short
ns2.redhat.com.
ns3.redhat.com.
ns1.redhat.com.
ns4.redhat.com.
7. DNS Reverse Look-up Using dig -x
To perform a DNS reverse look up using the ip-address using dig -x as shown below
For example, if you just have an external ip-address and would like to know the website that belongs to it, do the following.
$ dig -x 209.132.183.81 +short
www.redhat.com.
To view the full details of the DNS reverse look-up, remove the +short option.
$ dig -x 209.132.183.81 ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> -x 209.132.183.81
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62435
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 3 ;; QUESTION SECTION:
;81.183.132.209.in-addr.arpa. IN PTR ;; ANSWER SECTION:
81.183.132.209.in-addr.arpa. 600 IN PTR www.redhat.com. ;; AUTHORITY SECTION:
183.132.209.in-addr.arpa. 248 IN NS ns2.redhat.com.
183.132.209.in-addr.arpa. 248 IN NS ns1.redhat.com.
183.132.209.in-addr.arpa. 248 IN NS ns3.redhat.com.
183.132.209.in-addr.arpa. 248 IN NS ns4.redhat.com. ;; ADDITIONAL SECTION:
ns1.redhat.com. 363 IN A 209.132.186.218
ns2.redhat.com. 363 IN A 209.132.183.2
ns3.redhat.com. 363 IN A 209.132.176.100 ;; Query time: 35 msec
;; SERVER: 209.144.50.138#53(209.144.50.138)
;; WHEN: Thu Jan 12 10:15:00 2012
;; MSG SIZE rcvd: 193
8. Use a Specific DNS server Using dig @dnsserver
By default dig uses the DNS servers defined in your /etc/resolv.conf file.
If you like to use a different DNS server to perform the query, specify it in the command line as @dnsserver.
The following example uses ns1.redhat.com as the DNS server to get the answer (instead of using the DNS servers from the /etc/resolv.conf file).
$ dig @ns1.redhat.com redhat.com ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> @ns1.redhat.com redhat.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20963
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 4
;; WARNING: recursion requested but not available ;; QUESTION SECTION:
;redhat.com. IN A ;; ANSWER SECTION:
redhat.com. 60 IN A 209.132.183.81 ;; AUTHORITY SECTION:
redhat.com. 600 IN NS ns1.redhat.com.
redhat.com. 600 IN NS ns4.redhat.com.
redhat.com. 600 IN NS ns3.redhat.com.
redhat.com. 600 IN NS ns2.redhat.com. ;; ADDITIONAL SECTION:
ns1.redhat.com. 600 IN A 209.132.186.218
ns2.redhat.com. 600 IN A 209.132.183.2
ns3.redhat.com. 600 IN A 209.132.176.100
ns4.redhat.com. 600 IN A 209.132.188.218 ;; Query time: 160 msec
;; SERVER: 209.132.186.218#53(209.132.186.218)
;; WHEN: Thu Jan 12 10:22:11 2012
;; MSG SIZE rcvd: 180
9. Bulk DNS Query Using dig -f (and command line)
Query multiple websites using a data file:
You can perform a bulk DNS query based on the data from a file.
First, create a sample names.txt file that contains the website that you want to query.
$ vi names.txt
redhat.com
centos.org
Next, execute dig -f as shown below, which will perform DNS query for the websites listed in the names.txt file and display the output.
$ dig -f names.txt +noall +answer
redhat.com. 60 IN A 209.132.183.81
centos.org. 60 IN A 72.232.194.162
You can also combine record type with the -f option. The following example displays the MX records of multiple websites that are located in the names.txt file.
$ dig -f names.txt MX +noall +answer
redhat.com. 600 IN MX 10 mx2.redhat.com.
redhat.com. 600 IN MX 5 mx1.redhat.com.
centos.org. 3600 IN MX 10 mail.centos.org.
Query multiple websites from dig command line:
You can also query multiple websites from the dig command line as shown below. The following example queries MX record for redhat.com, and NS record for centos.org from the command line
$ dig redhat.com mx +noall +answer centos.org ns +noall +answer ; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com mx +noall +answer centos.org ns +noall +answer
;; global options: +cmd
redhat.com. 332 IN MX 10 mx2.redhat.com.
redhat.com. 332 IN MX 5 mx1.redhat.com.
centos.org. 3778 IN NS ns3.centos.org.
centos.org. 3778 IN NS ns4.centos.org.
centos.org. 3778 IN NS ns1.centos.org.
10. Use $HOME/.digrc File to Store Default dig Options
If you are always trying to view only the ANSWER section of the dig output, you don’t have to keep typing “+noall +answer” on your every dig command. Instead, add your dig options to the .digrc file as shown below.
$ cat $HOME/.digrc
+noall +answer
Now anytime you execute dig command, it will always use +noall and +answer options by default. Now the dig command line became very simple and easy to read without you have to type those options every time.
$ dig redhat.com
redhat.com. 60 IN A 209.132.183.81 $ dig redhat.com MX
redhat.com. 52 IN MX 5 mx1.redhat.com.
redhat.com. 52 IN MX 10 mx2.redhat.com.
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator
10 Linux DIG Command Examples for DNS Lookup--reference的更多相关文章
- 15 Linux Split and Join Command Examples to Manage Large Files--reference
		by HIMANSHU ARORA on OCTOBER 16, 2012 http://www.thegeekstuff.com/2012/10/15-linux-split-and-join-co ... 
- 18 Tar Command Examples in Linux
		FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ... 
- 12 Linux Which Command, Whatis Command, Whereis Command Examples
		This Linux tutorial will explain the three "W" commands. The three "W"s are what ... 
- 15 Basic ‘ls’ Command Examples in Linux
		FROM: http://www.tecmint.com/15-basic-ls-command-examples-in-linux/ ls command is one of the most fr ... 
- 15 Practical Grep Command Examples In Linux / UNIX
		You should get a grip on the Linux grep command. This is part of the on-going 15 Examples series, wh ... 
- linux环境,无dig命令-bash: dig: command not found?
		背景描述: 今天使用dig命令,报错命令不存在,-bash: dig: command not found 解决: 通过yum方式安装 yum -y install bind-utils 备注:之前尝 ... 
- Top 30 Nmap Command Examples For Sys/Network Admins
		Nmap is short for Network Mapper. It is an open source security tool for network exploration, securi ... 
- Linux运维实战之DNS(bind)服务器的安装与配置
		转自http://sweetpotato.blog.51cto.com/533893/1598225 上次博文我们讨论了DNS的基础,本次博文我们重点来看看如何配置一台DNS服务器. [本次博文的主要 ... 
- Linux搭建基于BIND的DNS服务器
		Linux搭建基于BIND的DNS服务器 实验目标: 通过本实验掌握基于Linux的DNS服务器搭建. 实验步骤: 1.安装BIND 2.防火墙放通DNS服务 3.编辑BIND的主配置文件 4.编 ... 
随机推荐
- c#winform pictureBox使用url加载图片
			string url = "http://b.hiphotos.baidu.com/image/pic/item/03087bf40ad162d93b3a196f1fdfa9ec8b13cd ... 
- angular 子路由
			const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', compo ... 
- WinForm中的多语言处理
			配置文件中存储当前语言环境,切换语言时进行修改,启动程序时读取该配置并设置当前线程的Culture 可根据线程的语言环境动态读取不同的资源文件,不同资源文件名用语言环境文本进行区分 
- 编码原则实例------c++程序设计原理与实践(进阶篇)
			编码原则: 一般原则 预处理原则 命名和布局原则 类原则 函数和表达式原则 硬实时原则 关键系统原则 (硬实时原则.关键系统原则仅用于硬实时和关键系统程序设计) (严格原则都用一个大写字母R及其编号标 ... 
- 类型转换构造函数 及使用explicit避免类型自动转换------新标准c++程序设计
			类型转换构造函数: 除复制构造函数外,只有一个参数的构造函数一般可以称作类型转换构造函数,因为这样的构造函数能起到类型自动转换的作用.例如下面的程序: #include<iostream> ... 
- NSURLConnection 网络请求
			前言 DEPRECATED: The NSURLConnection class should no longer be used. NSURLSession is the replacement f ... 
- 为什么要使用MQ消息中间件?
			在面试大型互联网公司的时候,很可能会被问到消息队列的问题: 1.在何种场景下使用了消息中间件? 2.为什么要在系统里引入消息中间件? 3.如何实现幂等? 链式调用是我们在写程序时候的一般流程,为了完成 ... 
- [AGC002D] Stamp Rally 整体二分+并查集
			Description 给你一个n个点m个条边构成的简单无向连通图,有Q组询问,每次询问从两个点x,y走出两条路径,使这两条路径覆盖z个点,求得一种方案使得路径上经过的变的最大编号最小. Input ... 
- 题解 P2960 【[USACO09OCT]Milkweed的入侵Invasion of the Milkweed】
			题目链接 首先这道题是一道经典的BFS.非常适合刚刚学习深搜的同学. 现在分析一下这个问题.首先,每周是八个方向.就是一圈. 也就是说入侵的范围关于时间是成辐射型扩散.让求最大时间. 也就是完美的BF ... 
- 自动化上传图片,路径driver.find_element_by_id("oper-img-change").send_keys("C:\\Users\\76776\\Pictures\\logo.jpg"),为正确姿势,单\报错 'unicodeescape' codec can't decode bytes in position XXX: trun
			自动化上传图片,路径driver.find_element_by_id("oper-img-change").send_keys("C:\\Users\\76776\\P ... 
