DNS解析过程

如下图所示,先清空本地DNS解析缓存(windows和linux的清空方法请自行搜索),然后用dig命令查看可知:

本地未发现缓存,直接查询根,根返回`com.`域, 在查询`com.`,返回`baidu.com.`,在查询`baidu.com.`,返回`www.baidu.com` 主机的A记录,即IP地址。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# michael @ m0nst3r in ~ [20:45:48] 
$ dscacheutil -flushcache

# michael @ m0nst3r in ~ [20:46:19]
$ dig +trace www.baidu.com

; <<>> DiG 9.10.6 <<>> +trace www.baidu.com
;; global options: +cmd
. 14138 IN NS g.root-servers.net.
. 14138 IN NS f.root-servers.net.
. 14138 IN NS a.root-servers.net.
. 14138 IN NS e.root-servers.net.
. 14138 IN NS h.root-servers.net.
. 14138 IN NS d.root-servers.net.
. 14138 IN NS k.root-servers.net.
. 14138 IN NS i.root-servers.net.
. 14138 IN NS l.root-servers.net.
. 14138 IN NS c.root-servers.net.
. 14138 IN NS b.root-servers.net.
. 14138 IN NS m.root-servers.net.
. 14138 IN NS j.root-servers.net.
;; Received 695 bytes from 192.168.1.1#53(192.168.1.1) in 5 ms

com. 172800 IN NS a.gtld-servers.net.
com. 172800 IN NS b.gtld-servers.net.
com. 172800 IN NS c.gtld-servers.net.
com. 172800 IN NS d.gtld-servers.net.
com. 172800 IN NS e.gtld-servers.net.
com. 172800 IN NS f.gtld-servers.net.
com. 172800 IN NS g.gtld-servers.net.
com. 172800 IN NS h.gtld-servers.net.
com. 172800 IN NS i.gtld-servers.net.
com. 172800 IN NS j.gtld-servers.net.
com. 172800 IN NS k.gtld-servers.net.
com. 172800 IN NS l.gtld-servers.net.
com. 172800 IN NS m.gtld-servers.net.
com. 86400 IN DS 30909 8 2 E2D3C916F6DEEAC73294E8268FB5885044A833FC5459588F4A9184CF C41A5766
com. 86400 IN RRSIG DS 8 1 86400 20181028050000 20181015040000 2134 . HHrLrltU/Pzku1DAAlxYsgUID+DnKajqKudOxnLwQNFKh2SkkA6le5cF ++eRShsNX7Q+lq2+5E5BMLF8+cm6zGC1VKoQLzIihdts7osyYviPvJ6/ lcCjV2OnF+EvEQhOCnBDhGkF5nmq+LIoEODyu2F51kneQqgWIjHvPvPG GGzMyTXpJC8pKo+VVDxcZf1nBjk6qHnM+dHB8aF0RS9hDupFHD55ePXD 6JIL87fmZW4be/Pe0haPtTVkzDRnY5RK5oQm7d1ryQV6NJB0sQJjSG7m nNLXofugjh5B6wWb8ZX24QYcDxSZvy52BF+DuGzkClRZQfCGDcToZx4y Lj5CGg==
;; Received 1173 bytes from 199.7.91.13#53(d.root-servers.net) in 232 ms

baidu.com. 172800 IN NS dns.baidu.com.
baidu.com. 172800 IN NS ns2.baidu.com.
baidu.com. 172800 IN NS ns3.baidu.com.
baidu.com. 172800 IN NS ns4.baidu.com.
baidu.com. 172800 IN NS ns7.baidu.com.
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 86400 IN NSEC3 1 1 0 - CK0Q1GIN43N1ARRC9OSM6QPQR81H5M9A NS SOA RRSIG DNSKEY NSEC3PARAM
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 86400 IN RRSIG NSEC3 8 2 86400 20181020121530 20181013110530 46475 com. dwbSHWsE9XgYaIlfAvddRHnM+SyareZ63CYwXOcbO3tcu7wpslbjNn37 tbqnZa89rsIKO1mzWUdxM5fQtXXj2k+haz8SV42mtiU9WvkXBx0jnUjn 18UXq+X1u/uLfwPOIIy0tDcFW8OdQB3nE18aCVIzy9YUcrFMilNQ+vP6 OSM=
HPVV2B5N85O7HJJRB7690IB5UVF9O9UA.com. 86400 IN NSEC3 1 1 0 - HPVVP23QUO0FP9R0A04URSICJPESKO9J NS DS RRSIG
HPVV2B5N85O7HJJRB7690IB5UVF9O9UA.com. 86400 IN RRSIG NSEC3 8 2 86400 20181021051422 20181014040422 46475 com. OIF0G5EhzboJ66h6mM+eslGRta4aM5M77zOMwZklsSgSBCpNdHByfteP WDx6PdN6a+QyoZQfjGjc2b8mnUxT+6rypSeHShrQdnPoFa/GtrOeWZS7 LVktINNV9hP/X2qk664VY05Q8SXSK1btMv68QXIznnx6DTZV6R4X2Dk4 mwk=
;; Received 697 bytes from 192.42.93.30#53(g.gtld-servers.net) in 277 ms

www.baidu.com. 1200 IN CNAME www.a.shifen.com.
a.shifen.com. 1200 IN NS ns1.a.shifen.com.
a.shifen.com. 1200 IN NS ns4.a.shifen.com.
a.shifen.com. 1200 IN NS ns5.a.shifen.com.
a.shifen.com. 1200 IN NS ns3.a.shifen.com.
a.shifen.com. 1200 IN NS ns2.a.shifen.com.
;; Received 239 bytes from 61.135.165.235#53(ns2.baidu.com) in 29 ms

完整的流程如下:

1
2
3
4
5
6
7
8
9
10
1. 本机向local dns请求www.baidu.com
2. local dns向根域请求www.baidu.com,根域返回com.域的服务器IP
3. 向com.域请求www.baidu.com,com.域返回baidu.com域的服务器IP
4. 向baidu.com请求www.baidu.com,返回cname www.a.shifen.com和a.shifen.com域的服务器IP
5. 向root域请求www.a.shifen.com
6. 向com.域请求www.a.shife.com
7. 向shifen.com请求
8. 向a.shifen.com域请求
9. 拿到www.a.shifen.com的IP
10. localdns返回本机www.baidu.com cname www.a.shifen.com 以及 www.a.shifen.com的IP

另外一篇文章说的比较明白,向前辈致敬:
https://www.cnblogs.com/liyuanhong/articles/7353974.html