建立完全不依赖第三方的 DNS 查询服务器,即一个递归解析器(recursive resolver),它会自己从根域名服务器(Root Servers)开始,逐级查询 .com、example.com、www.example.com 等,完全不依赖 Cloudflare、Google、ISP 等第三方。
需要在安全组开放TCP53和UDP53端口!!!!
✅ 自建独立 DNS 递归服务器(不依赖第三方)
使用 BIND9 实现递归 DNS 解析,只查询官方根服务器,以下是完整步骤。
🧱 1. 安装 BIND9
sudo apt update
sudo apt install bind9 bind9-utils bind9-dnsutils
如果遇到以下错误:
root@********************:~# apt install bind9 bind9-utils bind9-dnsutils
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package bind9-utils
E: Unable to locate package bind9-dnsutils
说明你遇到的错误是 apt 无法找到 bind9-utils 和 bind9-dnsutils 这两个软件包,可能是以下几种原因导致:
使用正确的包名(Ubuntu 20.04+ 改了名字)
在 Ubuntu 20.04+(含更高版本) 中,这些工具被整合到了 bind9-dnsutils 包中,或者你只需要安装:
sudo apt install bind9 dnsutils
更新软件包列表再试一次
如果包名没问题但仍无法找到,先更新你的包列表:
sudo apt update
然后再执行安装:
sudo apt install bind9 dnsutils
检查是否启用了 universe 仓库(可选)
bind9 和相关工具通常在 main 或 universe 仓库中。如果系统是云服务器或精简镜像,可能没启用:
运行以下命令启用 universe:
sudo add-apt-repository universe
sudo apt update
再安装:
sudo apt install bind9 dnsutils
检查可用包名(验证)
你可以通过以下命令搜索与 BIND 相关的包:
apt search bind9
输出中会显示实际可用的包名,例如:
-
bind9 -
bind9-host -
dnsutils(含 dig) -
bind9-doc -
bind9-libs
📝 2. 配置 named.conf.options
编辑主配置文件:
sudo nano /etc/bind/named.conf.options
使用以下配置:
options {
directory "/var/cache/bind";
// 不使用 forwarders,表示自己递归
recursion yes;
allow-query { 127.0.0.1; };
listen-on { 127.0.0.1; };
// DNSSEC 验证(可选,推荐开启)
dnssec-validation auto;
auth-nxdomain no;
};
📂 3. 检查根提示文件 root hints(默认已包含)
BIND 默认使用 /etc/bind/db.root 中的根服务器列表。如果你想更新它(可选),可以:
wget -O /etc/bind/db.root https://www.internic.net/domain/named.cache
在 named.conf 中通常已有此引用:
zone "." {
type hint;
file "/etc/bind/db.root";
};
✅ 4. 检查配置 & 重启服务
检查配置文件是否正确:
sudo named-checkconf
重启服务:
sudo systemctl restart bind9
🧪 5. 测试递归解析
执行:
dig @127.0.0.1 www.google.com
你应该会看到类似以下输出(查询耗时较长,首次需逐级递归):
;; Query time: 300 ms
;; SERVER: 127.0.0.1#53(127.0.0.1)
并不应出现 SERVER: 1.1.1.1 或类似第三方地址。
📌 注意事项
-
这是真实递归解析,首次解析会慢(例如
dig www.microsoft.com可能花几百毫秒甚至秒级),但第二次会缓存,速度快。 -
你可以通过
rndc flush清除缓存测试效果。 -
如果你想支持本地局域网也使用它,只需在配置中放宽
allow-query和listen-on绑定的地址即可。
