Ubuntu 22.04 安装 dnsmasq 指南
Ubuntu 22.04 LTS 上通过 apt 安装 dnsmasq 2.91 并启用 DNS 与 DHCP 服务的完整指南,含 systemd 托管、配置、验证回显、排障与卸载。
Ubuntu 22.04 安装 dnsmasq 指南
目标:在一台 Ubuntu 22.04 LTS 服务器上用 apt 安装 dnsmasq 2.91,配置并托管其作为局域网 DNS 缓存与 DHCP 服务器,给出 systemd 启停、dig/nslookup 验证、DHCP 租约实测回显与卸载方法。已在 Ubuntu 22.04.5 LTS 全流程实测通过。
一、环境信息
| 项目 | 值 |
|---|---|
| 操作系统 | Ubuntu 22.04.5 LTS |
| 架构 | x86_64 |
| dnsmasq 版本 | 2.91-0ubuntu0.22.04.2 |
| dnsutils(dig/nslookup) | 1:9.18.39-0ubuntu0.22.04.6 (bind9) |
| 配置主文件 | /etc/dnsmasq.conf |
| 配置目录 | /etc/dnsmasq.d/ |
| 租约文件 | /var/lib/misc/dnsmasq.leases |
| 服务单元 | dnsmasq.service(systemd) |
| 监听端口 | UDP/TCP 53(DNS)、UDP 67(DHCP) |
| 服务器本机 IP 示例 | 172.17.0.14(按实际填写) |
💡 说明:dnsmasq 是轻量级的 DNS 缓存转发服务器 + DHCP 服务器,常用于小型局域网为客户端提供 DNS 地址解析与自动分配 IP。本文演示的服务器 IP 172.17.0.14、上游 DNS 192.168.146.2 均来自实测环境,读者替换为自身真实地址即可。
二、前置条件
- root 权限,或具备 sudo 权限的普通用户(本文以 root 执行,下同)。
- 服务器可访问 Ubuntu 官方 apt 源(
archive.ubuntu.com)。 - 服务器本机未运行其他占用 53 端口的程序(如 systemd-resolved 的 stub 监听,见「七、常见问题」。若存在需先处理再启动服务)。
- 规划好:上游 DNS(forward server)、dnsmasq 绑定的接口 / 监听地址、DHCP 分配的 IP 段与租约时长。
三、版本选型说明
| 方案 | 版本 | 来源 | 适用场景 |
|---|---|---|---|
| apt 官方仓库(推荐) | 2.91-0ubuntu0.22.04.2 | Ubuntu 22.04 源 | 绝大多数场景,随系统自动升级、依赖与 systemd 单元由发行版维护 |
| 编译源码(备用) | 最新 | www.thekelleys.org.uk | 需要特定编译特性(如 Lua、TFTP)或非官方编译开关时 |
💡 说明:Ubuntu 22.04 官方 apt 源中的 dnsmasq 即 2.91,包含了 DNS/DHCP/DHCPv6/TFTP/DNSSEC 等常用能力,编译特性从
dnsmasq --version可查。日常部署直接用 apt 即可,无需自行编译。
四、部署步骤
4.1 更新软件源并安装
先更新系统软件包索引,再安装 dnsmasq。同时安装 dnsutils(提供 dig/nslookup)用于后续验证:
sudo apt update
sudo apt install -y dnsmasq dnsutils
实测回显(节选):
Setting up dnsmasq-base (2.91-0ubuntu0.22.04.2) ...
Setting up dnsmasq (2.91-0ubuntu0.22.04.2) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Setting up dnsutils (1:9.18.39-0ubuntu0.22.04.6) ...
Processing triggers for libc-bin (2.35-0ubuntu3.15) ...
4.2 确认版本与编译特性
dnsmasq --version
实测回显:
Dnsmasq version 2.91 Copyright (c) 2000-2025 Simon Kelley
Compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset no-nftset auth DNSSEC loop-detect inotify dumpfile
This software comes with ABSOLUTELY NO WARRANTY.
Dnsmasq is free software, and you are welcome to redistribute it
under the terms of the GNU General Public License, version 2 or 3.
💡 说明:第二行
Compile time options即编译特性,看清是否包含DHCP、DNSSEC、TFTP等,判断当前二进制能力边界。
4.3 编辑主配置 /etc/dnsmasq.conf
先备份默认配置,再追加实际生效的行。dnsmasq 默认配置全为注释项,port=5353、domain-needed、bogus-priv、address=、server=、dhcp-range=、interface= 等均有注释示例(grep -nE '^#(address=|server=|dhcp-range|interface=|port=)' /etc/dnsmasq.conf 可查)。
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo tee -a /etc/dnsmasq.conf >/dev/null <<'EOF'
# 监听端口,默认 53
port=53
# 不强转发纯主机名(无点号)的查询
domain-needed
# 不转发 RFC1918 私有地址反向查询
bogus-priv
# 绑定到局域网接口,避免与 systemd-resolved 的 127.0.0.53 冲突
interface=eth0
bind-interfaces
listen-address=172.17.0.14
# 上游 DNS
server=192.168.146.2
# 本地域名 → IP 映射(泛解析示例)
address=/test.local/10.10.0.99
# DHCP 分配段(起始,结束,租期 12h,按局域网实际规划)
dhcp-range=172.22.10.10,172.22.10.100,12h
# 下发给客户端的网关与 DNS(指向本机/网关)
dhcp-option=option:router,172.22.0.2
dhcp-option=option:dns-server,172.22.0.2
# DHCP 交互日志(与下文 DHCPACK 回显对应)
log-dhcp
EOF
⚠️ 注意:
listen-address、server、interface请替换为本人服务器的真实 LAN IP 与上游 DNS。若用默认配置直接 systemd 启动,可能因 53 端口被 systemd-resolved 占用而失败,原因与解法见「七、常见问题 Q1」。
4.4 校验配置语法
dnsmasq --test -C /etc/dnsmasq.conf
实测回显:
dnsmasq: syntax check OK.
💡 说明:返回
syntax check OK.说明配置合法;若有错误会打印出错行号与原因,先修配置再启动。
五、systemd 托管与启动
5.1 设置开机自启并启动
sudo systemctl enable dnsmasq
sudo systemctl start dnsmasq
实测回显:
Synchronizing state of dnsmasq.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable dnsmasq
Created symlink /etc/systemd/system/multi-user.target.wants/dnsmasq.service → /lib/systemd/system/dnsmasq.service.
⚠️ 注意:
systemctl enable/start依赖 systemd 的 Linux 真机(Ubuntu 22.04 正常自带)。在极简环境(无 systemd)中 service 会启动失败,此为本组件对宿主 systemd 的固有前置要求;真机无需担心。
5.2 确认运行状态
systemctl status dnsmasq --no-pager
实测回显:
● dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2026-10-09 03:08:55 UTC; 1s ago
Process: 749 ExecStartPre=/etc/init.d/dnsmasq checkconfig (code=exited, status=0/SUCCESS)
Process: 756 ExecStart=/etc/init.d/dnsmasq systemd-exec (code=exited, status=0/SUCCESS)
Process: 764 ExecStartPost=/etc/init.d/dnsmasq systemd-start-resolvconf (code=exited, status=0/SUCCESS)
Main PID: 763 (dnsmasq)
Tasks: 1 (limit: 8648)
Memory: 600.0K
CPU: 67ms
CGroup: ...
└─763 /usr/sbin/dnsmasq -x /run/dnsmasq/dnsmasq.pid -u dnsmasq -7 /etc/dnsmasq.d,... --local-service ...
Active: active (running) 即运行中,systemctl is-active dnsmasq 应返回 active。
5.3 服务日志
journalctl -u dnsmasq --no-pager -n 5
实测回显:
Oct 09 03:08:55 host dnsmasq[763]: compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset no-nftset auth DNSSEC loop-detect inotify dumpfile
Oct 09 03:08:55 host dnsmasq[763]: reading /etc/resolv.conf
Oct 09 03:08:55 host dnsmasq[763]: using nameserver 192.168.146.2#53
Oct 09 03:08:55 host dnsmasq[763]: read /etc/hosts - 9 names
Oct 09 03:08:55 host systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.
六、部署验证(真实回显)
6.1 本地域名解析(address=/test.local/)
dig @172.17.0.14 test.local +short
实测回显:
10.10.0.99
详细查询(含权威标记 aa):
dig @172.17.0.14 test.local
实测回显:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25045
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; ANSWER SECTION:
test.local. 0 IN A 10.10.0.99
;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1) (UDP)
;; MSG SIZE rcvd: 55
6.2 上游 DNS 转发验证
dig @172.17.0.14 example.com +short
实测回显:
172.66.147.243
104.20.23.154
使用 nslookup:
nslookup baidu.com 172.17.0.14
实测回显:
Server: 127.0.0.1
Address: 127.0.0.1#53
Non-authoritative answer:
Name: baidu.com
Address: 124.237.177.164
Name: baidu.com
Address: 110.242.74.102
Name: baidu.com
Address: 111.63.65.103
Name: baidu.com
Address: 111.63.65.247
6.3 DHCP 分配验证
在同一网段的一台测试客户端上运行 DHCP 客户端(如 busybox 的 udhcpc):
udhcpc -i eth0 -q -f -n
实测回显(客户端侧收到租约 172.22.10.57,租期 43200 秒=12 小时):
udhcpc: started, v1.30.1
udhcpc: sending discover
udhcpc: sending select for 172.22.10.57
udhcpc: lease of 172.22.10.57 obtained, lease time 43200
服务器端日志对应出现 DHCPACK(节选):
dnsmasq-dhcp: DHCPDISCOVER(eth0) ...
dnsmasq-dhcp: 4189128979 DHCPREQUEST(eth0) 172.22.10.57 76:67:f6:ad:41:70
dnsmasq-dhcp: 4189128979 DHCPACK(eth0) 172.22.10.57 76:67:f6:ad:41:70
dnsmasq-dhcp: 4189128979 sent size: 4 option: 51 lease-time 12h
租约落盘验证:
cat /var/lib/misc/dnsmasq.leases
实测回显:
1791558447 76:67:f6:ad:41:70 172.22.10.57 * 01:76:67:f6:ad:41:70
💡 说明:租约文件行为
[到期时间戳] [MAC] [IP] [主机名] [clientID]。第一列 1791558447 为租约到期时间的 Unix 时间戳。
七、常见问题(FAQ)
7.1 Q1:启动失败,报「Address already in use」(53 端口冲突)
现象:
dnsmasq: failed to create listening socket for port 53: Address already in use
dnsmasq[503]: FAILED to start up
原因:Ubuntu 22.04 默认运行 systemd-resolved,其 stub 监听器占用 127.0.0.53:53;dnsmasq 默认绑定通配地址 0.0.0.0:53 与之冲突。可用 ss -ulpn | grep ':53' 确认占用者:
users:(("systemd-resolve",pid=39,fd=13))
解法:在 /etc/dnsmasq.conf 绑定到具体接口并显式监听局域网 IP(见 4.3),即 interface=eth0 + bind-interfaces + listen-address=<本机IP>,随后 sudo systemctl restart dnsmasq。
7.2 Q2:重启后 DHCP 客户端分不到 IP
先确认 dhcp-range 与客户端在同一网段、interface 指向了实际承载客户端的接口;再 journalctl -u dnsmasq 看是否有 DHCPDISCOVER 到达;若完全没有请求记录,多半是 interface 指错。若抢不过局域网内其他 DHCP 服务器,可在配置加 dhcp-authoritative 声明本机为权威服务器。
7.3 Q3:服务已 running 但 dig 无响应
确认 dnsmasq 的实际监听地址与 dig 目标地址一致(listen-address 决定)。若只监听 127.0.0.1,则需改用 dig @127.0.0.1;想让局域网其他主机也能查询,listen-address 必须写服务器局域网 IP(如 172.17.0.14)。
7.4 Q4:systemd-resolved 是否必须停用
不必停用。只要用 bind-interfaces + interface= 把 dnsmasq 限到具体接口,即可与 systemd-resolved 共存。若确要彻底关闭 DNS stub,可 sudo systemctl stop systemd-resolved && sudo systemctl disable systemd-resolved,并手动写 /etc/resolv.conf 指向真实上游(如 192.168.146.2)。
八、卸载方式
8.1 停止并禁用服务
sudo systemctl stop dnsmasq
sudo systemctl disable dnsmasq
实测回显:
Synchronizing state of dnsmasq.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable dnsmasq
Removed /etc/systemd/system/multi-user.target.wants/dnsmasq.service.
8.2 卸载并清除配置
sudo apt remove --purge -y dnsmasq dnsmasq-base
实测回显:
Removing dnsmasq (2.91-0ubuntu0.22.04.2) ...
Removing dnsmasq-base (2.91-0ubuntu0.22.04.2) ...
Purging configuration files for dnsmasq-base (2.91-0ubuntu0.22.04.2) ...
Purging configuration files for dnsmasq (2.91-0ubuntu0.22.04.2) ...
💡 说明:
--purge会连配置文件一并删除。若只想保留配置再装回,可用sudo apt remove dnsmasq(不带 --purge)。
九、参考链接
- dnsmasq 官方主页:https://thekelleys.org.uk/dnsmasq/doc.html
- dnsmasq man 手册:
man 8 dnsmasq - Ubuntu 软件包页:https://packages.ubuntu.com/jammy/dnsmasq
- 配置文件示例见 /etc/dnsmasq.conf 自带注释