在OpenWrt上,正确配置dns,可以提高上网的效率。另外,一些插件,比如访问国外网站,以及adguardhome等,也依赖dns的配置。对本文介绍一些基本的dns的知识,以及结合实际配置,看看配置不同的dns,会对上网行为产生怎么样的影响。
后面是一些dns基本的知识以及验证的过程。
为了辨识和宣传网络上的各种服务,我们一般通过「域名」来识别他们。比如baidu.com、jd.com、douyin.com等都属于域名。然后过域名系统(DNS,Domain Name System)来将域名和IP地址相互映射,使人更方便地访问互联网,而不用去记住能够被机器直接读取的IP地址数串。换句话说,如果你的网络里面没有DNS,那么即使你连接到互联网上,也访问不了各种服务。
前面说了,如果你的机器没有用dns,那么即使具备网络连接,很多网络服务也访问不了。
在windows系统下,可以在命令行使用ipconfig /all
查看对应的dns是这么
以太网适配器 以太网:
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Intel(R) Ethernet Controller (3) I225-V
物理地址. . . . . . . . . . . . . : 48-F3-17-14-24-CB
DHCP 已启用 . . . . . . . . . . . : 是
自动配置已启用. . . . . . . . . . : 是
本地链接 IPv6 地址. . . . . . . . : fe80::ec96:623a:b22b:711%8(首选)
IPv4 地址 . . . . . . . . . . . . : 172.25.17.252(首选)
子网掩码 . . . . . . . . . . . . : 255.255.255.0
获得租约的时间 . . . . . . . . . : 2024年10月8日 8:46:47
租约过期的时间 . . . . . . . . . : 2024年10月9日 8:46:47
默认网关. . . . . . . . . . . . . : 172.25.17.254
DHCP 服务器 . . . . . . . . . . . : 172.25.17.254
DHCPv6 IAID . . . . . . . . . . . : 340325143
DHCPv6 客户端 DUID . . . . . . . : 00-01-00-01-2D-26-E3-1B-48-F3-17-13-24-CB
DNS 服务器 . . . . . . . . . . . : 61.139.2.69
TCPIP 上的 NetBIOS . . . . . . . : 已启用
如上,倒数第二个,就是对应的dns服务器。
linux下,需要需要手动指定对应的dns server,编辑/etc/resolv.conf
文件,按照格式填写dns server
root@localhost:~# cat /etc/resolv.conf
search lan
nameserver 127.0.0.1
nameserver ::1
上面的nameserver
部分就是dns的地址。
整个查询过程大概是这样的:
这中间有几个细节,结合大家上网的情况,来解释下:
熟悉OpenWrt的,可能知道在配置过程中,一共有如下几个可以配置dns的地方。
这里其实dnsmsq的配置,是OpenWrt作为路由器提供dns服务的组件。
lan口和wan口都在高级设置里面,有一个自定义dns服务器。
下面结合具体配置来验证这些dns配置的作用。「【测试组网】」
路由器的wan口dhcp上网 客户机使用ubunut,采用NetworkManager来自动获取ip和dns信息。
「【测试方法】」在客户机上和路由器本身,使用nslookup发起一个域名查询(为了便于查看转发的路径,我们用一个找不到的域名,客户机用:zhaobudao.domain,路由器上用zhaobudao.com)。为了避免缓存干扰,每次测试完成,重启路由器和客户机。
【场景1】 什么都不配做,每一个地方都按照默认
这个时候,dns是自动获取的,我的wan口是dhcp方式上网,所以dns获取到的是上一级的网络提供的dns服务器。
此时dns信息,可以查看openwrt上/tmp/resolv.conf.d/resolv.conf.auto
文件,这个文件是自动生成的,可以看到dns是上一级网络的dns服务器
root@localhost:/tmp# cat /tmp/resolv.conf.d/resolv.conf.auto
# Interface WAN
nameserver 172.23.32.1
search .
这个时候,从客户机上看到的配置的dns信息如下,这个时候dns的信息是NetworkMnager自动下发的,根据路由器宣告的信息。这里可以看到地址是路由器的地址,其实是路由器上的dnsmasq负责。
root@ubuntu-host:~# cat /etc/resolv.conf
# Generated by NetworkManager
search lan
nameserver 10.20.30.6
nameserver fe80::215:5dff:fe12:910a%eth0
在路由器的日志中,可以看到,分别收到了 127.0.0.1(本身)和10.20.30.113(客户机)的域名请求,同时这个请求都被forwarded到172.23.32.1(路由器自动获取到的dns服务器,即路由器的上一级dns服务器。)
root@localhost:/tmp# cat message|grep zhaobudao
Wed Oct 9 02:09:27 2024 daemon.info dnsmasq[1]: 907 127.0.0.1/36195 query[A] zhaobudao.com from 127.0.0.1
Wed Oct 9 02:09:27 2024 daemon.info dnsmasq[1]: 907 127.0.0.1/36195 forwarded zhaobudao.com to 172.23.32.1
Wed Oct 9 02:09:27 2024 daemon.info dnsmasq[1]: 908 127.0.0.1/36195 query[AAAA] zhaobudao.com from 127.0.0.1
Wed Oct 9 02:09:27 2024 daemon.info dnsmasq[1]: 908 127.0.0.1/36195 forwarded zhaobudao.com to 172.23.32.1
...
root@localhost:/tmp# cat message|grep zhaobudao.domain
Wed Oct 9 02:10:31 2024 daemon.info dnsmasq[1]: 985 10.20.30.113/51707 query[A] zhaobudao.domain from 10.20.30.113
Wed Oct 9 02:10:31 2024 daemon.info dnsmasq[1]: 985 10.20.30.113/51707 forwarded zhaobudao.domain to 172.23.32.1
...
【场景2】只配置wan口的自定义dns
这里wan口取消自动获取,改成手动获取,我们填写阿里的公共dns,223.5.5.5.
保存之后看到resolv.conf.auto
文件已经是223.5.5.5了
root@localhost:/tmp# cat /tmp/resolv.conf.d/resolv.conf.auto
# Interface WAN
nameserver 223.5.5.5
然后在客户机和路由器上发起域名查询,然后看日志
# 路由器是请求到127.0.0.1 (dnsmasq)上去应答的
root@localhost:/tmp# nslookup zhaobudao.com
Server: 127.0.0.1
Address: 127.0.0.1:53
Non-authoritative answer:
Non-authoritative answer:
# 客户机上是请求到路由器上(dnsmasq)去应答的
root@ubuntu-host:~# nslookup zhaobudao.domain
Server: 10.20.30.6
Address: 10.20.30.6#53
** server can't find zhaobudao.domain: NXDOMAIN
日志中看到所有请求到forward到223.5.5.5了
root@localhost:/tmp# cat message|grep zhaobudao
Wed Oct 9 02:31:31 2024 daemon.info dnsmasq[1]: 1602 127.0.0.1/58081 query[A] zhaobudao.com from 127.0.0.1
Wed Oct 9 02:31:31 2024 daemon.info dnsmasq[1]: 1602 127.0.0.1/58081 forwarded zhaobudao.com to 223.5.5.5
Wed Oct 9 02:31:31 2024 daemon.info dnsmasq[1]: 1603 127.0.0.1/58081 query[AAAA] zhaobudao.com from 127.0.0.1
Wed Oct 9 02:31:31 2024 daemon.info dnsmasq[1]: 1603 127.0.0.1/58081 forwarded zhaobudao.com to 223.5.5.5
Wed Oct 9 02:31:33 2024 daemon.info dnsmasq[1]: 1603 127.0.0.1/58081 reply zhaobudao.com is NODATA-IPv6
Wed Oct 9 02:31:33 2024 daemon.info dnsmasq[1]: 1602 127.0.0.1/58081 reply zhaobudao.com is NODATA-IPv4
Wed Oct 9 02:31:35 2024 daemon.info dnsmasq[1]: 1604 10.20.30.113/36733 query[A] zhaobudao.domain from 10.20.30.113
Wed Oct 9 02:31:35 2024 daemon.info dnsmasq[1]: 1604 10.20.30.113/36733 forwarded zhaobudao.domain to 223.5.5.5
Wed Oct 9 02:31:36 2024 daemon.info dnsmasq[1]: 1604 10.20.30.113/36733 reply zhaobudao.domain is NXDOMAIN
root@localhost:/tmp#
【场景3】 只配置lan口的自定义dns
配置lan口的时候,我们把wan口的自定义和自动配置去掉,然后lan口自定义一个,这里为了区分,我们用腾讯的公共dns 129.29.29.29
此时,可以看到resolv.conf.auto
文件信息如下
root@localhost:/tmp# cat resolv.conf.d/resolv.conf.auto
# Interface WAN
# Interface lan
nameserver 119.29.29.29
客户机和路由器上同时请求域名,日志如下
root@localhost:/tmp# cat message|grep zhaobudao
Wed Oct 9 02:43:57 2024 daemon.info dnsmasq[1]: 1775 127.0.0.1/56587 query[A] zhaobudao.com from 127.0.0.1
Wed Oct 9 02:43:57 2024 daemon.info dnsmasq[1]: 1775 127.0.0.1/56587 forwarded zhaobudao.com to 119.29.29.29
Wed Oct 9 02:43:57 2024 daemon.info dnsmasq[1]: 1776 127.0.0.1/56587 query[AAAA] zhaobudao.com from 127.0.0.1
Wed Oct 9 02:43:57 2024 daemon.info dnsmasq[1]: 1776 127.0.0.1/56587 forwarded zhaobudao.com to 119.29.29.29
Wed Oct 9 02:43:58 2024 daemon.info dnsmasq[1]: 1776 127.0.0.1/56587 reply zhaobudao.com is NODATA-IPv6
Wed Oct 9 02:43:58 2024 daemon.info dnsmasq[1]: 1775 127.0.0.1/56587 reply zhaobudao.com is NODATA-IPv4
Wed Oct 9 02:44:01 2024 daemon.info dnsmasq[1]: 1777 10.20.30.113/36640 query[A] zhaobudao.domain from 10.20.30.113
Wed Oct 9 02:44:01 2024 daemon.info dnsmasq[1]: 1777 10.20.30.113/36640 forwarded zhaobudao.domain to 119.29.29.29
Wed Oct 9 02:44:01 2024 daemon.info dnsmasq[1]: 1777 10.20.30.113/36640 reply zhaobudao.domain is NXDOMAIN
root@localhost:/tmp#
发现,请求被forward到119.29.29.29去了。
【场景4】lan和wan都配置自定义dns
这里为了区分,还是将lan口的用119.29.29.29, wan口的用223.5.5.5 ,不过这里有一个「权重」, 参考下面的配置
两个dns都会写到resolv.conf.auto
文件中,权重越小,优先级越高,所以lan口权重是100,小于wan口的200,优先级高,所以lan口在wan的前面。
root@localhost:/tmp# cat resolv.conf.d/resolv.conf.auto
# Interface lan
nameserver 119.29.29.29
# Interface WAN
nameserver 223.5.5.5
root@localhost:/tmp#
此时日志如下
root@localhost:/tmp# cat message |grep zhaobudao
Wed Oct 9 02:58:24 2024 daemon.info dnsmasq[1]: 6 10.20.30.113/37640 query[A] zhaobudao.domain from 10.20.30.113
Wed Oct 9 02:58:24 2024 daemon.info dnsmasq[1]: 6 10.20.30.113/37640 forwarded zhaobudao.domain to 119.29.29.29
Wed Oct 9 02:58:24 2024 daemon.info dnsmasq[1]: 6 10.20.30.113/37640 forwarded zhaobudao.domain to 223.5.5.5
Wed Oct 9 02:58:24 2024 daemon.info dnsmasq[1]: 6 10.20.30.113/37640 reply zhaobudao.domain is NXDOMAIN
Wed Oct 9 02:58:43 2024 daemon.info dnsmasq[1]: 7 127.0.0.1/54050 query[A] zhaobudao.com from 127.0.0.1
Wed Oct 9 02:58:43 2024 daemon.info dnsmasq[1]: 7 127.0.0.1/54050 forwarded zhaobudao.com to 223.5.5.5
Wed Oct 9 02:58:43 2024 daemon.info dnsmasq[1]: 8 127.0.0.1/54050 query[AAAA] zhaobudao.com from 127.0.0.1
Wed Oct 9 02:58:43 2024 daemon.info dnsmasq[1]: 8 127.0.0.1/54050 forwarded zhaobudao.com to 223.5.5.5
Wed Oct 9 02:58:43 2024 daemon.info dnsmasq[1]: 7 127.0.0.1/54050 reply zhaobudao.com is NODATA-IPv4
Wed Oct 9 02:58:43 2024 daemon.info dnsmasq[1]: 8 127.0.0.1/54050 reply zhaobudao.com is NODATA-IPv6
root@localhost:/tmp#
「可以看到,客户机的请求,会同时转到lan和wan配置的自定义dns,顺序按照配置的权重来转发。而路由器本身的请求,则直接转到wan口对应的dns,不会转到lan口的dns。」
【场景5】lan口dhcp通告某一个特有的dns配置
此时保留上面第四步里面lan和wan的自定义dns,然后在lan的dhcp里面配置dhcp附加选项,如下:
为了和前面的区分,这里用了8.8.8.8(google)和180.76.76.76(baidu)的 此时路由器上resolv.conf.auto
和原来一样,而这个时候,客户机的dns变成了我们配置的地址
# 客户机
root@ubuntu-host:~# cat /etc/resolv.conf
# Generated by NetworkManager
search lan
nameserver 8.8.8.8
nameserver 180.76.76.76
nameserver fe80::215:5dff:fe12:910a%eth0
root@ubuntu-host:~#
# 路由器
root@localhost:~# cat /tmp/resolv.conf.d/resolv.conf.auto
# Interface lan
nameserver 119.29.29.29
# Interface WAN
nameserver 223.5.5.5
这个时候,查询刚刚的域名,可以看到客户机是由我们配置的8.8.8.8来应答的,路由器还是还是由本地的dnsmasq应答。
# 客户机上
root@ubuntu-host:~# nslookup zhaobudao.domain
Server: 8.8.8.8
Address: 8.8.8.8#53
** server can't find zhaobudao.domain: NXDOMAIN
# 路由器上
root@localhost:~# nslookup zhaobudao.com
Server: 127.0.0.1
Address: 127.0.0.1:53
Non-authoritative answer:
Non-authoritative answer:
在路由器的日志中,只收到路由器本身的域名请求
root@localhost:~# cat /tmp/message |grep zhaobudao
Wed Oct 9 03:10:28 2024 daemon.info dnsmasq[1]: 15 127.0.0.1/35241 query[A] zhaobudao.com from 127.0.0.1
Wed Oct 9 03:10:28 2024 daemon.info dnsmasq[1]: 15 127.0.0.1/35241 forwarded zhaobudao.com to 223.5.5.5
Wed Oct 9 03:10:28 2024 daemon.info dnsmasq[1]: 16 127.0.0.1/35241 query[AAAA] zhaobudao.com from 127.0.0.1
Wed Oct 9 03:10:28 2024 daemon.info dnsmasq[1]: 16 127.0.0.1/35241 forwarded zhaobudao.com to 223.5.5.5
Wed Oct 9 03:10:28 2024 daemon.info dnsmasq[1]: 16 127.0.0.1/35241 reply zhaobudao.com is NODATA-IPv6
Wed Oct 9 03:10:28 2024 daemon.info dnsmasq[1]: 15 127.0.0.1/35241 reply zhaobudao.com is NODATA-IPv4
root@localhost:~#
【场景6】DHCP/DNS里面配置
这里主要是dnsmasq服务本身的配置,包括服务端口,缓存大小,并发数,日志、静态域名等。主要我没有自己的dns上游,其他的dns上游,还不如前面选的几个公共dns,所以这里就不单独验证了。
网上关于openwrt的dns,有很多文章,不过说的都云里雾里的,很多都是一篇文章到处转载,所以自己动手做了这篇验证,可能不一定完整,但是我觉得把几个dns都说清楚了。如果大家有什么疑问,可以留言讨论,如果觉得本文对你有帮助,欢迎点赞收藏。