在 Linux 上使用 dig 命令解析 DNS 域名
在此页面上
- 使用 dig 命令解析域名
- 如何使用 dig 查询本地 DNS 服务器
- 使用 dig 获取邮件服务器名称
- 获取 dig 命令的更多选项
- 获取域的名称服务器
- 使用dig命令获取域名的IP地址
Linux 中的 dig 命令是“Domain Information Groper”的缩写,是一种用于查询 DNS(域名系统) 服务器的网络管理工具。它允许用户检索有关域名的信息,例如 IP 地址、邮件服务器(MX 记录)和其他 DNS 资源记录。 dig 主要用于解决 DNS 问题,提供详细的输出,包括查询的响应时间和提供该信息的 DNS 服务器。与 nslookup 等更基本的命令相比,它的灵活性和精确性使其成为网络管理员诊断 DNS 问题、测试域配置和执行 DNS 查找的首选工具。
使用 dig 命令解析域名
解析域名 example.com:
dig example.com
如果可以解析域名,结果将类似于以下内容:
root@server1:~# dig linux教程
; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> linux教程
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50297
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;linux教程. IN A
;; ANSWER SECTION:
linux教程. 299 IN A 104.24.0.68
linux教程. 299 IN A 104.24.1.68
;; Query time: 233 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Aug 08 12:35:15 CEST 2024
;; MSG SIZE rcvd: 75
如何使用 dig 查询本地 DNS 服务器
要检查本地网络服务器是否负责该域,请使用以下命令:
dig @localhost example.com
使用 dig 获取邮件服务器名称
如果你想知道哪个邮件服务器负责处理 example.com 的邮件,可以使用以下命令:
dig MX example.com
结果将与此类似:
root@server1:~# dig MX linux教程
; <<>> DiG 9.11.3-1ubuntu1.1-Ubuntu <<>> MX linux教程
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 14115
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;linux教程. IN MX
;; ANSWER SECTION:
linux教程. 299 IN MX 10 mail.linux教程.
;; Query time: 205 msec
;; SERVER: 8.8.4.4#53(8.8.4.4)
;; WHEN: Wed Aug 08 10:36:30 CEST 2024
;; MSG SIZE rcvd: 64
获取 dig 命令的更多选项
要获取 dig 命令的所有可用选项,您可以运行:
dig --help
您可以在 dig 命令的联机帮助页中找到更详细的描述:
man dig
以下是该工具手册页的摘录,它非常清楚地说明了如何使用 dig:
A typical invocation of dig looks like:
dig @server name type
where:
server
is the name or IP address of the name server to query. This can be an IPv4 address in
dotted-decimal notation or an IPv6 address in colon-delimited notation. When the supplied
server argument is a hostname, dig resolves that name before querying that name server.
If no server argument is provided, dig consults /etc/resolv.conf; if an address is found
there, it queries the name server at that address. If either of the -4 or -6 options are in
use, then only addresses for the corresponding transport will be tried. If no usable addresses
are found, dig will send the query to the local host. The reply from the name server that
responds is displayed.
name
is the name of the resource record that is to be looked up.
type
indicates what type of query is required — ANY, A, MX, SIG, etc. type can be any valid query
type. If no type argument is supplied, dig will perform a lookup for an A record.
如果需要,您可以强制 dig 命令从输出显示中删除某些部分。例如,要在返回答案时切换查询的输出或问题部分中注释行的显示,请使用 +nocomments 和 +noquestion 选项。
以下屏幕截图显示了这些选项的实际效果:
获取域的名称服务器
接下来,您可以通过 dig 命令查询像 MX 记录一样的 NS 记录。您可以这样做:
挖掘 NS example.com
以下是该命令在我们的案例中产生的输出:
使用dig命令获取域名的IP地址
如果您希望 dig 仅显示服务器的 IP 地址,请使用 +short 选项。
例如:
挖 example.com +short
如果您想一次性查询多个网站/服务器,您可以在文件中创建名称列表,并将该文件的名称作为输入传递给 dig 命令的 -f 选项。
-f file
Batch mode: dig reads a list of lookup requests to process from the given file. Each line in the
file should be organized in the same way they would be presented as queries to dig using the
command-line interface.
这是一个例子: