前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >dnsmasq部署

dnsmasq部署

原创
作者头像
zero000
修改2019-06-15 11:31:25
2.7K0
修改2019-06-15 11:31:25
举报
文章被收录于专栏:程序员菜谱程序员菜谱

1. 简介

dnsmasq是一款小巧且方便地用于配置DNS服务器和DHCP服务器的工具,适用于小型网络,它提供了DNS解析功能和可选择的DHCP功能。

dnsmasq可以解决小范围的dns查询问题,如果业务是跨机房、跨地区的话不建议使用dnsmasq做为dns解析服务器。

dnsmasq官网如下:

http://www.thekelleys.org.uk/dnsmasq/doc.html

image.png
image.png

可以看到最新的版本为2.8.0

image.png
image.png

详细参考here

2. 安装dnsmasq

一般开源软件都可以同步源码安装和yum和apt-get安装两种方式

2.1 源码安装

源码安装dnsmasq,从官网下载安装包

代码语言:txt
复制
wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.80.tar.gz

安装GCC

代码语言:txt
复制
yum -y install gcc

解压安装

代码语言:txt
复制
tar -xf dnsmasq-2.80.tar.gz

cd dnsmasq-2.80

make install
image.png
image.png

可知,dnsmasq默认安装与/usr/local/sbin中

代码语言:txt
复制
dnsmasq -v 
image.png
image.png

2.2 yum和apt-get安装

yum安装

代码语言:txt
复制
yum -y install dnsmasq
代码语言:txt
复制
dnsmasq -v

apt-get安装

代码语言:txt
复制
sudo apt-get -y install dnsmasq
代码语言:txt
复制
dnsmasq -v

2.2 启动

配置正确,使用如下命令即可启动

代码语言:txt
复制
dnsmasq

3. 配置说明

dnsmasq配置十分简单,一般设置绑定的网卡(53端口)以及后端的NameServer即可

代码语言:txt
复制
# 本地监听
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文件参考:

代码语言:txt
复制
cat /data/dnsmasq/conf/dnsmasq.resolv.conf
nameserver  114.114.114.114
nameserver  8.8.8.8

其他配置:

代码语言:txt
复制
# 严格按照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

4. 监控

  1. 检查dnsmasq进程存活,不存在即拉起,并发出告警
  2. 如果拉起失败,先测试nameserver可用性,可用的话降级至/etc/resolv.conf

5. 特殊场景

5.1 开启cache缓存dns解析结果

查看dnsmasq cache详情,先使用USR1信号dump statistics to the system log.

代码语言:txt
复制
pkill -USR1 dnsmasq

接着查看系统日志,可以查看详细的cache情况

代码语言:txt
复制
[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

代码语言:txt
复制
-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是不生效的。

5.2 绑定本地lo网卡

设置监听端口为127.0.0.1,实际在多网卡的机器是没有生效的,绑定的IP为0.0.0.0

代码语言:txt
复制
# 本地监听
listen-address=127.0.0.1

监听53端口的网卡

代码语言:txt
复制
[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配置

代码语言:txt
复制
# interface uncomment,指向lo网卡
interface=lo

# bind-interfaces uncomment
bind-interfaces

重启dnsmasq后,效果如下

代码语言:txt
复制
[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

代码语言:txt
复制
 -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.

代码语言:txt
复制
dnsmasq --interface=lo

具体参考here

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 简介
  • 2. 安装dnsmasq
    • 2.1 源码安装
      • 2.2 yum和apt-get安装
        • 2.2 启动
        • 3. 配置说明
        • 4. 监控
        • 5. 特殊场景
          • 5.1 开启cache缓存dns解析结果
            • 5.2 绑定本地lo网卡
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档