dig和nslookup命令

-- TOC --

dig和nslookip命令,都是Linux上常用的域名查询工具,Win系统内只有nslookup可用。

dig

用dig命令查询一个域名:

$ dig www.maixj.net

; <<>> DiG 9.16.1-Ubuntu <<>> www.maixj.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22417
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;www.maixj.net.         IN  A

;; ANSWER SECTION:
www.maixj.net.      5   IN  A   114.215.183.12

;; Query time: 55 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN:  2月 07 09:11:21 CST 2022
;; MSG SIZE  rcvd: 58

指定DNS Server查询:

$ dig @8.8.8.8 www.maixj.net

; <<>> DiG 9.16.1-Ubuntu <<>> @8.8.8.8 www.maixj.net
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59726
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.maixj.net.         IN  A

;; ANSWER SECTION:
www.maixj.net.      21600   IN  A   114.215.183.12

;; Query time: 239 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN:  2月 07 09:15:34 CST 2022
;; MSG SIZE  rcvd: 58

8.8.8.8是Google提供的域名服务器!

dig命令默认查询域名的A记录

查询域名的MX记录

$ dig qq.com MX

; <<>> DiG 9.16.1-Ubuntu <<>> qq.com MX
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63939
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;qq.com.                IN  MX

;; ANSWER SECTION:
qq.com.         5   IN  MX  30 mx1.qq.com.
qq.com.         5   IN  MX  10 mx3.qq.com.
qq.com.         5   IN  MX  20 mx2.qq.com.

;; Query time: 52 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN:  2月 07 09:17:50 CST 2022
;; MSG SIZE  rcvd: 95

查询域名的NS记录

$ dig domain NS

查询域名的其它记录类似!

跟踪域名解析的全过程:

$ dig +trace pynote.net

; <<>> DiG 9.16.1-Ubuntu <<>> +trace pynote.net
;; global options: +cmd
.           5   IN  NS  m.root-servers.net.
.           5   IN  NS  b.root-servers.net.
.           5   IN  NS  c.root-servers.net.
.           5   IN  NS  d.root-servers.net.
.           5   IN  NS  e.root-servers.net.
.           5   IN  NS  f.root-servers.net.
.           5   IN  NS  g.root-servers.net.
.           5   IN  NS  h.root-servers.net.
.           5   IN  NS  a.root-servers.net.
.           5   IN  NS  i.root-servers.net.
.           5   IN  NS  j.root-servers.net.
.           5   IN  NS  k.root-servers.net.
.           5   IN  NS  l.root-servers.net.
;; Received 262 bytes from 127.0.0.53#53(127.0.0.53) in 48 ms

net.            172800  IN  NS  m.gtld-servers.net.
net.            172800  IN  NS  e.gtld-servers.net.
net.            172800  IN  NS  g.gtld-servers.net.
net.            172800  IN  NS  c.gtld-servers.net.
net.            172800  IN  NS  a.gtld-servers.net.
net.            172800  IN  NS  b.gtld-servers.net.
net.            172800  IN  NS  f.gtld-servers.net.
net.            172800  IN  NS  i.gtld-servers.net.
net.            172800  IN  NS  j.gtld-servers.net.
net.            172800  IN  NS  k.gtld-servers.net.
net.            172800  IN  NS  d.gtld-servers.net.
net.            172800  IN  NS  h.gtld-servers.net.
net.            172800  IN  NS  l.gtld-servers.net.
net.            86400   IN  DS  35886 8 2 7862B27F5F516EBE19680444D4CE5E762981931842C465F00236401D 8BD973EE
net.            86400   IN  RRSIG   DS 8 1 86400 20220219170000 20220206160000 9799 . M6ELz1E3seGJ0HwpcUADuFQ+6HK9ewtI0TUzw9e59HIbc9lNgYS2krRW G/QPppQgN8klstwcJCvFxCXAWfBxw3y/a5PPR7sgvIH80bzYodJydMpD k+8C5CxfAoVsE/V2U66NcsYbO1oeEGFIFQ7aOm1GKeSu65Sg69i3SGZK w/e9B4hLJCkKjY2jq9Fe8v3xggedCX1mtn8J/jdhGhx5e6GraZ6HUYf/ UG34Zr2RMXd3TyERZCgsLV4/JA+gLpmpkaAvMXbCTIs5SNfVDtCZPY3l qtX3Imp3NXD0rl69w8u2I0/1aw23Lv40rJiN+88opudnxgqXhiVh5CXP JKRxOA==
;; Received 1195 bytes from 192.33.4.12#53(c.root-servers.net) in 168 ms

pynote.net.     172800  IN  NS  ns1.alidns.com.
pynote.net.     172800  IN  NS  ns2.alidns.com.
A1RT98BS5QGC9NFI51S9HCI47ULJG6JH.net. 86400 IN NSEC3 1 1 0 - A1RTLNPGULOGN7B9A62SHJE1U3TTP8DR NS SOA RRSIG DNSKEY NSEC3PARAM
A1RT98BS5QGC9NFI51S9HCI47ULJG6JH.net. 86400 IN RRSIG NSEC3 8 2 86400 20220212064845 20220205053845 4604 net. ta23bIc3dUVSf2VKeQlku8H6gHjFrJUEfOQyubD+Mg4pGwybcZ9LNeaE Yzxt1XQ/09EIciByZqEWDpqxC7m0W45dg2in62sEF62g9OKf9LPUG6dX +AlC5u0WsImxVeMQX6LJj/eM+Zv4LiIKNo2mAlIZoQYQUzvtgEHFfR4p 1y3V/va78uQ2FFMmLq0Yc3FVC63Y0skRioJROPsVOVWQWQ==
3TIT80RVCQV0U3NM4ATC327DCOOJ3KBU.net. 86400 IN NSEC3 1 1 0 - 3TIUMOHH2CMO7HH88H6MV0LUKJBRJFUT NS DS RRSIG
3TIT80RVCQV0U3NM4ATC327DCOOJ3KBU.net. 86400 IN RRSIG NSEC3 8 2 86400 20220211064223 20220204053223 4604 net. AfheAXQ9MdIaaIx5rPtBJ8kpUpB/VyVQjck0GoAZBFps4Y4rA0VRoLBR UCeDv4Un9VjzYUrhGFRMAwURstXi7ulNDt8IUjVrAsui3S0bUT5IdQg5 ox6URB8XAtdd8fiFS5N5C1jY0KCltm7VtPtwMbkqSav+mn17K/63DWWf 50sdjNQV+WLhqdofWl04YA6/wsPX6LgSHBZJGNXNUSNe4A==
;; Received 634 bytes from 192.54.112.30#53(h.gtld-servers.net) in 220 ms

pynote.net.     86400   IN  A   148.72.212.244
;; Received 55 bytes from 106.11.141.112#53(ns2.alidns.com) in 32 ms

nslookup

如下示例,基本上一看就明白:

$ nslookup www.maixj.net
Server:     127.0.0.53
Address:    127.0.0.53#53

Non-authoritative answer:
Name:   www.maixj.net
Address: 114.215.183.12

$ nslookup www.maixj.net 8.8.8.8
Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
Name:   www.maixj.net
Address: 114.215.183.12

Linux下配置NameServer

/etc/resolv.conf

nameserver指定了DNS服务器的IP地址。修改此文件,保存后立即生效。

可以有很多行的nameserver,每一个带一个IP地址。在查询时就按nameserver在本文件中的顺序进行,且只有当第一个nameserver没有反应时才查询下面的nameserver。

Linux配置静态域名解析

修改/etc/hosts

DNS查询会先查这个文件,如果域名在此文件中有记录,就不会在走网络查询了。

本文链接:https://cs.pynote.net/sf/linux/shell/202202071/

-- EOF --

-- MORE --