Ubuntu 22.04 安装 Unbound 指南
Ubuntu 22.04 下 apt 官方仓库与源码编译两种方式安装 Unbound 递归 DNS 解析服务器的完整指南,含最小 unbound.conf 配置、systemd 托管、dig 验证回显、FAQ 与卸载。
Ubuntu 22.04 安装 Unbound 指南
目标:在 Ubuntu 22.04 上以「apt 官方仓库」与「官方源码编译」两种方式从零安装 Unbound 递归 DNS 解析服务器,完成最小 unbound.conf 配置、systemd 托管、dig 递归验证与卸载。已在 Ubuntu 22.04.5 LTS / x86_64 环境实测通过(系统 unbound 1.13.1、源码 unbound 1.19.3)。
一、环境信息
- 操作系统:Ubuntu 22.04.5 LTS(
PRETTY_NAME) - 架构:x86_64
- 系统仓库内置 Unbound 版本:
1.13.1-1ubuntu5.16 - 官方源码最新版本:
1.19.3 - 默认配置目录:
/etc/unbound/ - 默认数据目录:
/var/lib/unbound/(root.key 信任锚) - 监听端口:53(UDP/TCP)
- 校验工具:
unbound-checkconf、unbound-control、dig(来自 bind9-dnsutils)
二、前置条件
- root 权限(或 sudo)执行安装与配置;
- 可联网(apt 下载软件包需访问 archive.ubuntu.com;递归解析需访问根服务器);
- 端口 53 未被占用;
- systemd 托管方式需运行在含 systemd 的 Linux 真机(Ubuntu 22.04 桌面/服务版均满足)。
三、版本选型说明
Unbound 在 Ubuntu 各 LTS 的 apt 仓库中版本不同,常用情况如下表:
| 发行版 | apt 仓库内置版本(实测) | 说明 |
|---|---|---|
| Ubuntu 22.04(Jammy) | 1.13.1-1ubuntu5.16 | 较旧但稳定,官方长期维护 |
| Ubuntu 24.04(Noble) | 更高版本 | 视仓库实际为准 |
💡 选择建议:求稳定与安全修复省心,直接用 apt 仓库版本(1.13.1);需要较新特性(如更高并发、新算法、更全的 DNSSEC 支持),则用「官方源码编译」方式安装 1.19.3。两种方式本质都是同一个 unbound 程序,配置文件语法完全一致,可互相替换。
四、方案A:apt 官方仓库安装(推荐)
4.1 更新软件源
apt-get update
4.2 安装 unbound 与 dnsutils
apt-get install -y unbound dnsutils
实测关键回显:
Setting up libunbound8:amd64 (1.13.1-1ubuntu5.16) ...
Setting up unbound-anchor (1.13.1-1ubuntu5.16) ...
Setting up unbound (1.13.1-1ubuntu5.16) ...
Setting up bind9-dnsutils (1:9.18.39-0ubuntu0.22.04.6) ...
Setting up dnsutils (1:9.18.39-0ubuntu0.22.04.6) ...
4.3 确认安装版本
unbound -V | head -3
实测回显:
Version 1.13.1
已装入的软件包:
dpkg -l | grep -E "unbound|dnsutils" | awk '{print $1, $2, $3}'
实测回显:
ii bind9-dnsutils 1:9.18.39-0ubuntu0.22.04.6
ii dnsutils 1:9.18.39-0ubuntu0.22.04.6
ii libunbound8:amd64 1.13.1-1ubuntu5.16
ii unbound 1.13.1-1ubuntu5.16
ii unbound-anchor 1.13.1-1ubuntu5.16
五、方案B:官方源码编译安装(新版本)
⚠️ 前置要求:需要
build-essential(gcc/make)、flex、bison、libssl-dev、libexpat1-dev、libevent-dev等编译依赖,以及网络下载官方源码包。
5.1 安装编译依赖
apt-get install -y wget build-essential flex bison libssl-dev libexpat1-dev libevent-dev
5.2 下载官方源码包并解压
以下下载地址拼接规则为 https://www.nlnetlabs.nl/downloads/unbound/unbound-<版本>.tar.gz,版本号以官网 Release 为准:
cd /tmp
wget https://www.nlnetlabs.nl/downloads/unbound/unbound-1.19.3.tar.gz
tar xzf unbound-1.19.3.tar.gz
cd unbound-1.19.3
实测解压后可看到顶层目录 unbound-1.19.3。
5.3 configure 配置(设置安装前缀)
./configure --prefix=/usr/local/unbound
实测收尾回显:
config.status: creating contrib/unbound.socket
config.status: creating contrib/unbound.service
config.status: creating contrib/unbound_portable.service
config.status: creating config.h
config.status: executing libtool commands
💡 说明:安装前缀选
/usr/local/unbound避免与 apt 版本冲突,二进制落在/usr/local/unbound/sbin/,配置目录仍指向/etc/unbound/(可自建维护)。
5.4 编译
make -j2
编译成功无错误后执行安装:
make install
5.5 验证源码安装版本
/usr/local/unbound/sbin/unbound -V | head -2
实测回显:
Version 1.19.3
Configure line: --prefix=/usr/local/unbound
💡 说明:源码安装后需使用完整路径或以
/usr/local/unbound/sbin加入 PATH 调用,unbound-checkconf、unbound-control亦在sbin/与bin/下。
六、配置说明
6.1 认识默认配置文件
Ubuntu 的 unbound 主配置 /etc/unbound/unbound.conf 默认内容为一个 include-toplevel 指向 conf.d 目录:
# Unbound configuration file for Debian.
#
# See the unbound.conf(5) man page.
#
# See /usr/share/doc/unbound/examples/unbound.conf for a commented
# reference config file.
#
# The following line includes additional configuration files from the
# /etc/unbound/unbound.conf.d directory.
include-toplevel: "/etc/unbound/unbound.conf.d/*.conf"
且 conf.d 目录内默认含 root-auto-trust-anchor-file.conf:
server:
# The following line will configure unbound to perform cryptographic
# DNSSEC validation using the root trust anchor.
auto-trust-anchor-file: "/var/lib/unbound/root.key"
⚠️ 注意:
auto-trust-anchor-file指向的/var/lib/unbound/root.key若不存在,unbound-checkconf会报错(实测错误:fatal error: auto-trust-anchor-file: "/var/lib/unbound/root.key" does not exist)。此时需用unbound-anchor生成根信任锚:
unbound-anchor -a /var/lib/unbound/root.key
6.2 最小可运行配置
新建 /etc/unbound/unbound.conf.d/minimal.conf(作为本地递归解析的最小配置):
server:
verbosity: 1
interface: 127.0.0.1
port: 53
access-control: 127.0.0.0/8 allow
do-ip6: no
💡 说明:
do-ip6: no表示不监听 IPv6,纯 IPv4 环境常见设置;access-control限制仅本机可查询,避免开放递归被滥用。
检查配置语法:
unbound-checkconf
实测通过回显:
unbound-checkconf: no errors in /etc/unbound/unbound.conf
6.3 systemd 托管(apt 安装后自带)
apt 安装 unbound 会自动生成 unbound.service 单元,启停与开机自启命令如下:
systemctl enable unbound
systemctl start unbound
systemctl is-active unbound
systemctl status unbound --no-pager
实测回显(enable / is-active / status):
Synchronizing state of unbound.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable unbound
active
● unbound.service - Unbound DNS server
Loaded: loaded (/lib/systemd/system/unbound.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2026-10-09 03:06:28 UTC; 12ms ago
Docs: man:unbound(8)
Process: 653 ExecStartPre=/usr/lib/unbound/package-helper chroot_setup (code=exited, status=0/SUCCESS)
Process: 656 ExecStartPre=/usr/lib/unbound/package-helper root_trust_anchor_update (code=exited, status=0/SUCCESS)
Main PID: 659 (unbound)
Tasks: 1 (limit: 8648)
Memory: 6.7M
CPU: 91ms
CGroup: /system.slice/unbound.service
└─659 /usr/sbin/unbound -d -p
apt 打包的单元文件 /lib/systemd/system/unbound.service 关键内容:
[Unit]
Description=Unbound DNS server
Documentation=man:unbound(8)
After=network.target
Before=nss-lookup.target
Wants=nss-lookup.target
[Service]
Type=notify
Restart=on-failure
EnvironmentFile=-/etc/default/unbound
ExecStartPre=-/usr/lib/unbound/package-helper chroot_setup
ExecStartPre=-/usr/lib/unbound/package-helper root_trust_anchor_update
ExecStart=/usr/sbin/unbound -d -p $DAEMON_OPTS
ExecStopPost=-/usr/lib/unbound/package-helper chroot_teardown
ExecReload=+/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
6.4 递归解析验证(dig)
将本机 DNS 指向 unbound 后,用 dig 验证递归解析(实测走 unbound 递归):
dig @127.0.0.1 www.baidu.com +short
实测回显:
www.a.shifen.com.
110.242.70.57
110.242.69.21
带完整答案的查询:
dig @127.0.0.1 www.qq.com +noall +answer
实测回显:
www.qq.com. 299 IN CNAME ins-r23tsuuf.ias.tencent-cloud.net.
ins-r23tsuuf.ias.tencent-cloud.net. 120 IN A 116.128.170.212
ins-r23tsuuf.ias.tencent-cloud.net. 120 IN A 58.246.163.58
6.5 运行状态确认(unbound-control)
unbound-control status
实测回显:
version: 1.13.1
verbosity: 1
threads: 1
modules: 3 [ subnet validator iterator ]
uptime: 9 seconds
options: reuseport control(ssl)
unbound (pid 637) is running...
七、注意事项
- 递归开放到公网有被滥用风险(放大攻击、缓存投毒),内网请限制
access-control网段,公网场景应配合 ACL、DNSSEC 校验与限流(rate-limit)使用; do-ip6: no会关闭 IPv6 监听与 IPv6 查询,需要 IPv6 环境时移除该行;- 修改
unbound.conf*后务必先unbound-checkconf再systemctl reload unbound(或unbound-control reload); - apt 与源码两套安装并存时,注意 PATH 中
unbound指向哪个版本(用which unbound/unbound -V确认),避免误调用。
八、常见问题(FAQ)
Q1:unbound-checkconf 报 auto-trust-anchor-file: "/var/lib/unbound/root.key" does not exist 怎么办?
A:根信任锚缺失,执行 unbound-anchor -a /var/lib/unbound/root.key 生成后再校验即可。
Q2:invoke-rc.d: policy-rc.d denied execution of start 是什么意思?
A:这是精简/最小化部署环境下「软件包安装后未自动拉起服务」的现象(策略脚本拒绝安装期自动 start)。在正常 Ubuntu 真机(含 systemd)上用 systemctl enable --now unbound 即可正常托管。
Q3:dig 返回 ;; communications error 是什么原因?
A:通常是 unbound 未启动或监听端口不对。先 unbound-checkconf 校验配置,再用 systemctl status unbound 确认 Active,最后用 ss -ulnp | grep 53 查看端口是否被监听。
Q4:如何让 unbound 只对本机生效?
A:interface: 127.0.0.1 + access-control: 127.0.0.0/8 allow 即可仅监听本机回环,外界不可查询,是纯本机递归的最安全形态。
Q5:修改配置后如何热加载?
A:unbound-control reload(保留 process)或 systemctl reload unbound;需要完全重启用 systemctl restart unbound。
九、卸载方式
apt 安装版本卸载:
apt-get remove -y unbound unbound-anchor
apt-get purge -y unbound unbound-anchor # 连同配置文件一起清除
💡 说明:
purge会删除/etc/unbound/配置与/var/lib/unbound/数据,升级/重装前如需保留配置请用remove。
源码编译安装版本卸载:手动删除安装前缀目录即可:
rm -rf /usr/local/unbound
十、参考链接
- 官方源码与 Releases:https://www.nlnetlabs.nl/projects/unbound/
- GitHub 仓库:https://github.com/NLnetLabs/unbound
- unbound.conf 手册:
man unbound.conf - Ubuntu 软件包页面:https://packages.ubuntu.com/jammy/unbound