git clone git://thekelleys.org.uk/dnsmasq.git
编译安装
cd dnsmasq
make install
编译生成可执行文件dnsmasq,拷贝到设备上,然后手动创建配置文件/etc/dnsmasq.conf。
配置启动
user=root # 程序因为需要绑定特权级别的53端口,所以启动时需要root权限,后续切换到普通用户,此处提供普通用户
group=root # 程序因为需要绑定特权级别的53端口,所以启动时需要root权限,后续切换到普通用户,此处提供普通用户组
no-dhcp-interface==wlan0 # 指定监听的网卡,此处关闭dhcp功能
listen-address=127.0.0.1 # 监听地址
no-hosts # 不使用/etc/hosts文件
no-resolv # 不解析/etc/resolv.conf文件
no-poll # 不监听/etc/resolv.conf文件变化
strict-order # 严格按照设置的DNS服务器顺序,逐个去访问。此处是个非常关键的参数,不加会导致收不到上游DNS的回应,原因未知。
cache-size=1000 # 缓存DNS的条数
server=8.8.8.8 # 指定上游DNS服务器
server=8.8.4.4 # 指定上游DNS服务器
server=114.114.114.114 # 指定上游DNS服务器
log-queries # 记录日志
log-facility=/var/dnsmasq.log # 日志保存位置
测试功能
启动服务进程
./dnsmasq -i wlan0 --max-cache-ttl=600 & # 监听wlan0端口,并设置缓存生命周期为600秒
ping baidu.com测试
[root@Meari /mnt/mmc01]$ ping baidu.com
PING baidu.com (110.242.68.66): 56 data bytes
64 bytes from 110.242.68.66: seq=0 ttl=49 time=95.151 ms
64 bytes from 110.242.68.66: seq=1 ttl=49 time=116.884 ms
64 bytes from 110.242.68.66: seq=2 ttl=49 time=130.659 ms
^C
--- baidu.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 95.151/114.231/130.659 ms
[root@Meari /mnt/mmc01]$
[root@Meari /mnt/mmc01]$ ping baidu.com
PING baidu.com (110.242.68.66): 56 data bytes
64 bytes from 110.242.68.66: seq=0 ttl=49 time=139.305 ms
64 bytes from 110.242.68.66: seq=1 ttl=49 time=140.883 ms
^C
--- baidu.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 139.305/140.094/140.883 ms
[root@Meari /mnt/mmc01]$
[root@Meari /mnt/mmc01]$ ping baidu.com
PING baidu.com (39.156.66.10): 56 data bytes
64 bytes from 39.156.66.10: seq=0 ttl=51 time=105.012 ms
64 bytes from 39.156.66.10: seq=1 ttl=51 time=39.607 ms
^C
--- baidu.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 39.607/72.309/105.012 ms
[root@Meari /mnt/mmc01]$
dnsmasq进程log如下,可以看到,第一次ping,服务器将DNS请求转发给了8.8.8.8,后面两次ping命令都是从cached中获取的IP地址,证明DNS缓存起作用了。大大加快了DNS解析速度。
[root@Meari /mnt/mmc01]$ cat /var/dnsmasq.log
Jan 1 00:58:23 dnsmasq[963]: started, version 2.90 cachesize 1000
Jan 1 00:58:23 dnsmasq[963]: compile time options: IPv6 GNU-getopt no-DBus no-UBus no-i18n no-IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset no-nftset auth no-cryptohash no-DNSSEC loop-detect inotify dumpfile
Jan 1 00:58:23 dnsmasq[963]: using nameserver 8.8.8.8#53
Jan 1 00:58:23 dnsmasq[963]: using nameserver 8.8.4.4#53
Jan 1 00:58:23 dnsmasq[963]: using nameserver 114.114.114.114#53
Jan 1 00:58:23 dnsmasq[963]: cleared cache
Jan 1 00:58:27 dnsmasq[963]: query[AAAA] baidu.com from 127.0.0.1
Jan 1 00:58:27 dnsmasq[963]: forwarded baidu.com to 8.8.8.8
Jan 1 00:58:27 dnsmasq[963]: reply baidu.com is NODATA-IPv6
Jan 1 00:58:27 dnsmasq[963]: query[A] baidu.com from 127.0.0.1
Jan 1 00:58:27 dnsmasq[963]: forwarded baidu.com to 8.8.8.8
Jan 1 00:58:27 dnsmasq[963]: reply baidu.com is 110.242.68.66
Jan 1 00:58:27 dnsmasq[963]: reply baidu.com is 39.156.66.10
Jan 1 00:58:31 dnsmasq[963]: query[AAAA] baidu.com from 127.0.0.1
Jan 1 00:58:31 dnsmasq[963]: cached baidu.com is NODATA-IPv6
Jan 1 00:58:31 dnsmasq[963]: query[A] baidu.com from 127.0.0.1
Jan 1 00:58:31 dnsmasq[963]: cached baidu.com is 110.242.68.66
Jan 1 00:58:31 dnsmasq[963]: cached baidu.com is 39.156.66.10
Jan 1 00:58:34 dnsmasq[963]: query[AAAA] baidu.com from 127.0.0.1
Jan 1 00:58:34 dnsmasq[963]: cached baidu.com is NODATA-IPv6
Jan 1 00:58:34 dnsmasq[963]: query[A] baidu.com from 127.0.0.1
Jan 1 00:58:34 dnsmasq[963]: cached baidu.com is 39.156.66.10
Jan 1 00:58:34 dnsmasq[963]: cached baidu.com is 110.242.68.66
如下是ping baidu.com过程中的网络数据包,可以看到,也确实只有第一次ping之前执行了DNS请求。
再借助dig工具来直观感受一下速度提升
是不是效果很明显?
我这里设置的DNS缓存有效时间是600s,也就是十分钟内重复解析的域名都将从缓存中快速读取,大大提高了WEB页面的加载速度。
原创文章,作者:速盾高防cdn,如若转载,请注明出处:https://www.sudun.com/ask/93705.html