BIND9 DNS 服务器搭建教程

BIND(Berkeley Internet Name Domain)是全球使用最广泛的 DNS 服务器软件,互联网上的根域名服务器大部分都在运行 BIND。BIND9 是其最新的主要版本,功能全面、配置灵活,支持权威 DNS、递归 DNS、DNSSEC 等所有 DNS 功能。本文将介绍如何在搬瓦工 VPS 上搭建 BIND9 DNS 服务器,用于托管自己的域名解析或构建私有 DNS 基础设施。

一、安装 BIND9

Ubuntu/Debian

apt update
apt install bind9 bind9-utils bind9-dnsutils -y

CentOS

yum install bind bind-utils -y

查看安装版本:

named -v

二、基础配置

BIND9 的主配置文件在 Ubuntu 上位于 /etc/bind/named.conf,在 CentOS 上位于 /etc/named.conf

2.1 配置全局选项

cat > /etc/bind/named.conf.options <<EOF
options {
    directory "/var/cache/bind";

    listen-on { any; };
    listen-on-v6 { any; };

    allow-query { any; };
    allow-recursion { 127.0.0.1; 10.0.0.0/8; 172.16.0.0/12; 192.168.0.0/16; };

    forwarders {
        8.8.8.8;
        8.8.4.4;
    };

    dnssec-validation auto;

    recursion yes;

    version "not disclosed";

    querylog yes;
};
EOF

三、配置正向解析区域

添加域名区域声明:

cat >> /etc/bind/named.conf.local <<EOF
zone "example.com" {
    type master;
    file "/etc/bind/zones/db.example.com";
    allow-transfer { 从服务器IP; };
};
EOF

创建区域文件:

mkdir -p /etc/bind/zones
cat > /etc/bind/zones/db.example.com <<EOF
\$TTL    604800
@       IN      SOA     ns1.example.com. admin.example.com. (
                        2024010101      ; Serial
                        3600            ; Refresh
                        1800            ; Retry
                        604800          ; Expire
                        86400 )         ; Negative Cache TTL
;
@       IN      NS      ns1.example.com.
@       IN      NS      ns2.example.com.
@       IN      A       你的VPS公网IP
ns1     IN      A       你的VPS公网IP
ns2     IN      A       从服务器IP
www     IN      A       你的VPS公网IP
mail    IN      A       你的VPS公网IP
@       IN      MX  10  mail.example.com.
EOF

四、配置反向解析区域

cat >> /etc/bind/named.conf.local <<EOF
zone "1.168.192.in-addr.arpa" {
    type master;
    file "/etc/bind/zones/db.192.168.1";
};
EOF
cat > /etc/bind/zones/db.192.168.1 <<EOF
\$TTL    604800
@       IN      SOA     ns1.example.com. admin.example.com. (
                        2024010101      ; Serial
                        3600            ; Refresh
                        1800            ; Retry
                        604800          ; Expire
                        86400 )         ; Negative Cache TTL
;
@       IN      NS      ns1.example.com.
10      IN      PTR     www.example.com.
11      IN      PTR     mail.example.com.
EOF

五、配置主从同步

在从服务器上配置为 slave 类型:

zone "example.com" {
    type slave;
    file "/var/cache/bind/db.example.com";
    masters { 主服务器IP; };
};

每次修改主服务器的区域文件后,需要递增 Serial 序列号,从服务器会根据序列号变化自动同步。

六、验证和启动

6.1 检查配置语法

named-checkconf
named-checkzone example.com /etc/bind/zones/db.example.com

6.2 启动服务

systemctl restart bind9
systemctl enable bind9
systemctl status bind9

6.3 测试解析

dig @127.0.0.1 example.com A
dig @127.0.0.1 www.example.com A
dig @127.0.0.1 -x 192.168.1.10

七、日志配置

配置详细的 DNS 查询日志,用于排查问题和安全审计:

logging {
    channel query_log {
        file "/var/log/bind/query.log" versions 5 size 50m;
        severity info;
        print-time yes;
        print-category yes;
    };
    category queries { query_log; };
};
mkdir -p /var/log/bind
chown bind:bind /var/log/bind

八、安全加固

  • 限制递归查询:只允许信任的 IP 范围使用递归查询,避免被用作 DNS 放大攻击的跳板。
  • 隐藏版本号:在 options 中设置 version "not disclosed";
  • 启用 TSIG 密钥:用于主从同步的身份认证。
  • 限制区域传输:使用 allow-transfer 限制哪些 IP 可以获取完整区域数据。

生成 TSIG 密钥

tsig-keygen -a hmac-sha256 transfer-key > /etc/bind/transfer.key

九、防火墙配置

ufw allow 53/tcp
ufw allow 53/udp

十、常见问题

SERVFAIL 错误

通常是区域文件语法错误或 DNSSEC 验证失败,查看日志排查:

journalctl -u bind9 -n 50

区域传输失败

检查 allow-transfer 配置和防火墙规则,确保 TCP 53 端口可达。

总结

BIND9 是最全面的 DNS 服务器解决方案,适合需要完整 DNS 功能的场景。在搬瓦工 VPS 上部署 BIND9 可以用于托管自己的域名权威解析或搭建私有递归 DNS。如果只需要轻量级的 DNS 服务,可以参考 CoreDNS 教程Dnsmasq 教程。购买搬瓦工 VPS 请查看 全部方案,使用优惠码 NODESEEK2026 可享 6.77% 折扣,购买链接:bwh81.net

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。