各种 DNS 工具的使用
DNS工具
1 - windows工具
1.1 - 概述
1.2 - ipconfig
刷新DNS记录
当本地 dns 记录已经过期,但由于缓存的原因没能及时更新时,可以手动刷新 dns 记录。
ipconfig /flushdns
输出为:
Windows IP 配置
已成功刷新 DNS 解析缓存。
之后再用 nslookup 命令就能拿到最新的 dns 记录了。
注意:这个命令可以在 cmd 或者 powershell 下使用。但是不能在 git bash 下使用。会报错:
ipconfig /flushdns
Error: unrecognized or incomplete command line.
USAGE:
ipconfig [/allcompartments] [/? | /all |
/renew [adapter] | /release [adapter] |
/renew6 [adapter] | /release6 [adapter] |
/flushdns | /displaydns | /registerdns |
/showclassid adapter |
/setclassid adapter [classid] |
/showclassid6 adapter |
/setclassid6 adapter [classid] ]
where
adapter Connection name
(wildcard characters * and ? allowed, see examples)
Options:
/? Display this help message
/all Display full configuration information.
/release Release the IPv4 address for the specified adapter.
/release6 Release the IPv6 address for the specified adapter.
/renew Renew the IPv4 address for the specified adapter.
/renew6 Renew the IPv6 address for the specified adapter.
/flushdns Purges the DNS Resolver cache.
/registerdns Refreshes all DHCP leases and re-registers DNS names
/displaydns Display the contents of the DNS Resolver Cache.
/showclassid Displays all the dhcp class IDs allowed for adapter.
/setclassid Modifies the dhcp class id.
/showclassid6 Displays all the IPv6 DHCP class IDs allowed for adapter.
/setclassid6 Modifies the IPv6 DHCP class id.
The default is to display only the IP address, subnet mask and
default gateway for each adapter bound to TCP/IP.
For Release and Renew, if no adapter name is specified, then the IP address
leases for all adapters bound to TCP/IP will be released or renewed.
For Setclassid and Setclassid6, if no ClassId is specified, then the ClassId is removed.
Examples:
> ipconfig ... Show information
> ipconfig /all ... Show detailed information
> ipconfig /renew ... renew all adapters
> ipconfig /renew EL* ... renew any connection that has its
name starting with EL
> ipconfig /release *Con* ... release all matching connections,
eg. "Wired Ethernet Connection 1" or
"Wired Ethernet Connection 2"
> ipconfig /allcompartments ... Show information about all
compartments
> ipconfig /allcompartments /all ... Show detailed information about all
compartments
显示 DNS 解析缓存内容
ipconfig /displaydns
2 - linux工具
2.1 - 概述
2.2 - host
host将主机名解析为IP地址或将IP地址解析为主机名。
语法
host [-aCdlriTwv] [-c class] [-N ndots] [-t type] [-W time] [-R number] [-m flag] hostname [server]
host 命令会返回主机的 IP 地址(当指定了 HostName 参数时)和主机名(当指定了 Address 参数时)。根据名称解析服务的配置,host 命令还可能显示与 HostName 参数关联的任何别名。 名称解析服务的示例包含 local、nis 和 bind。
命令行选项
注意:不同操作系统下的命令行选项会有所不同,下面是ubuntu 16.04的参数:
选项 | 描述 |
---|---|
-a | 相当于使用“-v -t *” |
-c Class | 指定当它搜索非因特网数据时要查看的类。以下是有效类:IN: 因特网类;CHAOS: Chaos类;HESIOD: MIT Althena Hesiod 类; ANY: 通配符(以上任意一个) |
-C | 比较权威名称服务器上的SOA记录 |
-d | 等同 -v |
-l | lists all hosts in a domain, using AXFR |
-i | IP6.INT reverse lookups |
-n | 相当于发布 /usr/bin/hostnew 命令。hostnew 命令将执行绑定解析服务。 |
-N | changes the number of dots allowed before root lookup is done |
-r | 禁用递归处理。 |
-R | specifies number of retries for UDP packets |
-s | a SERVFAIL response should stop query |
-t Type | 指定要查询的记录类型。 |
-T | enables TCP/IP mode |
-v | 详细方式。 |
-V | print version number and exit |
-w | 永远等待 DNS 服务器的一个回答。 |
-W | specifies how long to wait for a reply |
-4 | use IPv4 query transport only |
-6 | use IPv6 query transport only |
-m | set memory debugging flag `(trace |
-t Type
支持的类型有:
Type | 记录类型 |
---|---|
CNAME | 别名的规范名称 |
HINFO | 主机处理器和操作系统类型 |
KEY | 安全密钥记录 |
MINFO | 邮箱或邮件列表信息 |
MX | 邮件交换器 |
NS | 指定范围的名称服务器 |
PTR | 如果查询的是 IP 地址,那么此项为主机名;否则,为指向其他信息的指针 |
SIG | 签名记录 |
SOA | 域的"授权开始"信息 |
TXT | 文本信息 |
UINFO | 用户信息 |
WKS | 所支持的众所周知的服务 |
命令行参数
参数 | 描述 |
---|---|
Address | 指定解析主机名时要使用的主机的 IP 地址。Address 参数必须是有效的 IP 地址,以点分十进制格式表示。 |
HostName | 指定解析 IP 地址时要使用的主机的名称。HostName 参数可以是不平常的主机名,也可以是熟知主机名(例如 nameserver、printserver 或 timeserver,如果这些名称存在)。 |
Server | 指定查询要使用的名称服务器。可选,如果没有指定则使用 resolv.conf 文件中定义的DNS服务器 |
Address 和 HostName 参数必须二选一,Server 可选。
使用案例
最简单的查询,默认只有A和MX记录:
$ host skyao.io
skyao.io has address 119.28.182.148
如果要输出全部信息:
host -a skyao.io
Trying "skyao.io"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34465
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 10
;; QUESTION SECTION:
;skyao.io. IN ANY
;; ANSWER SECTION:
skyao.io. 561 IN A 119.28.182.148
skyao.io. 22 IN NS f1g1ns1.dnspod.net.
skyao.io. 22 IN NS f1g1ns2.dnspod.net.
skyao.io. 22 IN SOA f1g1ns1.dnspod.net. freednsadmin.dnspod.com. 1515202762 3600 180 1209600 180
;; ADDITIONAL SECTION:
f1g1ns1.dnspod.net. 76593 IN A 61.151.180.44
......
f1g1ns2.dnspod.net. 79458 IN A 182.140.167.188
Received 315 bytes from 30.102.12.12#53 in 3988 ms
查询SOA权威域名服务器:
$ host -C skyao.io
Nameserver 121.51.128.164:
skyao.io has SOA record f1g1ns1.dnspod.net. freednsadmin.dnspod.com. 1515202762 3600 180 1209600 180
Nameserver 14.215.155.170:
skyao.io has SOA record f1g1ns1.dnspod.net. freednsadmin.dnspod.com. 1515202762 3600 180 1209600 180
......
Nameserver 121.51.1.151:
skyao.io has SOA record f1g1ns1.dnspod.net. freednsadmin.dnspod.com. 1515202762 3600 180 1209600 180
参考资料
- host 命令@IBM Knowledge Center:信息很清晰,不过和linux上的host命令,有些细微差异。
2.3 - nslookup
nslookup支持交互模式和非交互模式。
非交互模式
进入非交互模式,即一次性执行一次查询操作就结束,如果直接在nslookup命令后加上所要查询的IP或主机名,那么就进入了非交互模式。当然,也可以在第二个参数位置设置所要连接的域名服务器。
nslookup [-option] [name | -] [server]
可以通过man nslookup
查看详细信息。
简单的直接查询:
$ nslookup skyao.io
Server: 10.243.28.52
Address: 10.243.28.52#53
Non-authoritative answer:
Name: skyao.io
Address: 119.28.182.148
指定要使用的DNS服务器:
$ nslookup skyao.io 8.8.8.8
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
Name: skyao.io
Address: 119.28.182.148
指定记录类型:
$ nslookup -query=ns skyao.io
Server: 10.243.28.52
Address: 10.243.28.52#53
Non-authoritative answer:
skyao.io nameserver = f1g1ns2.dnspod.net.
skyao.io nameserver = f1g1ns1.dnspod.net.
Authoritative answers can be found from:
交互模式
进入交互模式有如下方法:
- 直接输入nslookup命令,不加任何参数,则直接进入交互模式,此时nslookup会连接到默认的域名服务器(即/etc/resolv.conf的第一个dns地址)。
- 是支持选定不同域名服务器的。需要设置第一个参数为“-”,然后第二个参数是设置要连接的域名服务器主机名或IP地址。例如可以设置本机为域名服务器
nslookup - 127.0.0.1
进入交互模式,然后简单查询:
$ nslookup
> skyao.io
Server: 10.243.28.52
Address: 10.243.28.52#53
Non-authoritative answer:
Name: skyao.io
Address: 119.28.182.148
通过 set 命令设置查询不同的信息:
> set type=ns
> skyao.io
Server: 10.243.28.52
Address: 10.243.28.52#53
Non-authoritative answer:
skyao.io nameserver = f1g1ns2.dnspod.net.
skyao.io nameserver = f1g1ns1.dnspod.net.
Authoritative answers can be found from:
具体的set 参数,或者交互模式下的更多命令,请参考 man nslookup
。
参考资料
- nslookup 命令@IBM knowledge Center: 不是linux,有细微差别
2.4 - dig
dig(域信息搜索器)命令是一个用于询问 DNS 域名服务器的灵活的工具。
Dig执行 DNS 查询,显示从已查询名称服务器返回的应答。多数 DNS 管理员使用 dig 命令 来诊断 DNS 问题,因为它灵活性好、容易使用、输出清晰。虽然通常情况下 dig 与命令行参数配合使用,但它也可以按批处理方式从文件读取查询请求。
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} [...]]
详细参数看 dig -h
。
使用案例
简单查询:
$ dig skyao.io
; <<>> DiG 9.10.3-P4-Ubuntu <<>> skyao.io
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61118
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;skyao.io. IN A
;; ANSWER SECTION:
skyao.io. 599 IN A 119.28.182.148
;; Query time: 94 msec
;; SERVER: 10.243.28.52#53(10.243.28.52)
;; WHEN: Tue Nov 27 18:45:28 CST 2018
;; MSG SIZE rcvd: 53
指定要查询的记录类型:
$ dig skyao.io ns
; <<>> DiG 9.10.3-P4-Ubuntu <<>> skyao.io ns
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31927
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;skyao.io. IN NS
;; ANSWER SECTION:
skyao.io. 19119 IN NS f1g1ns2.dnspod.net.
skyao.io. 19119 IN NS f1g1ns1.dnspod.net.
;; Query time: 0 msec
;; SERVER: 10.243.28.52#53(10.243.28.52)
;; WHEN: Tue Nov 27 18:46:13 CST 2018
;; MSG SIZE rcvd: 91
dig命令最好用的方式是加上+trace
选项,然后就可以看到 dig 程序是如何一步一步解析出域名的IP地址的。从这里可以体验 DNS 的递归查询:
$ dig skyao.io +trace
; <<>> DiG 9.10.3-P4-Ubuntu <<>> skyao.io +trace
;; global options: +cmd
. 179247 IN NS l.root-servers.net.
......
. 179247 IN NS k.root-servers.net.
;; Received 239 bytes from 10.243.28.52#53(10.243.28.52) in 0 ms
io. 172800 IN NS a0.nic.io.
......
io. 172800 IN NS ns-a3.io.
io. 86400 IN DS 57355 8 1 434E91E206134F5B3B0AC603B26F5E029346ABC9
io. 86400 IN DS 57355 8 2 95A57C3BAB7849DBCDDF7C72ADA71A88146B141110318CA5BE672057 E865C3E2
io. 86400 IN DS 64744 8 2 2E7D661097A76EAC145858E4FF8F3DDAE5EAEDFD527725BC6F8A943E 4FE23A29
io. 86400 IN RRSIG DS 8 1 86400 20181210050000 20181127040000 2134 . MD7BIr97UWfePzWdROfWCXYYKIss1w7PDNDGlkB9H6AGqVL2yZEXiGL9 bKVJEQ7TSC4oZpDjlB0OmlzZHAvui0dS1fF0ZK3R1kqc7svzmCOZIgS9 Lnvp2h6o9S4g7IywaWBltffxdCrZqjtneF+9W0TAmzGKI3GE81PpLbRQ WWxoTcz/wfUTgMWgdTNnjQE/b1I8Bab0nZdSbcdVmy4dl45zqg7lXKPB wtluM8BS73Ejjk4Ss3tai/u/TQhVwLK+b9ZhXYXUBHcruB4M1AOicaw0 uKF/T5K73IRTG2PNYv2vFNV07LD1hTkK7FdHn4QA2MeWQdYF17fS0PVQ /1MNog==
;; Received 804 bytes from 192.58.128.30#53(j.root-servers.net) in 35 ms
skyao.io. 86400 IN NS f1g1ns1.dnspod.net.
skyao.io. 86400 IN NS f1g1ns2.dnspod.net.
2iui5t1khct6c5o8i2i67rppatgvegqo.io. 900 IN NSEC3 1 1 1 D399EAAB 2J0BQLE6LOOPHCJAUAVJJVSTG25U7I13 NS SOA RRSIG DNSKEY NSEC3PARAM
2iui5t1khct6c5o8i2i67rppatgvegqo.io. 900 IN RRSIG NSEC3 8 2 900 20181218104621 20181127094621 17116 io. mW9xTV3iv5BBocYpA8o6zYp3pYw7Hk5vMCqaXVL7W2+hXZrOxw27vkoy /PiPauXzoByCFx7mY7uA1iG8NTQH3d1HGo2/Js7naepn9dXjxn+LODPw HBtsm94ad83F2B7M9Sz/6Y+6C+L4fkkS7xsIzFGBREFPE90khutCmqa9 6s4=
2dgrnhfe6c36eout03irkmjqepkqj7ro.io. 900 IN NSEC3 1 1 1 D399EAAB 2DHMP300B8T8G9PKLNU3UG0SSUH0BA2C NS DS RRSIG
2dgrnhfe6c36eout03irkmjqepkqj7ro.io. 900 IN RRSIG NSEC3 8 2 900 20181216151728 20181125141728 17116 io. c6xUgTIcrPtkui/IWwsBXvtdRB2iEVslOWXi3ETybrVTtT8lhkirF1Ax AH+wWl4K6hgOnTy0e+h8Q6CeW5jSAGJMbWRUxWUbTgHJIxpvNupflVZr +4IKo3ZwAPbchfU01cKiEMctpmuYLYgE0+Nt/PZZwkdbANvLrp0FwhuU fuQ=
;; Received 582 bytes from 65.22.161.17#53(b0.nic.io) in 152 ms
skyao.io. 600 IN A 119.28.182.148
skyao.io. 86400 IN NS f1g1ns2.dnspod.net.
skyao.io. 86400 IN NS f1g1ns1.dnspod.net.
;; Received 117 bytes from 58.247.212.36#53(f1g1ns1.dnspod.net) in 73 ms
可以加一个 +short
来简化输出:
$ dig skyao.io +trace +short
NS e.root-servers.net. from server 10.243.28.52 in 0 ms.
......
NS d.root-servers.net. from server 10.243.28.52 in 0 ms.
A 119.28.182.148 from server 14.215.155.170 in 16 ms.
最简洁的方式:
dig skyao.io +short
119.28.182.148