linux 下 安装 dnsmasq 配置域名解析

dnsmasq_install_on_ubuntu18

1.dnsmasq 简介

  • Dnsmasq 可以提供 DNS 缓存服务和 DHCP 服务功能,可以做正向反向 dns 解析
  • dnsmasq 轻量且易配置,适用于个人用户或百台左右规模的集群,bind 较重,配置复杂,建议用于上千或者更大规模的集群

1.1.dnsmasq 查询顺序

1.1.1.域名记录查询顺序

01./etc/hosts
02./etc/dnsmasq.conf 主配置文件 address 配置
03./etc/dnsmasq.d/ 子配置文件 address 配置
04.向上游 DNS 配置文件 /etc/dnsmasq.resolv.conf 指定的 NS 服务器做最后查询

1.1.2.获取域名 NS 服务器列表顺序

01./etc/resolv.conf
02.dnsmasq
03./etc/dnsmasq.conf 主配置文件 server 配置
04.resolv-file 指定的配置文件

1.1.3.查询 域名记录 时,dnsmasq 查询 NS 服务器的优先级

01.dnsmasq 主配置文件中下面的 server 参数行优先级高
02.resolv-file 文件列表中上面的优先级高

2.安装 dnsmasq 软件

Ubuntu18 安装 dnsmasq

  • 某些版本的 ubuntu18 默认使用 systemd-resolved 服务管理 dns 可以停掉
# 停掉并禁用默认 dns 服务并安装 dnsmasq
netstat -lanpt|grep 53
systemctl status systemd-resolved.service
systemctl stop systemd-resolved.service
systemctl disable systemd-resolved.service

# 清理软连接
rm -f /etc/resolv.conf
echo "nameserver 223.5.5.5" >> /etc/resolv.conf
cat /etc/resolv.conf

# 安装 dnsmasq
apt install dnsmasq -y
dnsmasq -v

# 启动服务并设置开机自启动
systemctl start dnsmasq
systemctl enable dnsmasq
netstat -lanpt|grep 53
  • 如果是 ubuntu14 系统下的托管服务可以修改以下配置文件,例如
vim /etc/resolvconf/resolv.conf.d/head
-------------------------------
172.0.0.1
223.5.5.5
-------------------------------

Centos7 安装 dnsmasq

  • 可以直接安装
yum install dnsmasq -y
dnsmasq -v

# 启动服务并设置开机自启动
systemctl start dnsmasq
systemctl enable dnsmasq
netstat -lanpt|grep 53

源码安装 dnsmasq

http://www.thekelleys.org.uk/dnsmasq/
http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.80.tar.gz

3.dnsmasq 配置管理

  • 系统 dns-client 获取域名解析记录时的顺序
/etc/hosts
dns-cache
/etc/resolv.conf
  • 默认 dnsmasq 使用系统的 /etc/resolv.conf,并读取 /etc/hosts 文件

3.1.修改系统 NS 配置

  • 作为 NS 的主机需要添加本机 127.0.0.1 地址
  • 为了防止意外可以添加几个公网的 NS 地址,例如阿里 NS 223.5.5.5
vim /etc/resolv.conf
------------------------------
nameserver {{PRIVATE_IP}}
nameserver 127.0.0.1
nameserver 223.5.5.5
------------------------------

3.2.通用配置

  • 这些配置可以保持默认,不影响服务的启动运行
  • 可以更具需求修改,监听端口,日志服务等,
# 注意备份配置文件
cp /etc/dnsmasq.conf /etc/dnsmasq.conf.ori

# 修改主配置文件,重启服务配置生效
vim /etc/dnsmasq.conf
------------------------------
# 监听地址,可以配置 IP 地址或者接口名称,多个地址使用英文逗号隔开,或者配置多行
# 只配置监听 0.0.0.0,无法正常连接到 dnsmasq 获取解析记录
listen-address=127.0.0.1,192.168.1.2
#listen-address={{PRIVATE_IP}}

# 服务运行的网卡,可与 listen-address 结合使用,可以添加多行
# interface=eth1

# 指定服务不在哪些网卡上运行
# except-interface=eth0

# 监听端口,默认配置
port=53

# 服务运行用户和运行组
user=root
group=root

# 是否启用日志,默认启用日志保存在 /var/log/debug 中,可以手动置顶日志存储位置
log-queries
log-facility=/var/log/dnsmasq.log

# 设置 DNS 缓存大小,单位条数
cache-size=10000

# 重启后清空缓存
clear-on-reload

domain-needed
bogus-priv

# conf-file 选项,指定子配置文件
conf-file=/opt/dnsmasq/config/dnsmasq-server.conf

# conf-dir 选项,指定子配置文件的目录,可以配置多行
# 支持高级语法,根据扩展名来包含或忽略配置文件,星号表示包含,不加星号表示排除
conf-dir=/opt/dnsmasq/config

# conf-dir 高级语法,包含指定目录下除 .bak 以外的所有文件
# conf-dir=/opt/dnsmasq/config,.bak

# conf-dir 高级语法,包含指定目录的所有以 .conf 结尾文件
# conf-dir=/opt/dnsmasq/config,*.conf
------------------------------
# 关于子配置文件的高级语法和普通写法不建议同时使用,防止配置覆盖

# 检查生效的配置
egrep  -v '^$|^[#;]' /etc/dnsmasq.conf
------------------------------
listen-address=127.0.0.1,192.168.1.2
port=53
user=root
group=root
log-queries
log-facility=/var/log/dnsmasq.log
cache-size=10000
conf-file=/opt/dnsmasq/config/dnsmasq-server.conf
conf-dir=/opt/dnsmasq/config
------------------------------

# 创建自定义的配置目录-用于配置域名解析
mkdir -p /opt/dnsmasq/config
tree /opt/dnsmasq

# 检查配置语法
dnsmasq --test

# 重启服务配置生效
systemctl restart dnsmasq.service

3.3.使用 dnsmasq 配置域名解析记录

3.3.1.使用 hosts 配置格式配置域名记录-即时生效

  • 注意:直接修改 /etc/hosts 不会即时生效,需要重启服务
  • 特点:使用 addn-hosts 参数指定的额外 hosts 文件,修改配置即时生效,不需要重启服务,推荐使用
# 创建用于测试的域名记录
vim /etc/hosts
-----------------------------
1.1.1.1 111.zuiyoujie.com
-----------------------------

# 修改主配置文件,增加相关配置
vim /etc/dnsmasq.conf
-----------------------------
# for_hosts_record
# 是否要读取 /etc/hosts 文件内容提供解析记录,默认关闭,表示使用该文件内容
# no-hosts

# hostsdir 参数,指定存放解析记录文件的目录,文件名可自定义,配置格式参考 /etc/hosts 格式
hostsdir=/opt/dnsmasq/hostsdir

# addn-hosts 参数,指定存放域名解析记录的配置文件,指定的文件可以不存在,修改内容即时生效
addn-hosts=/opt/dnsmasq/hosts/dnsmasq.hosts
addn-hosts=/opt/dnsmasq/hosts/333.zuiyoujie.com.hosts
-----------------------------

# 创建自定义的配置目录-用于配置域名解析
mkdir -p /opt/dnsmasq/{hosts,hostsdir}
tree /opt/dnsmasq

# 创建 addn-hosts 参数指定的域名解析文件,如果文件不存在只告警不影响服务启动
touch /opt/dnsmasq/hosts/dnsmasq.hosts

# 创建 hostsdir 参数指定的域名解析文件
cd /opt/dnsmasq/hostsdir
vim 222.zuiyoujie.com.hosts
-----------------------------
2.2.2.2 222.zuiyoujie.com
-----------------------------

cd /opt/dnsmasq/hosts
vim 333.zuiyoujie.com.hosts
-----------------------------
3.3.3.3 333.zuiyoujie.com
-----------------------------

# 检查配置语法
dnsmasq --test

# 重启服务配置生效
systemctl restart dnsmasq.service
  • 实例演示:
# 检查启动日志
-----------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
-----------------------------

# 解析测试
-----------------------------
root@zuiyoujie:~# nslookup 111.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   111.zuiyoujie.com
Address: 1.1.1.1

root@zuiyoujie:~# nslookup 222.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   222.zuiyoujie.com
Address: 2.2.2.2

root@zuiyoujie:~# nslookup 333.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   333.zuiyoujie.com
Address: 3.3.3.3
-----------------------------
  • 如果要支持一个域名对应多个 IP,必须使用 addn-hosts 选项,例如
vim /opt/dnsmasq/hosts/t1.zuiyoujie.com.host
-----------------------------
10.0.0.1 t1.zuiyoujie.com
10.0.0.2 t1.zuiyoujie.com
10.0.0.3 t1.zuiyoujie.com
-----------------------------

3.3.2.使用 address 参数配置域名记录-重启服务生效

  • address 参数支持泛解析,可用于防止域名劫持
  • address 参数,修改配置需要重启服务
# 修改主配置文件,增加相关配置
vim /etc/dnsmasq.conf
-----------------------------
# for_address_config
# address 参数,配置域名解析地址,一行一条解析记录,修改后重启服务配置生效
address=/444.zuiyoujie.com/4.4.4.4

# 为了便于域名管理可以将 address 配置在子配置文件中,文件名自定义,配置内容保持正常格式即可
conf-file=/opt/dnsmasq/address/555.zuiyoujie.com.addr
#conf-dir=/opt/dnsmasq/address
conf-dir=/opt/dnsmasq/address,.bak
conf-dir=/opt/dnsmasq/address,*.conf
-----------------------------

# 创建自定义的配置目录-用于配置域名解析
mkdir -p /opt/dnsmasq/address
tree /opt/dnsmasq

# 配置用于测试的域名记录
cd /opt/dnsmasq/address
echo "address=/555.zuiyoujie.com/5.5.5.5" > 555.zuiyoujie.com.addr
echo "address=/666.zuiyoujie.com/6.6.6.6" > 666.zuiyoujie.com.bak
echo "address=/.zuiyoujie.com/7.7.7.7" > 777.zuiyoujie.com.conf

# 检查配置语法
dnsmasq --test

# 重启服务配置生效
systemctl restart dnsmasq.service
  • 实例演示:
# 检查启动日志
--------------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
--------------------------------

# 解析测试
--------------------------------
root@zuiyoujie:/opt/dnsmasq/address# nslookup 555.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   555.zuiyoujie.com
Address: 5.5.5.5      # 子配置文件解析生效

root@zuiyoujie:/opt/dnsmasq/address# nslookup 666.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

 server can t find 666.zuiyoujie.com: NXDOMAIN  ## 使用 .bak 文件,解析记录失效

root@zuiyoujie:/opt/dnsmasq/address# nslookup 777.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   777.zuiyoujie.com
Address: 7.7.7.7      # 配置的泛域名解析生效

root@zuiyoujie:/opt/dnsmasq/address# nslookup 888.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   888.zuiyoujie.com
Address: 7.7.7.7      # 配置的泛域名解析生效
--------------------------------

3.3.3.使用 host-record 参数配置域名记录-重启服务生效

# 修改主配置文件,增加相关配置
vim /etc/dnsmasq.conf
-----------------------------
# for_host-record_config
host-record=ttt.zuiyoujie.com,10.10.10.10
-----------------------------

# 检查配置语法
dnsmasq --test

# 重启服务配置生效
systemctl restart dnsmasq.service
  • 实例演示:
# 检查启动日志
--------------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: reading /opt/dnsmasq/config/dnsmasq.resolv.conf
dnsmasq: using nameserver 8.8.8.8#53
dnsmasq: using nameserver 4.4.4.4#53
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
--------------------------------

# 解析测试
--------------------------------
root@zuiyoujie:/opt/dnsmasq/address# nslookup ttt.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Name:   ttt.zuiyoujie.com
Address: 10.10.10.10    ## 泛域名解析优先级低于已配置的域名记录
--------------------------------

3.3.4.使用 cname 参数配置域名别名记录-重启服务生效

  • 配置域名 cname 别名记录
# 修改主配置文件,增加相关配置
vim /etc/dnsmasq.conf
-----------------------------
# for_cname_config
cname=ccc.zuiyoujie.com,ttt.zuiyoujie.com
-----------------------------

# 检查配置语法
dnsmasq --test

# 重启服务配置生效
systemctl restart dnsmasq.service
  • 实例演示:
# 检查启动日志
--------------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
--------------------------------

# 解析测试
--------------------------------
root@zuiyoujie:/opt/dnsmasq/address# nslookup ccc.zuiyoujie.com
Server:         127.0.0.1
Address:        127.0.0.1#53

ccc.zuiyoujie.com       canonical name = ttt.zuiyoujie.com.
Name:   ttt.zuiyoujie.com
Address: 10.10.10.10
   server can t find ttt.zuiyoujie.com: NXDOMAIN
--------------------------------

3.4.配置上游 DNS

  • 配置 dnsmasq 到其他 DNS 服务器获取解析记录
  • 一般的主机或者服务器只能配置 2 到 3 个 DNS 服务器,dnsmasq 支持配置多个 DNS 服务器

3.4.1.使用 resolv-file 参数配置上游 DNS 服务器

  • resolv-file 指定的文件配置格式与 /etc/resolv.conf 相同
  • resolv-file 制定的文件,修改内容立即生效,不需要重启服务,推荐使用
  • resolv-file 指定的文件可以不存在,默认从 /etc/resolv.conf 中获取
  • 默认 dnsmasq 会查询 /etc/resolv.conf 文件获取 NS 服务器列表
# 修改主配置文件,增加相关配置
vim /etc/dnsmasq.conf
-----------------------------
# for_resolv_config
# no-resolv 参数,控制是否查询 resolv 相关文件,默认关闭,即不使用上游 DNS 服务器查询域名记录
# no-resolv

# no-poll 参数,是否轮询查找不同的 resolv 配置文件获取 NS 服务器列表,默认关闭,即同时查询所有配置的 resolv 文件
# no-poll

# resolv-file 参数,配置上游 DNS 服务器列表
resolv-file=/opt/dnsmasq/config/dnsmasq.resolv.conf

# strict-order 参数,控制是否严格按照 resolv 文件中的列表顺序查询,默认关闭,即将请求同时发送到所有 DNS 服务器,使用响应最快的服务器的结果
strict-order

# all-servers 参数,是否对所有注册的 DNS 服务器发起查询请求,选择响应最快的服务器的结果
# all-servers
-----------------------------

# 创建自定义的配置目录
mkdir -p /opt/dnsmasq/config
tree /opt/dnsmasq/

# 创建对应的 resolv 配置文件上游 DNS 服务器配置
vim /opt/dnsmasq/config/dnsmasq.resolv.conf
-----------------------------
# resolv_for_dnsmasq
nameserver 119.29.29.29
nameserver 180.76.76.76
-----------------------------

# 检查配置语法,重启服务
dnsmasq --test
systemctl restart dnsmasq.service
  • 实例演示:查看 dnsmasq 日志
dnsmasq: reading /opt/dnsmasq/config/dnsmasq.resolv.conf
dnsmasq: using nameserver 119.29.29.29#53
dnsmasq: using nameserver 180.76.76.76#53

3.4.2.使用 server 参数配置上游 DNS 服务器

  • server 参数与 resolv-file 相比,配置格式不同,但功能更多,比如支持对某个域名单独指定获取解析时使用的 DNS 服务器
  • server 参数不支持子配置文件
  • server 参数修改配置内容需要重启生效
# 修改主配置文件,增加相关配置
vim /etc/dnsmasq.conf
-----------------------------
# for_resolv_config
# server 参数,配置 DNS 上游服务器
server=/baidu.com/8.8.8.8
server=/aliyun.com/223.6.6.6
server=114.114.114.114
server=/google.com/8.8.8.8

# conf-dir 高级语法,包含指定目录的所有以 .conf 结尾文件
# conf-dir=/opt/dnsmasq/config,*.conf
-----------------------------

# 检查配置语法,重启服务
dnsmasq --test
systemctl restart dnsmasq.service
  • 实例演示:
# 查看 dnsmasq 启动日志
-----------------------------
dnsmasq: exiting on receipt of SIGTERM
dnsmasq: started, version 2.79 cachesize 10000
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth nettlehash DNSSEC loop-detect inotify
dnsmasq: using nameserver 8.8.8.8#53 for domain google.com 
dnsmasq: using nameserver 114.114.114.114#53
dnsmasq: using nameserver 223.6.6.6#53 for domain aliyun.com 
dnsmasq: using nameserver 8.8.8.8#53 for domain baidu.com 
dnsmasq: reading /opt/dnsmasq/config/dnsmasq.resolv.conf
dnsmasq: using nameserver 8.8.8.8#53 for domain google.com 
dnsmasq: using nameserver 114.114.114.114#53
dnsmasq: using nameserver 223.6.6.6#53 for domain aliyun.com 
dnsmasq: using nameserver 8.8.8.8#53 for domain baidu.com 
dnsmasq: using nameserver 119.29.29.29#53
dnsmasq: using nameserver 180.76.76.76#53
dnsmasq: read /etc/hosts - 9 addresses
dnsmasq: read /opt/dnsmasq/hosts/333.zuiyoujie.com.hosts - 1 addresses
dnsmasq: read /opt/dnsmasq/hosts/dnsmasq.hosts - 0 addresses
dnsmasq: read /opt/dnsmasq/hostsdir/222.zuiyoujie.com.hosts - 1 addresses
-----------------------------

# 解析测试
root@zuiyoujie:/opt/dnsmasq/address# nslookup www.baidu.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
Name:   www.a.shifen.com
Address: 14.215.177.38
Name:   www.a.shifen.com
Address: 14.215.177.39
www.a.shifen.com        canonical name = www.wshifen.com.
------------------------------
dnsmasq: query[A] www.baidu.com from 127.0.0.1  # 按照 /etc/resolv.conf 文件中的 NS 记录顺序
dnsmasq: forwarded www.baidu.com to 8.8.8.8  # dnsmasq 接受请求,根据主配置文件中 server=/baidu.com/8.8.8.8 中的配置,使用 8.8.8.8 劫持查询
dnsmasq: reply www.baidu.com is <CNAME>
dnsmasq: reply www.a.shifen.com is 14.215.177.38
dnsmasq: reply www.a.shifen.com is 14.215.177.39
dnsmasq: query[AAAA] www.a.shifen.com from 127.0.0.1
dnsmasq: forwarded www.a.shifen.com to 114.114.114.114
dnsmasq: reply www.a.shifen.com is <CNAME>
dnsmasq: reply www.wshifen.com is NODATA-IPv6
------------------------------

root@zuiyoujie:/opt/dnsmasq/address# nslookup www.google.com
Server:         127.0.0.1
Address:        127.0.0.1#53

Non-authoritative answer:
Name:   www.google.com
Address: 142.250.66.68
Name:   www.google.com
Address: 2404:6800:4005:81b::2004
------------------------------
dnsmasq: query[A] www.google.com from 127.0.0.1  # 按照 /etc/resolv.conf 文件中的 NS 记录顺序
dnsmasq: forwarded www.google.com to 8.8.8.8     # dnsmasq 接受请求,根据主配置文件中 server=/google.com/8.8.8.8 中的配置,使用 8.8.8.8 劫持查询
dnsmasq: reply www.google.com is 142.250.4.147
dnsmasq: reply www.google.com is 142.250.4.105
dnsmasq: reply www.google.com is 142.250.4.106
dnsmasq: reply www.google.com is 142.250.4.99
dnsmasq: reply www.google.com is 142.250.4.104
dnsmasq: reply www.google.com is 142.250.4.103
dnsmasq: query[AAAA] www.google.com from 127.0.0.1
dnsmasq: forwarded www.google.com to 8.8.8.8
dnsmasq: reply www.google.com is 2404:6800:4003:c0f::69
dnsmasq: reply www.google.com is 2404:6800:4003:c0f::63
dnsmasq: reply www.google.com is 2404:6800:4003:c0f::67
dnsmasq: reply www.google.com is 2404:6800:4003:c0f::6a
  • 根据 dnsmasq 启动和查询日志可知:
  • 1.获取 NS 服务器列表时,先查询 dnsmasq 主配置文件,后查询 resolv-file 文件列表
  • 2.查询 域名记录 时,dnsmasq 主配置文件中越靠后的 server 参数行优先级越高,resolv-file 文件列表中越往上优先级越高

3.5.常用配置参数说明

参数名称 参数说明
listen-address=127.0.0.1,192.168.1.2 监听地址,可以配置 IP 地址或者接口名称,多个地址使用英文逗号隔开
interface=eth1 服务运行的网卡,可以添加多行,可与 listen-address 结合使用
except-interface=eth0 指定服务不在以下网卡上运行
no-dhcp-interface=eth0 指定不提供 DHCP 或 TFTP 服务的接口,仅提供 DNS 服务
port=53 侦听端口
user=root 和 group=root 服务运行用户及用户组
cache-size=10000 DNS 缓存条数
clear-on-reload 重启后清空缓存
log-queries 启用日志记录,默认保存在 /var/log/debug 中
log-facility=/var/log/dnsmasq.log 日志文件名
log-async=20 异步 log,可以缓解阻塞
no-negcache 不缓存未知域名缓存,默认情况下 dnsmasq 缓存未知域名并直接返回为客户端
bogus-nxdomain=114.114.114.114 对于任何被解析到此 IP 的域名,将响应 NXDOMAIN 使其解析失效,可以多次指定,通常用于对于访问不存在的域名,禁止其跳转到运营商的广告站点
domain-needed 完整的域名才向上游服务器查找,如果仅仅是主机名仅查找hosts文件
bogus-priv Never forward addresses in the non-routed address spaces.避免含有格式出错的域名或私有 IP 地址的数据包离开你的网络
----------------------- ---------------------------
no-hosts 不加载本地的 /etc/hosts 文件
addn-hosts= 域名解析配置文件,可以有多行,hosts 文件格式,不支持泛域名解析,修改内容重启服务生效
conf-file=/etc/dnsmasq.more.conf 指定域名解析记录的文件,可以配置多行,hosts 文件格式,修改内容解析立即生效,默认为 /etc/dnsmasq.more.conf 文件
conf-dir=/etc/dnsmasq.d 指定域名解析记录的文件目录,可以配置多行,hosts 文件格式,修改内容解析立即生效,默认为 /etc/dnsmasq.d 目录
conf-dir=/etc/dnsmasq.d,.bak 高级语法,包含指定目录的所有文件,但是排除以 .bak 结尾的文件
conf-dir=/etc/dnsmasq.d/,*.conf 高级语法,包含指定目录的所有文件,且文件需要以 .conf 结尾
expand-hosts 和 domain=zuiyoujie.com 用于扩站主机名,在主机名后自动添加指定的域名,可以省略很多配置,
address=/zuiyoujie.com/192.168.1.5 泛域名配置,除非在域名规划架构相当合理的时候否则尽量少用繁泛域名解析,k8s 集群中使用泛域名也会极大的减少管理成本
----------------------- ---------------------------
no-resolv 不使用上游 DNS 服务器的配置文件 /etc/resolv.conf 或者 resolv-file 选项
no-poll 不允许 Dnsmasq 通过轮询 /etc/resolv.conf 或者其他文件来动态更新上游 DNS 服务列表
resolv-file= 指定上游 DNS 服务器的配置文件,dnsmasq 可以从哪里获取解析记录,修改内容重启服务生效
strict-order 严格按照 resolv.conf 文件中的顺序进行查找
server=/aliyun.com/223.5.5.5 配置某个域名使用指定的 NS 进行解析

3.6.配置日志格式

vim /etc/logrotate.d/dnsmasq
------------------------------
/var/log/dnsmasq.log {
    daily
    copytruncate
    missingok
    rotate 30
    compress
    notifempty
    dateext
    size 200M
} 
------------------------------

3.7.使用 supervisor 管理 dnsmasq

  • 配置服务统一管理和宕机重启
  • 需要让 dnsmasq 保持前台运行
# 停止并禁用 systemctl 管理的 dnsmasq 服务
systemctl stop dnsmasq
systemctl disable dnsmasq

# 检查服务状态
netstat -lanpt

cd /opt/supervisord/etc
vim supervisor_dnsmasq.conf
-----------------------------
[program:dnsmasq]
directory = /opt/dnsmasq
command = /usr/sbin/dnsmasq --no-daemon --log-queries
user=root
group=root
numprocs=1
stopsignal=TERM
startretries=0
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile = /opt/supervisord/var/log/dnsmasq.log
-----------------------------

# 更新 supervisor 配置
supervisorctl update

# 启动管理 dnsmasq
supervisorctl restart dnsmasq
supervisorctl status dnsmasq

10.参考地址:

https://www.cnblogs.com/sunsky303/p/9238669.html
https://cloud.tencent.com/developer/article/1174717
https://linux.cn/article-9438-1.html
http://www.enkichen.com/2017/05/23/dnsmasq-introduce/
https://www.jianshu.com/p/26e11c3babc2
https://linux.cn/article-9438-1.html
本文版权归作者和博客园共有,如需转载请在文章页面给出原文链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/tssc/p/15148864.html