dnsmasq是一款小巧且方便地用于配置DNS服务器和DHCP服务器的工具,适用于小型网络,它提供了DNS解析功能和可选择的DHCP功能。
dnsmasq可以解决小范围的dns查询问题,如果业务是跨机房、跨地区的话不建议使用dnsmasq做为dns解析服务器。
dnsmasq官网如下:
http://www.thekelleys.org.uk/dnsmasq/doc.html
可以看到最新的版本为2.8.0
详细参考here
一般开源软件都可以同步源码安装和yum和apt-get安装两种方式
源码安装dnsmasq,从官网下载安装包
wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.80.tar.gz
安装GCC
yum -y install gcc
解压安装
tar -xf dnsmasq-2.80.tar.gz
cd dnsmasq-2.80
make install
可知,dnsmasq默认安装与/usr/local/sbin中
dnsmasq -v
yum安装
yum -y install dnsmasq
dnsmasq -v
apt-get安装
sudo apt-get -y install dnsmasq
dnsmasq -v
配置正确,使用如下命令即可启动
dnsmasq
dnsmasq配置十分简单,一般设置绑定的网卡(53端口)以及后端的NameServer即可
# 本地监听
listen-address=127.0.0.1
# 或者
# 所有网卡监听
listen-address=0.0.0.0
# resolv配置,跟/etc/resolv.conf类似,配置dns服务器地址
resolv-file=/data/dnsmasq/conf/dnsmasq.resolv.conf
# 开启cache(size上限10000),默认没有开启
cache-size=1000
# 不缓存失败的结果
no-negcache
resolv文件参考:
cat /data/dnsmasq/conf/dnsmasq.resolv.conf
nameserver 114.114.114.114
nameserver 8.8.8.8
其他配置:
# 严格按照resolv-file文件中的顺序从上到下进行DNS解析,直到第一个解析成功为止(默认多个nameserver并发访问获取最快结果)
strict-order
# 过滤网站,将nameserver指向不存在的服务器
address=/double-click.net/127.0.0.1
# 域名劫持,对某个内部域名,使用指定的内部dns解析
server=/localnet/192.168.0.1
# 需要大量特殊化配置时,可以使用dnsmasq.d存放这些配置文件,配置文件以.conf结尾
# 例如,新建/etc/dnsmasq.d/my.conf
# 内容为"server=/localnet/192.168.0.1",是一些内部dns的设置
conf-dir=/etc/dnsmasq.d
查看dnsmasq cache详情,先使用USR1信号dump statistics to the system log.
pkill -USR1 dnsmasq
接着查看系统日志,可以查看详细的cache情况
[root@zero_machine /var/log]# tail -f /var/log/messages
Apr 15 12:59:01 zero_machine nscd: ERR|passwd.c|185|_nss_ldap_getpwnam_r|get nslcd error respond, ul_result=7
Apr 15 12:59:01 zero_machine nscd: ERR|passwd.c|185|_nss_ldap_getpwnam_r|get nslcd error respond, ul_result=7
Apr 15 12:59:01 zero_machine nscd: ERR|passwd.c|185|_nss_ldap_getpwnam_r|get nslcd error respond, ul_result=7
Apr 15 12:59:05 zero_machine dnsmasq[3747]: time 1555304345
Apr 15 12:59:05 zero_machine dnsmasq[3747]: cache size 150, 0/4 cache insertions re-used unexpired cache entries.
Apr 15 12:59:05 zero_machine dnsmasq[3747]: queries forwarded 2, queries answered locally 2
Apr 15 12:59:05 zero_machine dnsmasq[3747]: queries for authoritative zones 0
Apr 15 12:59:05 zero_machine dnsmasq[3747]: server 9.23.140.180#53: queries sent 1, retried or failed 0
Apr 15 12:59:05 zero_machine dnsmasq[3747]: server 10.58.89.15#53: queries sent 1, retried or failed 0
Apr 15 12:59:05 zero_machine dnsmasq[3747]: server 10.206.31.142#53: queries sent 2, retried or failed 0
启动dnsmasq时,使用-q
参数可获得full cache dump
-q, --log-queries
Log the results of DNS queries handled by dnsmasq. Enable a full
cache dump on receipt of SIGUSR1.
特别注意,当dns服务器没有开启cache功能时,dnsmasq单方面开启cache是不生效的。
设置监听端口为127.0.0.1,实际在多网卡的机器是没有生效的,绑定的IP为0.0.0.0
# 本地监听
listen-address=127.0.0.1
监听53端口的网卡
[root@zero_machine /data]# netstat -tulnp |grep :53
tcp 0 0 0.0.0.0:53 0.0.0.0:* LISTEN 11822/dnsmasq
tcp6 0 0 :::53 :::* LISTEN 11822/dnsmasq
udp 0 0 0.0.0.0:53 0.0.0.0:* 11822/dnsmasq
udp6 0 0 :::53 :::* 11822/dnsmasq
如果需要dnsmasq进程只绑定在loopback端口(127.0.0.1),只开放本机使用,可以修改dnsmasq.conf配置
# interface uncomment,指向lo网卡
interface=lo
# bind-interfaces uncomment
bind-interfaces
重启dnsmasq后,效果如下
[root@zeroguo_op /data]# netstat -tulnp |grep :53
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 18985/dnsmasq
udp 0 0 127.0.0.1:53 0.0.0.0:* 18985/dnsmasq
也可以,启动dnsmasq时使用参数显示说明绑定的interface
-i, --interface=<interface name>
Listen only on the specified interface(s). Dnsmasq automatically adds the loopback (local) interface to the list of interfaces to use
when the --interface option is used. If no --interface or --listen-address options are given dnsmasq listens on all available inter‐
faces except any given in --except-interface options. IP alias interfaces (eg "eth1:0") cannot be used with --interface or --except-
interface options, use --listen-address instead. A simple wildcard, consisting of a trailing '*', can be used in --interface and
--except-interface options.
即
dnsmasq --interface=lo
具体参考here
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。