首頁·終端機命令

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