首页·终端命令

dig

网络

用法

dig [@server] <name> [type]

直接向 DNS 服务器提问并原样显示记录;它绕过 /etc/hosts 和系统缓存,所以结果可能和浏览器看到的不一样,有些发行版还要先装 dnsutils 或 bind-utils。

常用选项

选项含义
+shortJust the answer, one record per line.
@1.1.1.1Ask a specific resolver instead of the system one.
A / AAAA / MX / TXT / NS / CNAMEThe record type goes last, with no dash.
+noall +answerTrim the output to the answer section only.
-x <ip>Reverse lookup, address to name.
+traceWalk down from the root servers, which shows where a delegation breaks.
+ttlunitsPrint TTLs as 1h30m instead of raw seconds.

示例

dig +short example.com

The A records, nothing else.

dig @1.1.1.1 example.com MX +noall +answer

Mail records straight from a public resolver.

dig -x 8.8.8.8 +short

Which name that address claims.

连不上的时候,这些能把范围缩小——是名字解析不了、没有路由,还是端口被挡。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. dig 是做什么的?

直接向 DNS 服务器提问并原样显示记录;它绕过 /etc/hosts 和系统缓存,所以结果可能和浏览器看到的不一样,有些发行版还要先装 dnsutils 或 bind-utils。

Q. 怎么写?

dig [@server] <name> [type] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 7 个,完整列表在 man dig。这条命令属于网络。

相关命令

man 手册: man dig