Akemi

BIND搭建主从DNS权威服务器

2025/04/01

基础配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

/# 安装
yum -y install bind

# 配置文件/etc/named.conf
options {
# 监听IPv4地址网卡的地址的53端口
listen-on port 53 { 10.163.2.100; };
listen-on-v6 port 53 { ::1; };

# 定义BIND的工作目录,区域文件默认存放路径
directory "/var/named";
# 缓存转储文件路径(当执行`rndc dumpdb`时生成,用于调试缓存数据)
dump-file "/var/named/data/cache_dump.db";
# 统计信息文件路径(当执行`rndc stats`时生成,记录DNS查询统计)
statistics-file "/var/named/data/named_stats.txt";
# 内存统计文件路径(记录BIND内存使用情况)
memstatistics-file "/var/named/data/named_mem_stats.txt";
# 安全根(Security Roots)记录文件路径(DNSSEC相关数据)
secroots-file "/var/named/data/named.secroots";
# 递归查询跟踪文件路径(记录正在进行的递归查询信息)
recursing-file "/var/named/data/named.recursing";

# 允许发起DNS查询的客户端IP范围localhost本地主机
allow-query { any; };
};

配置本地zone——单DNS权威服务器

zone类型

类型 用途 示例场景
master 主 Zone,直接管理 DNS 数据(读写权限) 本地权威域名(如内部网络 corp.local
slave 从 Zone,从主 Zone 同步数据 配置 DNS 冗余服务器
hint 根 Zone,用于初始化根域名服务器列表 type hint; file "named.ca";
forward 转发 Zone,将查询请求转发到其他 DNS 服务器 将特定域名解析委托给上游 DNS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 配置文件添加本地区域(zone)*/

/* 正向 Zone(域名解析)
禁止动态更新(安全加固)*/
zone "wangsheng.com" IN {
type master;
file "wangsheng.com.zone";
allow-update { none; };
};

/* 反向 Zone(IP 10.163.2.0/24 的反向解析)*/
zone "2.163.10.in-addr.arpa" IN {
type master;
file "10.163.2.rev";
};

/* 反向 Zone(IP 1.1.1.0/24 的反向解析)*/
zone "1.1.1.in-addr.arpa" IN {
type master;
file "1.1.1.rev";
};

添加zone文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 进入dns工作目录
cd /var/named/

# 添加正向解析zone
vim wangsheng.com.zone

$TTL 3h ←TTL
wangsheng.com. IN SOA ns.wangsheng.com. root.wangsheng.com.( ←记录主体
1 ←主从标识,这个号码越大越主,每次修改都要+1
1h ←slave隔多久进行数据同步
1h ←更新失败后,隔多久进行重试
1h ←隔多久未能取得同步,则放弃重试
1h) ←TTL值

wangsheng.com. IN NS ns.wangsheng.com.
ns.wangsheng.com. IN A 10.163.2.100
www.wangsheng.com. IN A 1.1.1.1
ftp.wangsheng.com. IN A 1.1.1.2

wangsheng.com. IN MX 0 mail.wangsheng.com.
mail.wangsheng.com. IN A 1.1.1.3

www1.wangsheng.com. IN CNAME www.wangsheng.com.
ftp1.wangsheng.com. IN CNAME ftp.wangsheng.com.

# 添加反向解析zone
vim 10.163.2.rev
$TTL 3h
2.163.10.in-addr.arpa. IN SOA ns.wangsheng.com. root.wangsheng.com.(
1
1h
1h
1h
1h)
2.163.10.in-addr.arpa. IN NS ns.wangsheng.com.
100.2.163.10.in-addr.arpa. IN PTR ns.wangsheng.com.

vim 1.1.1.rev
$TTL 3h
1.1.1.in-addr.arpa. IN SOA ns.wangsheng.com. root.wangsheng.com.(
1
1h
1h
1h
1h)
1.1.1.in-addr.arpa. IN NS ns.wangsheng.com.
1.1.1.1.in-addr.arpa. IN PTR www.wangsheng.com.
1.1.1.2.in-addr.arpa. IN PTR ftp.wangsheng.com.

# 启动服务
systemctl enable named --now

netstat -tunlp |grep :53
tcp 0 0 10.163.2.100:53 0.0.0.0:* LISTEN 1798/named
tcp6 0 0 ::1:53 :::* LISTEN 1798/named

另一种zone文件写法

使用@直接代指域,更加清晰

1
2
3
4
5
6
7
8
9
10
11
12
vim 1.1.1.rev
$TTL 3h
@ IN SOA ns.wangsheng.com. root.wangsheng.com. (
1
1h
1h
1h
1h )

@ IN NS ns.wangsheng.com.
1 IN PTR www.wangsheng.com. ; 1.1.1.1反向解析
2 IN PTR ftp.wangsheng.com. ; 1.1.1.2反向解析

解析测试

进入dns-client,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat /etc/resolv.conf
nameserver 10.163.2.100

host www.wangsheng.com
www.wangsheng.com has address 1.1.1.1

host ftp.wangsheng.com
ftp.wangsheng.com has address 1.1.1.2
host ftp1.wangsheng.com
ftp1.wangsheng.com is an alias for ftp.wangsheng.com.
ftp.wangsheng.com has address 1.1.1.2

host wangsheng.com
wangsheng.com mail is handled by 0 mail.wangsheng.com.
host mail.wangsheng.com
mail.wangsheng.com has address 1.1.1.3

配置DNS主从权威服务器

修改DNS主权威服务器配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/etc/named.conf
zone "wangsheng.com" IN {
type master;
file "wangsheng.com.zone";
allow-update { none; };
allow-transfer { 10.163.2.102; }; ;主动通知从服务器
also-notify { 10.163.2.102; };
};
zone "2.163.10.in-addr.arpa" IN {
type master;
file "10.163.2.rev";
allow-transfer { 10.163.2.102; };
also-notify { 10.163.2.102; };
};
zone "1.1.1.in-addr.arpa" IN {
type master;
file "1.1.1.rev";
allow-transfer { 10.163.2.102; };
also-notify { 10.163.2.102; };
};

/var/named/wangsheng.com.zone
$TTL 3h
@ IN SOA ns.wangsheng.com. root.wangsheng.com. (
1 ; Serial
1h ; Refresh
1h ; Retry
1h ; Expire
1h ; Negative Cache TTL
)

@ IN NS ns.wangsheng.com.

ns IN A 10.163.2.100 ; ns.wangsheng.com
www IN A 1.1.1.1 ; www.wangsheng.com
ftp IN A 1.1.1.2 ; ftp.wangsheng.com

@ IN MX 0 mail ; mail.wangsheng.com
mail IN A 1.1.1.3 ; mail.wangsheng.com

www1 IN CNAME www ; www1.wangsheng.com → www.wangsheng.com
ftp1 IN CNAME ftp ; ftp1.wangsheng.com → ftp.wangsheng.com

/var/named/10.163.2.rev
$TTL 3h
@ IN SOA ns.wangsheng.com. root.wangsheng.com. (
1
1h
1h
1h
1h)
@ IN NS ns.wangsheng.com.
100 IN PTR ns.wangsheng.com.

/var/named/1.1.1.rev
$TTL 3h
@ IN SOA ns.wangsheng.com. root.wangsheng.com. (
1
1h
1h
1h
1h)
@ IN NS ns.wangsheng.com.
1 IN PTR www.wangsheng.com.
2 IN PTR ftp.wangsheng.com.

# 重启服务
systemctl restart named

配置DNS从权威服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
yum -y install bind
vim /etc/named.conf
listen-on port 53 { 10.163.2.102; };
allow-query { any; };
......
zone "wangsheng.com" IN {
type slave;
file "wangsheng.com.zone";
masters { 10.163.2.100; };
also-notify { 10.163.2.100; };
};
zone "2.163.10.in-addr.arpa" IN {
type slave;
file "10.163.2.rev";
masters { 10.163.2.100; };
also-notify { 10.163.2.100; };
};
zone "1.1.1.in-addr.arpa" IN {
type slave;
file "1.1.1.rev";
masters { 10.163.2.100; };
also-notify { 10.163.2.100; };
};

named-checkconf
systemctl enable named --now

netstat -tunlp | grep :53
tcp 0 0 10.163.2.102:53 0.0.0.0:* LISTEN 1733/named
tcp6 0 0 ::1:53 :::* LISTEN 1733/named

此时查看/var/named会发现slave已经从master同步了dns资源记录
ls /var/named/
10.163.2.rev data named.ca named.localhost slaves
1.1.1.rev dynamic named.empty named.loopback wangsheng.com.zone

且无法编辑,因为同步过来是二进制文件

解析测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
修改dns-clinet的dns服务器为slave服务器
cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 10.163.2.102
nameserver 10.163.2.100

先找从,再找主,以此实现高可用

dig ftp1.wangsheng.com
; <<>> DiG 9.16.23-RH <<>> ftp1.wangsheng.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32042
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: cde7320eb24168480100000067eb8aaa6dcd3cbfa42b3ac8 (good)
;; QUESTION SECTION:
;ftp1.wangsheng.com. IN A

;; ANSWER SECTION:
ftp1.wangsheng.com. 10800 IN CNAME ftp.wangsheng.com.
ftp.wangsheng.com. 10800 IN A 1.1.1.2

;; Query time: 0 msec
;; SERVER: 10.163.2.102#53(10.163.2.102)
;; WHEN: Tue Apr 01 02:41:46 EDT 2025
;; MSG SIZE rcvd: 109

可以看到解析是从10.163.2.102也就是slaveDNS权威服务器来的
CATALOG
  1. 1. 基础配置信息
  2. 2. 配置本地zone——单DNS权威服务器
    1. 2.1. zone类型
    2. 2.2. 另一种zone文件写法
    3. 2.3. 解析测试
  3. 3. 配置DNS主从权威服务器
    1. 3.1. 修改DNS主权威服务器配置
    2. 3.2. 配置DNS从权威服务器
    3. 3.3. 解析测试