dnsmasq的使用

date
Apr 10, 2021
slug
rdzchyme
status
Published
tags
工具
summary
type
Post
 

背景

公司系统较多,很多通过 IP+端口方式,往往很难记住,也不好做管理和监控。
为此引入内网域名方式来访问系统,降低使用门槛,做好流量监控,同时在意外情况下方便断网。
最期待的情况是,一个域名在内网解析内网 IP,在外网解析公网 IP。
不过大部分情况只需满足前者即可,毕竟有很多系统暴露到公网之后容易被别人攻击。所以下面情况仅针对在内网使用,顶级域名也是自定义的:.hx
对比发现 dnsmasq 有使用方便简单、功能齐全,不过缺少可视化配置的,希望后面官方能提供支持。

介绍

安装

1、源码方式
版本是最新的,推荐!
git clone https://thekelleys.org.uk/git/dnsmasq.git

cd dnsmasq

make && make install
2、yum 方式
可能版本不是最新的,推荐!
yum -y install dnsmasq
3、docker 方式
4km3/dnsmasq,感觉性能比不上真机安装。贴一个 docker-compose.yml
version: "3.9"services:  dnsmasq:    container_name: dnsmasq    image: "4km3/dnsmasq:2.85-r2"    restart: always    environment:      TZ: "Asia/Shanghai"    ports:      - 53:53      - 53:53/udp    cap_add:      - NET_ADMIN    volumes:      - "~/docker/dnsmasq/dnsmasq.conf:/etc/dnsmasq.conf"      - "~/docker/dnsmasq/resolv.conf:/etc/resolv.conf"      # - "~/docker/dnsmasq/conf.d:/etc/dnsmasq.d"      # - "~/docker/dnsmasq/log:/var/log/dnsmasq"
安装完成后,设为开机启动
systemctl enable dnsmasq.service
systemctl start dnsmasq.service

解析流程

dnsmasq 先去解析 hosts 文件,再去解析/etc/dnsmasq.d/下的*.conf 文件,并且这些文件的优先级要高于 dnsmasq.conf。我们自定义的 resolv.dnsmasq.conf 中的 DNS 也被称为上游 DNS,这是最后去查询解析的。
如果不想用 hosts 文件做解析,我们可以在/etc/dnsmasq.conf 中加入 no-hosts 这条语句,这样的话就直接查询上游 DNS 了。如果我们不想做上游查询,就是不想做正常的解析,我们可以加入 no-reslov 这条语句。
同 nginx 类似,dnsmasq 检查语法配置dnsmasq -test,默认配置文件为/etc/dnsmasq.conf,可包含更多配置文件。
我在这里踩过一个大坑:配置后一直不生效,以为有缓存,原来是启用了第一个 conf-dir,把备份文件的内容也包含进去了。
# Include another lot of configuration options.
#conf-file=/etc/dnsmasq.more.conf
#conf-dir=/etc/dnsmasq.d

# Include all the files in a directory except those ending in .bak
#conf-dir=/etc/dnsmasq.d,.bak

# Include all files in a directory which end in .conf
conf-dir=/etc/dnsmasq.d/,*.conf

配置

dnsmasq 经常修改的比较重要参数说明 | 具体参数 | 参数说明 | | ———– | ———– | | resolv-file | 定义 dnsmasq 从哪里获取上游 DNS 服务器的地址,默认从/etc/resolv.conf 获取 | | strict-order | 表示严格按照 resolv-file 文件中的顺序从上到下进行 DNS 解析,直到第一个解析成功为止 | | listen-address | 定义 dnsmasq 监听的地址,默认是监控本机的所有网卡上的 IP。 | | address | 启用泛域名解析,即自定义解析 a 记录 | | bogus-nxdomain | 对于任何被解析到此 IP 的域名,将响应 NXDOMAIN 使其解析失效,可以多次指定通常用于对于访问不存在的域名,禁止其跳转到运营商的广告站点 | | server | 指定使用哪个 DNS 服务器进行解析,对于不同的网站可以使用不同的域名对应解析 |
1、DNS 解析指定 IP
指定域名解析到特定的 IP 上。DNS 劫持就是非法的 DNS 就经常把一些正规的网站解析到不正确 IP 上,所以可以用来屏蔽网页广告。
address=/abc.hx/192.168.1.249

address=/ad.youku.com/127.0.0.1
address=/ad.iqiyi.com/127.0.0.1
同时也支持泛域名解析,这是我非常喜欢的功能。例如访问 long.com 时的所有域名(含子域名)都会被解析成 192.168.115.10
address=/long.com/192.168.115.10
2、DNS 加快解析
通过 server 来指定特定域名使用制定 DNS 解析,同时使用 DNS 解析缓存提升内网解析速度。

国内指定 DNS
server=/cn/114.114.114.114
server=/taobao.com/114.114.114.114
server=/taobaocdn.com/114.114.114.114

国外指定 DNS
server=/google.com/223.5.5.5
3、TTL 相关的
缓存时间设置,一般不需要设置。TTL 相关的在 dnsmasq.conf 中没有写出,需要自己手动加上。
#如果查询的域名没 ttl,则使用此设置为缓存 ttl 时间
neg-ttl=600

#指定返回给客户端的 ttl 时间。小于查询域名的 ttl 以设置为准,大于以域名的 ttl 为准。dnsmasq 服务器中缓存 ttl 不变
max-ttl=600

#同 max-ttl 类似,这个是 dnsmasq 服务器缓存时间设定,低于域名 ttl 以设定为准,否则以域名 ttl 为准,即 Min(max-cache-ttl, 域名 ttl)
max-cache-ttl=3600

#和 max-cache-ttl 相反,如果域名 ttl 低于设定值,强制使用设定为 dnsmasq 服务器的缓存时间,限制不能超过 3600
min-cache-ttl=3600

#本地 hosts 文件的缓存时间,通常不要求缓存本地,这样更改hosts文件后就即时生效
#local-ttl=3600
总结(可能有点绕口,其实就是判断该返回过期时间还有好久):
  • max-ttl 和 max-cache-ttl 没必要设置,想要客户端缓存时间小于域名 ttl 设置 max-ttl,但是 dnsmasql 缓存时间以域名 ttl 为准。
  • 想要 dnsmasq 缓存时间小于域名 ttl 可以设置 max-cache-ttl,同时如果没设置 max-ttl 首次请求返回客户端的 ttl 时间以域名 ttl 为准,再次请求返回的 ttl 则是 dnsmasq 缓存剩余时间 ttl。
  • 如果想强制任何域名的缓存时间使用 min-cache-ttl,dnsmasq 限制最高为 1 小时,更高需重新编译,首次请求返回 ttl 为域名 ttl,再次请求返回的 ttl 则是 dnsmasq 缓存剩余时间 ttl。
4、其他配置
#指定DNS的端口,默认53。设置 port=0 将完全禁用 DNS 功能,仅使用 DHCP/TFTP
#port=53

#缓存条数。默认为 150 条,cache-size=0 表示禁用缓存
cache-size=10000

#默认是本机的 IP。若要为局域网提供 DNS,建议配置静态 IP 和本地 IP
listen-address=192.168.10.30,127.0.0.1

#添加读取额外的 hosts 文件路径,可以多次指定。 #如果指定为目录,则读取目录中的所有文件。 #感觉和 address 没啥区别
addn-hosts=/etc/dnsmasq.hosts

#设置日志记录器
log-queries
log-facility=/var/log/dnsmasq.log

DHCP

dnsmasp 内置了 DHCP 功能,可以取代 dhcpd(DHCPD 服务配置)和 bind 等服务,配置起来更简单。
# dhcp动态分配的地址范围,自动分配50-150ip段,有效期48小时
# Uncomment this to enable the integrated DHCP server, you need
# to supply the range of addresses available for lease and optionally a lease time
dhcp-range=192.168.1.50,192.168.1.150,48h

# 设置默认租期
# Set the limit on DHCP leases, the default is 150
#dhcp-lease-max=150

# dhcp服务的静态绑定
# dhcp-host的配置方式有很多种,这里使用的是:[client端MAC地址] + [分配的IP]+ [主机名]
# Always set the name and ipaddr of the host with hardware address
# dhcp-host=00:0C:29:5E:F2:6F,192.168.1.201
# dhcp-host=00:0C:29:5E:F2:6F,192.168.1.201,infinite    无限租期
dhcp-host=00:0C:29:5E:F2:6F,192.168.1.201,os02
dhcp-host=00:0C:29:15:63:CF,192.168.1.202,os03
dhcp 服务的其它几种绑定方式
# Always allocate the host with ethernet address 11:22:33:44:55:66
# The IP address 192.168.0.60
# 绑定网卡地址对应ip地址,用的是host的方式,类似在hosts文件写一个host name 对应一个ip。所以这个不是arp绑定,要区分。
# dhcp-host=11:22:33:44:55:66,192.168.1.60

# Always set the name of the host with hardware address
# 11:22:33:44:55:66 to be "fred"
# 绑定mac地址对应一个host name ,我个人觉得绑定mac对应ip就足够了,绑定对应名字比较少见
# dhcp-host=11:22:33:44:55:66,fred

# Always give the host with ethernet address 11:22:33:44:55:66
# the name fred and IP address 192.168.0.60 and lease time 45 minutes
# 这个是组合版,绑定某个mac对应fred名字,然后加上一个ip分配,并设置租约,这个只能说是灵活配置的参考,没啥实际意义
# dhcp-host=11:22:33:44:55:66,fred,192.168.0.60,45m

# Give a host with ethernet address 11:22:33:44:55:66 or
# 12:34:56:78:90:12 the IP address 192.168.0.60. Dnsmasq will assume
# that these two ethernet interfaces will never be in use at the same
# time, and give the IP address to the second, even if it is already
# in use by the first. Useful for laptops with wired and wireless
# addresses.
# 绑定一个ip对应多mac地址,用途场景存在于实验室和无线网络,我想只是为了让2块网卡用同一个ip这样单纯而纯洁的需求而已[不常用]
# dhcp-host=11:22:33:44:55:66,12:34:56:78:90:12,192.168.0.60

# Give the machine which says its name is "bert" IP address
# 192.168.0.70 and an infinite lease
# 绑定名字对应ip,并且给了一个新参数,无限租约
# dhcp-host=bert,192.168.0.70,infinite

# Always give the host with client identifier 01:02:02:04
# the IP address 192.168.0.60
# 给予一个特殊标识符对应ip,用途不明
# dhcp-host=id:01:02:02:04,192.168.0.60

# Always give the host with client identifier "marjorie"
# the IP address 192.168.0.60
# 跟上一条类似,只是标识符还支持普通字符字串
# dhcp-host=id:marjorie,192.168.0.60

# 通过/etc/hosts来分配对应的hostname
# Enable the address given for "judge" in /etc/hosts
# to be given to a machine presenting the name "judge" when
# it asks for a DHCP lease.
# dhcp-host=judge

# 忽略下面MAC地址的DHCP请求,不分配ip
# Never offer DHCP service to a machine whose ethernet
# address is 11:22:33:44:55:66
#dhcp-host=11:22:33:44:55:66,ignore

# If this line is uncommented, dnsmasq will read /etc/ethers and act
# on the ethernet-address/IP pairs found there just as if they had
# been given as --dhcp-host options. Useful if you keep
# MAC-address/host mappings there for other purposes.
# 使用额外的文件代替主配置文件来处理dhcp-host的匹配,这个主要为了方便管理,将所有需要用到的dhcp-host配置都放进/etc/ethers

参考资料:

© 刘德华 2020 - 2023