强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

BIND DNS 服务器搭建完全教程 / 第 03 章:核心配置文件详解

本章概述

named.conf 是 BIND 的核心配置文件,决定了 DNS 服务器的全部行为。本章将逐一讲解配置文件的结构、全局选项(options)、ACL(访问控制列表)、日志配置(logging)、视图(views)和区域定义。


3.1 named.conf 文件结构

3.1.1 整体结构概览

// 1. 全局选项
options { ... };

// 2. ACL 定义
acl "name" { ... };

// 3. TSIG 密钥
key "name" { ... };

// 4. 日志配置
logging { ... };

// 5. 视图(可选)
view "name" { ... };

// 6. 区域定义
zone "example.com" { ... };

// 7. 引入其他文件
include "/etc/bind/named.conf.local";

3.1.2 语法规则

规则说明
//单行注释
/* ... */多行注释
;每条语句以分号结尾
{ }语句块用花括号包裹
关键字不区分大小写但值(如域名)可能区分
引号字符串值用双引号包裹

3.1.3 配置文件分离(推荐实践)

# 主配置文件 /etc/bind/named.conf
include "/etc/bind/named.conf.options";    # 全局选项
include "/etc/bind/named.conf.local";      # 本地区域
include "/etc/bind/named.conf.default-zones";  # 默认区域
include "/etc/bind/named.conf.zones";      # 自定义业务区域(可选)

💡 为什么要分离? 方便版本控制和模块化管理。不同管理员可以负责不同配置文件。


3.2 options 全局选项

3.2.1 完整 options 示例

options {
    // ---- 工作目录 ----
    directory "/var/cache/bind";

    // ---- 网络监听 ----
    listen-on port 53 { 127.0.0.1; 192.168.1.10; };
    listen-on-v6 port 53 { ::1; fd00::1; };

    // ---- 查询控制 ----
    allow-query { localhost; localnets; };
    allow-query-cache { localhost; localnets; };
    recursion yes;
    allow-recursion { localhost; localnets; };

    // ---- 转发 ----
    forwarders {
        8.8.8.8;
        8.8.4.4;
        1.1.1.1;
    };
    forward first;  // 先转发,失败后自行迭代

    // ---- 安全 ----
    version "not disclosed";
    hostname "not disclosed";
    server-id "not disclosed";
    dnssec-validation auto;

    // ---- 性能 ----
    max-cache-size 256m;
    max-cache-ttl 3600;
    max-ncache-ttl 900;
    cleaning-interval 60;
    max-clients-per-query 100;
    max-udp-size 1232;

    // ---- EDNS ----
    edns-udp-size 1232;

    // ---- 文件 ----
    pid-file "/run/named/named.pid";
    session-keyfile "/run/named/session.key";
    managed-keys-directory "/var/cache/bind/managed-keys";
    bindkeys-file "/etc/bind/bind.keys";

    // ---- 其他 ----
    prefetch 2 9;   // 预取:在 TTL 剩余 2 秒且查询次数 >= 9 时预取
    minimal-responses yes;  // 减少响应大小
};

3.2.2 关键选项详解

监听配置

listen-on port 53 { 192.168.1.10; };
参数默认值说明
port 5353监听端口
地址列表127.0.0.1监听的 IPv4 地址
listen-on-v6::1监听的 IPv6 地址

⚠️ 不设 listen-on 的默认行为:只监听 127.0.0.1,外部无法访问。

查询权限控制

allow-query { any; };         // 谁可以查询(权威服务器通常用 any)
allow-query-cache { trusted; }; // 谁可以查看缓存(递归服务器限制)
allow-recursion { trusted; };   // 谁可以使用递归

关系

  • allow-query 控制所有查询(含权威数据 + 缓存数据)
  • allow-query-cache 仅控制缓存查询,覆盖 allow-query 对缓存部分的限制
  • allow-recursion 控制谁可以发起递归查询

转发策略

forwarders { 8.8.8.8; 1.1.1.1; };
forward only;   // 仅转发,失败则返回 SERVFAIL
forward first;  // 先转发,失败后自行迭代查询
模式行为适用场景
only只转发,不自行查询企业内网,上游 DNS 可靠
first先转发,失败自行查询通用,兼顾速度和可靠性
不设转发纯递归,从根开始查完全自主的递归服务器

缓存调优

max-cache-size 256m;    // 最大缓存内存(0 = 无限制)
max-cache-ttl 3600;     // 正响应最大缓存时间(秒)
max-ncache-ttl 900;     // 负响应(NXDOMAIN)最大缓存时间
cleaning-interval 60;   // 缓存清理间隔(分钟)

3.3 ACL(访问控制列表)

3.3.1 语法定义

// 使用 IP 地址和网络
acl "trusted" {
    127.0.0.1;          // 单个 IP
    192.168.1.0/24;     // 子网
    10.0.0.0/8;         // 大子网
    172.16.0.0/12;
    !192.168.1.100;     // 排除特定 IP
};

acl "office-beijing" {
    10.1.0.0/16;
};

acl "office-shanghai" {
    10.2.0.0/16;
};

acl "bad-actors" {
    192.168.100.0/24;   // 已知恶意来源
    10.255.0.0/16;
};

3.3.2 内置 ACL

BIND 预定义了四个内置 ACL:

ACL 名称含义
any所有 IP 地址
none无 IP 地址
localhost本机所有 IP(自动检测)
localnets本机直连的网络

3.3.3 ACL 使用场景

acl "trusted" {
    localhost;
    localnets;
    192.168.0.0/16;
};

options {
    allow-query { any; };               // 权威数据:任何人可查
    allow-query-cache { trusted; };     // 缓存数据:仅信任网络
    allow-recursion { trusted; };       // 递归查询:仅信任网络
    allow-transfer { secondary-servers; }; // 区域传输:仅从服务器
    blackhole { bad-actors; };          // 完全拒绝(不响应)
};

⚠️ ACL 定义顺序很重要:ACL 必须在使用前定义(通常放在 options 前面)。

3.3.4 ACL 与 TSIG 结合

key "transfer-key" {
    algorithm hmac-sha256;
    secret "base64-encoded-secret-here";
};

acl "secondary-servers" {
    192.168.1.11;       // 从服务器 IP
    key transfer-key;   // 也可以按 TSIG 密钥匹配
};

3.4 日志配置(logging)

3.4.1 日志架构

BIND 的日志系统由三个概念组成:

日志事件 → 通道(Channel) → 类别(Category)
  • 通道(Channel):日志输出目标(文件、syslog、stderr)
  • 类别(Category):日志来源分类(查询、解析、区域传输等)

3.4.2 完整日志配置示例

logging {
    // ---- 通道定义 ----
    
    // 默认通道(调试用,生产环境建议关闭或降低级别)
    channel default_log {
        file "/var/log/named/default.log" versions 5 size 50m;
        severity info;
        print-time yes;
        print-severity yes;
        print-category yes;
    };

    // 查询日志
    channel query_log {
        file "/var/log/named/query.log" versions 10 size 100m;
        severity dynamic;
        print-time yes;
        print-severity yes;
        print-category yes;
    };

    // 安全日志
    channel security_log {
        file "/var/log/named/security.log" versions 10 size 50m;
        severity info;
        print-time yes;
        print-severity yes;
        print-category yes;
    };

    // 区域传输日志
    channel transfer_log {
        file "/var/log/named/transfer.log" versions 5 size 20m;
        severity info;
        print-time yes;
        print-severity yes;
        print-category yes;
    };

    // 动态更新日志
    channel update_log {
        file "/var/log/named/update.log" versions 5 size 20m;
        severity info;
        print-time yes;
        print-severity yes;
        print-category yes;
    };

    // DNSSEC 日志
    channel dnssec_log {
        file "/var/log/named/dnssec.log" versions 5 size 20m;
        severity info;
        print-time yes;
        print-severity yes;
        print-category yes;
    };

    // sysylog 通道示例
    channel syslog_channel {
        syslog daemon;
        severity info;
    };

    // ---- 类别绑定 ----
    category default { default_log; };
    category queries { query_log; };
    category security { security_log; };
    category xfer-in { transfer_log; };
    category xfer-out { transfer_log; };
    category update { update_log; };
    category update-security { update_log; security_log; };
    category dnssec { dnssec_log; };
    category lame-servers { null; };         // 丢弃(减少噪音)
    category edns-disabled { null; };        // 丢弃
};

3.4.3 日志通道选项

选项说明示例
file输出到文件file "/var/log/named/query.log"
versions保留历史文件数量versions 10
size单个文件最大大小size 100m
severity日志级别severity info
print-time是否输出时间戳yes
print-severity是否输出级别yes
print-category是否输出类别yes
syslog输出到 syslogsyslog daemon
null丢弃日志null

3.4.4 日志级别

从低到高:

级别数值说明
critical1致命错误
error2错误
warning3警告
notice4重要通知
info5一般信息
debug [N]6-99调试(级别越高越详细)
dynamic-跟随 rndc trace 级别动态调整

3.4.5 常用日志类别

类别说明是否默认开启
default默认
queries查询日志❌(需手动开启)
security安全事件
update动态更新
xfer-in/xfer-out区域传输
dnssecDNSSEC 操作
resolver递归解析
network网络事件
lame-serverslame 服务器错误✅(很吵)
client客户端操作

💡 生产环境建议:关闭 lame-serversedns-disabled(设为 null;),它们产生大量无用日志。


3.5 视图(Views)基础

详细内容第 08 章,此处仅作基本语法介绍。

// 内网视图 —— 允许递归,解析内部域名
view "internal" {
    match-clients { trusted; };
    recursion yes;

    zone "example.com" {
        type primary;
        file "internal/example.com.zone";
    };

    zone "." {
        type hint;
        file "db.root";
    };
};

// 外网视图 —— 仅返回权威数据
view "external" {
    match-clients { any; };
    recursion no;

    zone "example.com" {
        type primary;
        file "external/example.com.zone";
    };
};

⚠️ 使用 views 的注意事项:一旦使用 view 语句,所有 zone 都必须在 view 内部定义,不能有全局 zone


3.6 TSIG 密钥定义

// 生成 TSIG 密钥:
// tsig-keygen -a hmac-sha256 transfer-key

key "transfer-key" {
    algorithm hmac-sha256;
    secret "aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789=";
};

详细使用见 第 09 章(区域传输)第 07 章(动态更新)


3.7 完整配置模板

3.7.1 递归服务器配置模板

// /etc/bind/named.conf —— 递归 DNS 服务器

// ACL 定义
acl "trusted" {
    localhost;
    localnets;
    192.168.0.0/16;
    10.0.0.0/8;
};

// 全局选项
options {
    directory "/var/cache/bind";

    // 监听配置
    listen-on port 53 { any; };
    listen-on-v6 port 53 { any; };

    // 递归配置
    recursion yes;
    allow-recursion { trusted; };
    allow-query { trusted; };
    allow-query-cache { trusted; };

    // 转发器
    forwarders {
        8.8.8.8;
        1.1.1.1;
    };
    forward first;

    // 安全
    version "not disclosed";
    hostname "not disclosed";
    server-id "not disclosed";
    dnssec-validation auto;

    // 性能
    max-cache-size 512m;
    max-cache-ttl 3600;
    max-ncache-ttl 900;
    minimal-responses yes;

    // 文件
    pid-file "/run/named/named.pid";
    session-keyfile "/run/named/session.key";
};

// 日志
logging {
    channel default_log {
        file "/var/log/named/default.log" versions 5 size 50m;
        severity info;
        print-time yes;
        print-severity yes;
        print-category yes;
    };
    channel query_log {
        file "/var/log/named/query.log" versions 10 size 100m;
        severity dynamic;
        print-time yes;
    };
    category default { default_log; };
    category queries { query_log; };
    category lame-servers { null; };
    category edns-disabled { null; };
};

// 根提示
zone "." {
    type hint;
    file "db.root";
};

// 本地区域
zone "localhost" {
    type primary;
    file "db.local";
};

zone "127.in-addr.arpa" {
    type primary;
    file "db.127";
};

// RFC 1918 反向区域(推荐作为权威服务器)
zone "168.192.in-addr.arpa" {
    type primary;
    file "db.192.168";
    allow-query { trusted; };
};

3.7.2 权威服务器配置模板

// /etc/bind/named.conf —— 权威 DNS 服务器

acl "secondary-servers" {
    192.168.1.11;   // 从服务器 IP
};

options {
    directory "/var/cache/bind";

    // 监听所有地址
    listen-on port 53 { any; };
    listen-on-v6 port 53 { any; };

    // 禁用递归(纯权威)
    recursion no;
    allow-query { any; };
    allow-query-cache { none; };

    // 安全
    version "not disclosed";
    dnssec-validation no;

    // 区域传输控制
    allow-transfer { secondary-servers; };

    // 文件
    pid-file "/run/named/named.pid";
    session-keyfile "/run/named/session.key";
};

logging {
    channel default_log {
        file "/var/log/named/default.log" versions 5 size 50m;
        severity info;
        print-time yes;
        print-severity yes;
        print-category yes;
    };
    category default { default_log; };
    category lame-servers { null; };
};

zone "example.com" {
    type primary;
    file "primary/example.com.zone";
    allow-transfer { secondary-servers; };
    also-notify { 192.168.1.11; };
};

3.8 配置检查工具

# 检查 named.conf 语法
sudo named-checkconf /etc/bind/named.conf

# 检查区域文件语法
sudo named-checkzone example.com /var/cache/bind/primary/example.com.zone

# 检查配置并显示加载的区域列表
sudo named-checkconf -l /etc/bind/named.conf

# 输出配置的解析结果(用于排查 include 问题)
sudo named-checkconf -p /etc/bind/named.conf

3.9 本章小结

配置块作用关键选项
options全局行为recursion, allow-query, forwarders, listen-on
acl访问控制IP/子网匹配,与 allow-* 配合
logging日志管理通道(文件/syslog)+ 类别绑定
view视图match-clients 匹配不同客户端返回不同结果
zone区域定义type(primary/secondary/hint/forward)
keyTSIG 密钥algorithm, secret

💡 小技巧

  1. 每次修改配置后:先运行 named-checkconf,再 rndc reload
  2. 查询日志会严重影响性能:生产环境慎用,调试时用 rndc querylog on 临时开启。
  3. ACL 中的 ! 表示排除:匹配规则从上到下,先匹配先生效。
  4. 日志文件的 versions 参数:自动轮转,无需 logrotate,但建议仍配置 logrotate 作为后备。
  5. minimal-responses yes 可以减少 DNS 响应大小,缓解放大攻击。

📖 扩展阅读