$ cat /etc/resolv.conf # Dynamic resolv.conf(5) filefor glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILEBY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 10.0.2.2 search home
root@linuxdns1:~# cat /etc/resolv.conf # Dynamic resolv.conf(5) filefor glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILEBY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 127.0.0.1 search home
在目前的情况下,所有的 DNS 查询都会使用 127.0.0.1:53 这个 DNS 服务器,下一步会发生什么呢?
我再次查看 /var/run 目录,可以发现一个线索:resolvconf 目录下 resolv.conf 文件中的配置也相应变更,变更为 dnsmasq 对应的 DNS 服务器:
1 2 3 4 5
$ cat /var/run/resolvconf/resolv.conf # Dynamic resolv.conf(5) filefor glibc resolver(3) generated by resolvconf(8) # DO NOT EDIT THIS FILEBY HAND -- YOUR CHANGES WILL BE OVERWRITTEN nameserver 127.0.0.1 search home
Jul 319:56:07 ubuntu-xenial dnsmasq[15372]: query[A] bbc.co.uk from 127.0.0.1 [...] forwarded bbc.co.uk to 10.0.2.2 [...] reply bbc.co.uk is 151.101.192.81 [...] reply bbc.co.uk is 151.101.0.81 [...] reply bbc.co.uk is 151.101.64.81 [...] reply bbc.co.uk is 151.101.128.81 [...] query[PTR]81.192.101.151.in-addr.arpa from 127.0.0.1 [...] forwarded 81.192.101.151.in-addr.arpa to 10.0.2.2 [...] reply 151.101.192.81 is NXDOMAIN
可以清晰看出 dnsmasq 收到的查询、查询被转发到了哪里以及收到的回复。
如果查询被缓存命中(或者说,本地的查询结果还在 存活时间 TTL 内,并未过期),日志显示如下:
1 2 3 4 5 6
[...] query[A] bbc.co.uk from 127.0.0.1 [...] cached bbc.co.uk is 151.101.64.81 [...] cached bbc.co.uk is 151.101.128.81 [...] cached bbc.co.uk is 151.101.192.81 [...] cached bbc.co.uk is 151.101.0.81 [...] query[PTR]81.64.101.151.in-addr.arpa from 127.0.0.1
如果你想了解缓存中有哪些记录,可以向 dnsmasq 进程 id 发送 USR1 信号,这样 dnsmasq 会将缓存记录导出并写入到相同的日志文件中: