0.前言

在日常上网时都是用域名访问网路,如www.baidu.com,而在实际寻址过程中,是使用IP地址,如180.101.49.11,域名到IP地址的解析是通过DNS服务器来实现的,在系统中可以用一些网络命令来解析指定的域名,比如说nslookup,dig和host命令。

1.nslookup

1.1 nslookup命令简介

nslookup命令用于查询DNS记录,查看域名解析是否正常,在网络故障时可以用来诊断网络问题。

nslookup(全称name server lookup),是一个在命令行界面下的网络工具,它有两种模式: 交互 & 非交互,进入交互模式在命令行界面直接输入nslookup按回车,非交互模式则是后面跟上查询的域名或者IP地址按回车。一般来说,非交互模式适用于简单的单次查询,若需要多次查询,则交互模式更加适合,从根服务器进行迭代查询。

RR (Resource Records)————资源记录(RR)是包含了下列字段的4元组:
(Name, Value, Type, TTL)

  • 主机记录(A记录):RFC 1035 定义,A记录是用于名称解析的重要记录,提供标准的主机名到IP的地址映射。
  • 别名记录(CNAME记录):RFC 1035 定义,向查询的主机提供主机名对应的规范主机名。
  • 域名服务器记录(NS记录):用来指定该域名由哪个DNS服务器来进行解析。您注册域名时,总有默认的DNS服务器,每个注册的域名都是由一个DNS域名服务器来进行解析的,DNS服务器NS记录地址一般以以下的形式出现:ns1.domain.com、ns2.domain.com等。简单的说,NS记录返回域中主机IP地址的权威DNS服务器的主机名。
  • 邮件交换记录(MX记录):返回别名为Name对应的邮件服务器的规范主机名。

语法格式:

nslookup –option1 –option2 host-to-find dns-server

nslookup命令分为直接查询指定参数查询。

直接查询的语法为nslookup domain [dns-server],其中domain是要查询的域名,dns-server是可选字段,默认使用本地DNS服务器(/etc/resolve.conf)解析。

指定参数查询的语法为nslookup -qt=type domain [dns-server],其中type表示参数类型,常见类型有:

A 地址记录(IPV4)
AAAA 地址记录(IPV6)
CNAME 别名记录
MX 邮件服务器记录
PTR 反向记录

格式:

nslookup [FQDN] [server]

选项与参数:

  1. 可以直接在 nslookup 加上待查询的主机名或者是IP,[server]可有可无;
  2. 如果在 nslookup 后面没有加上任何主机名或 IP,那将进入 nslookup的查询功能,在 nslookup 的查询功能当中,可以输入其他参数来进行特殊查询。

例如:
set type=any: 列出所有的信息(正解方面配置文件)
set type=mx: 列出与mx相关的信息

1.2 nslookup应用

1.2.1 非交互模式

使用非交互模式nslookup www.baidu.com解析百度的域名

其中119.29.29.29是域名服务器地址(腾讯DNSPod DNS),端口为53号,canonical name是别名,Address指的是域名对应的IP地址。
除此之外,可以指定域名服务器,比如nslookup www.baidu.com 8.8.8.8

然后再使用指定参数查询,如nslookup -q=AAAA www.qq.com的IPV6地址(注:Linux下使用nslookup -q=type domain)

可以查询到对应的IPV6地址。

1.2.2 交互模式

在命令行下输入

nslookup
baidu.com

可以看到进入交互模式后不再需要输入完整的命令便可以进行查询,并且可以连续的进行查询(友情提示Ctrl+C或者Cmd+C退出)

[root@devops03 ~]# nslookup 
> baidu.com
Server:         223.5.5.5
Address:        223.5.5.5#53

Non-authoritative answer:
Name:   baidu.com
Address: 110.242.68.66
Name:   baidu.com
Address: 39.156.66.10

> qq.com
Server:         223.5.5.5
Address:        223.5.5.5#53

Non-authoritative answer:
Name:   qq.com
Address: 123.151.137.18
Name:   qq.com
Address: 183.3.226.35
Name:   qq.com
Address: 61.129.7.47

> google.com
Server:         119.29.29.29
Address:        119.29.29.29#53

Non-authoritative answer:
Name:   google.com
Address: 172.217.163.46

输出部分:

  • 最上面的 Server 和 Address 是该词查询的 DNS 服务器。可以自己指定,也可以默认,之后会说到。
  • 默认情况下 DNS 服务器的端口为53
  • 非权威应答(Non-authoritative answer)意味着answer来自于其他服务器的缓存,而不是权威的Baidu DNS服务器。缓存会根据 ttl(Time to Live)的值定时的进行更新。这个链接或许对你有所帮助: What is the meaning of the non-authoritative-answer?
  • 注意到,在对google.com进行查询时,Mac 返回的结果并没有非权威服务器提示,而Windows下的命令却提示了,这是因为 8.8.8.8 这个 DNS 服务器正是谷歌的权威名字服务器,可以使用nslookup -ty=ptr 8.8.8.8验证,ptr也是一种记录类型,可以用进行反向DNS解析(Reverse DNS Lookup),拓展链接: reverse-dns-lookup

If a hostname/IP address pair is cached in a DNS server and another query arrives to the DNS server for the same hostname, the DNS server can provide the desired IP address, even if it is not authoritative for the hostname. Because hosts and mappings between hostnames and IP addresses are by no means permanent, DNS servers discard cached information after a period of time (often set to two days).

PS: 查询返回的多个结果均正确,不妨在你的浏览器用默认的80号端口试着访问一下,以第一个返回的百度网站为例,39.156.66.10:80

1.2.3 拓展: PTR反向DNS解析(8.8.8.8)
[root@devops03 ~]# nslookup -ty=prt 8.8.8.8
unknown query type: prt
8.8.8.8.in-addr.arpa    name = dns.google.

Authoritative answers can be found from:

[root@devops03 ~]# nslookup -ty=prt 114.114.114.114
unknown query type: prt
114.114.114.114.in-addr.arpa    name = public1.114dns.com.

Authoritative answers can be found from:
1.2.4 帮助界面

或许提前看看命令的帮助界面会非常的有帮助

  • Mac下输入man nslookup | less,使用空格往下翻页,b往上翻页,q退出
man nslookup | less
  • Windows下有两种,一种是直接输入nslookup/?,还有一种是在交互模式下输入help或者?
nslookup/?

Windows 给出的命令为通用命令,Mac/Linux 可用于借鉴

1.2.5 关于querytype和type的小疑惑

-querytype-type的效用一致,可以简写为-q-ty,其在不指定类型的情况下默认查询类型为 A。

1.2.6 设置查询类型

非交互模式: nslookup -ty=类型 name
交互模式: set ty=类型
先来看看之前提到的 NS 类型

1.2.7 NS(Nameserver DNS record)

查找权威名字服务器

If Type=NS , then Name is a domain (such as foo.com ) and Value is the hostname of an authoritative DNS server that knows how to obtain the IP addresses for hosts in the domain.This record is used to route DNS queries further along in the query chain. As an example, ( foo.com , dns.foo.com , NS) is a Type NS record.

[root@devops03 ~]# nslookup 
> set ty=NS
> set all
Default server: 119.29.29.29
Address: 119.29.29.29#53
Default server: 223.5.5.5
Address: 223.5.5.5#53
Default server: 114.114.114.114
Address: 114.114.114.114#53

Set options:
  novc                  nodebug         nod2
  search                recurse
  timeout = 1           retry = 4       port = 53       ndots = 1
  querytype = NS        class = IN
  srchlist = 

PS: 交互模式下的 set 命令可以更改影响查找的状态信息,不仅用于记录类型的变更,使用set all可以查看常用选项的当前值以及当前默认的服务器和主机信息,注意,DNS 服务器不能通过set指定

接下来以baidu.com为出发点,来看看对应的权威名字服务器

> baidu.com
Server:         119.29.29.29
Address:        119.29.29.29#53

Non-authoritative answer:
baidu.com       nameserver = ns3.baidu.com.
baidu.com       nameserver = dns.baidu.com.
baidu.com       nameserver = ns7.baidu.com.
baidu.com       nameserver = ns2.baidu.com.
baidu.com       nameserver = ns4.baidu.com.

Authoritative answers can be found from:

Q1:哪个才是baidu.com的权威DNS服务器?请用 “name, value” 格式描述一下NS记录的返回内容
S1: 返回的 ns2.baidu.com ns7.baidu.com dns.baidu.com ns4.baidu.com ns3.baidu.com皆为baidu.com的权威DNS服务器。
dns.baidu.com为例,NS记录的内容: (baidu.com, dns.baidu.com)

拓展: dig 命令
如果你的操作系统是 Mac/Linux,不妨尝试一下dig baidu.com ns,会对结果有个更直观的感触

dig baidu.com ns

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.5 <<>> baidu.com ns
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45503
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
baidu.com.              86400   IN      NS      ns4.baidu.com.
baidu.com.              86400   IN      NS      dns.baidu.com.
baidu.com.              86400   IN      NS      ns7.baidu.com.
baidu.com.              86400   IN      NS      ns2.baidu.com.
baidu.com.              86400   IN      NS      ns3.baidu.com.

;; Query time: 18 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: Tue Sep 13 17:17:21 CST 2022
;; MSG SIZE  rcvd: 128

其中86400为TTL,即DNS缓存刷新时间,单位为s ,NS就是这次查询的类型。

1.2.8 A(查看 IP 地址)

If Type=A , then Name is a hostname and Value is the IP address for the hostname. Thus, a Type A record provides the standard hostname-to-IP address mapping. As an example, ( relay1.bar.foo.com , 145.37.93.126, A) is a Type A record.

用以下命令(交互模式下)指定dns.baidu.com对baidu.com执行type=A的查询来看看结果与最开始有什么不同。

> server dns.baidu.com
Default server: dns.baidu.com
Address: 110.242.68.134#53
> set ty=A
> baidu.com
Server:         dns.baidu.com
Address:        110.242.68.134#53

Name:   baidu.com
Address: 110.242.68.66
Name:   baidu.com
Address: 39.156.66.10

发现了吗,此时并没有提示非权威应答(Non-authoritative answer),不妨思考一下以下两个问题:
Q2:A 记录的内容是什么?用 “name, value” 格式回答其中之一
S2: baidu.com的 ip 地址,(baidu.com, 110.242.68.66)

Q3:现在的这次查询和第一次有什么区别?
S3: 该次查询指定了 baidu.com 的权威DNS服务器

1.2.9 MX(Mail eXchange record)

A mail exchanger record (MX record) specifies the mail server responsible for accepting email messages on behalf of a domain name. It is a resource record in the Domain Name System (DNS). It is possible to configure several MX records, typically pointing to an array of mail servers for load balancing and redundancy.

查找host-to-find域的邮件服务器,

[root@devops03 ~]# nslookup 
> set ty=mx
> ustc.edu.cn
Server:         119.29.29.29
Address:        119.29.29.29#53

Non-authoritative answer:
ustc.edu.cn     mail exchanger = 10 smtp.ustc.edu.cn.
ustc.edu.cn     mail exchanger = 5 smtp1.ustc.edu.cn.
ustc.edu.cn     mail exchanger = 10 smtp2.ustc.edu.cn.

Authoritative answers can be found from:

熟悉的SMTP(Simple Mail Transfer Protocol) (ps:smtp并非一定是邮件服务器名字的组成部分)。

1.2.10 CNAME(Canonical Name record)

CNAME记录将当前 hostname 映射到另一个 hostname,相应的记录 (name, value)中的name对应的是value的别名:

> set ty=cname
> www.bing.com
Server:         119.29.29.29
Address:        119.29.29.29#53

Non-authoritative answer:
www.bing.com    canonical name = www-www.bing.com.trafficmanager.net.

Authoritative answers can be found from:

2.dig

2.1 dig简介

除了常用的nslookup之外,有一个更为强大的命令dig,dig是UNIX/BSD系统都自带的DNS诊断工具,使用起来比较灵活,功能也足够强大,windows下的nslookup功能比较简单,Linux下的dig命令支持更多DNS查询功能,比如DNS跟踪,一般使用dig domain +trace进行DNS解析跟踪。
当本地的DNS服务器去访问一个域名的时候,整个域名解析的完整过程如下:

2.2 dig应用

1.dig查询www.baidu.com的ip全过程

结合具体的例子来看一下DNS解析域名的具体过程。

首先在终端输入dig www.baidu.com +trace

拆解这整个过程,可以看出从119.29.29.29服务器返回了13个根域名服务器信息,系统从13个根服务器随机选择一个进行访问。

可以看到系统随机选择了根域名服务器e.root-servers.net,并返回了.com域名服务器列表。

系统选择了e.gtld-servers.net并返回了a.shifen.com.的域名服务器列表。

接下来系统访问ns7.baidu.com并返回了www.baidu.com的域名服务器列表。

最后得到www.baidu.com的IP地址为180.76.76.92。

  1. 查询linux.vbird.org的SOA相关信息(授权机构起始——SOA,记录存储有关域或区域的重要信息,如管理员的电子邮件地址、域上次更新的时间,以及服务器在刷新之间应等待的时间。)
dig -t soa linux.vbird.org

由于dig的输出信息太丰富了,又分成多个部分去进行输出,很适合作为DNS追踪回报的一个指令,可以透过这个指令来了解一下所设定的DNS数据库是否正确,并进行除错。此外,也可以透过-t type的功能去查询其他服务器的设定值,可以方便设定DNS服务器时的参考,正解查询完毕,接下来看一下反解。

  1. 查询180.101.49.11(百度)反解析
dig -x 180.101.49.11

从上面的输出结果来看,反解的查询目标从180.101.49.11变成了101.180.in-addr.arpa.查询域名跟正解不一样,结尾的in-addr.arpa.可以先记下来。

3.host

3.1host简介

格式:

host [-a] FQDN [server]
host -l domain [server]

选项:
-a: 代表列出该主机所有的相关信息,包括IP、TTL与除错讯息等;
-l: 如果domain设定允许allow-transfer时,则列出该domain所管理的所有主机名对应数据。

server: 这个参数可有可无,当想要利用非/etc/resolv.conf内的DNS主机来查询主机名与IP的对应时,就可以利用这个参数了。

3.2host应用

1.使用默认值来查出百度的IP

host www.baidu.com

  1. 查出百度的所有重要参数
host -a www.baidu.com

  1. 强制以8.8.8.8这台DNS服务器来查询
host www.baidu.com 8.8.8.8

  1. 找出sxjy.com域的所有主机
host -l sxjy.com
; Transfer failed.
Host sxjy.com not found: 9(NOTAUTH)
; Transfer failed.

无法响应是因为管理vbird.org领域的DNS并不许我们的领域查询,毕竟我们不是vbird.org的系统管理员,没有权限可以读取整个vbird.org的领域设定,host -l是用在自己的DNS服务器上,使用这个选项就能够读取相关的数据了。

posted on 2021-01-18 12:02  jiayou111  阅读(3831)  评论(0编辑  收藏  举报