DNS服务搭建

一、搭建环境选择

常见 DNS 服务器软件有:

  • Bind9 (功能全面,企业常用)
  • dnsmasq (轻量级,适合小型网络、开发环境)
  • Unbound (专注缓存递归解析,性能高)
  • CoreDNS (Go 写的,云原生、K8s 默认用)

二、Bind9 搭建

1. 安装

apt update
apt install -y bind9 bind9-utils

2. 配置主配置文件

编辑 /etc/bind/named.conf.options

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

    recursion yes;               // 开启递归查询
    allow-query { any; };        // 允许所有人查询,也可以限制内网段
    forwarders { 8.8.8.8; 1.1.1.1; };  // 上游DNS

    dnssec-validation no;
    listen-on { any; };
};

3. 配置一个自定义域

编辑 /etc/bind/named.conf.local

zone "test.local" {
    type master;
    file "/etc/bind/db.test.local";
};

4. 创建区域文件

/etc/bind/db.test.local

$TTL    604800
@       IN      SOA     ns1.test.local. root.test.local. (
                        2         ; Serial
                        604800    ; Refresh
                        86400     ; Retry
                        2419200   ; Expire
                        604800 )  ; Negative Cache TTL

; DNS 服务器
@       IN      NS      ns1.test.local.

; 主机记录
ns1     IN      A       192.168.1.10
www     IN      A       192.168.1.20
db      IN      A       192.168.1.30

5. 检查配置

named-checkconf
named-checkzone test.local /etc/bind/db.test.local

6. 重启服务

systemctl restart bind9
systemctl enable bind9

7. 客户端测试

修改客户端 /etc/resolv.conf

nameserver 10.0.8.10

然后测试

dig @10.0.8.10 www.test.local

三、dnsmasq 搭建(轻量级)

如果只是内网用,可以装 dnsmasq

apt install -y dnsmasq

配置 /etc/dnsmasq.conf

# 监听的网卡(只监听内网网卡,安全)
interface=ens33
listen-address=127.0.0.1,10.0.8.10

# 主机名解析
address=/docker/10.0.8.9
# 自定义域名解析
address=/www.test.local/10.0.8.10

# 转发未知域名到公网 DNS
server=223.5.5.5
server=114.114.114.114
server=8.8.8.8

重启

systemctl restart dnsmasq

四、生产建议

  • 企业内网域名解析:用Bind9 或 CoreDNS
  • 缓存加速:用Unbound
  • 学习,开发环境:用dnsmasq

本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/zhaohaiqi/articles/19042129

posted @ 2025-08-16 14:45  DevOps_node  阅读(135)  评论(0)    收藏  举报