05实现子域
子域概述
假设子域 sh.xiangzheng.com(上海子域),那么:
- com 为 根的子域
- xiangzheng.com, xiangzheng 为 com 的子域
- sh.xiangzheng.com,sh 为 xiangzheng 的子域
子域范例
- 将子域委派给其它主机管理,实现分布式DNS数据库
- 定义两个自愈
#定义两个子域区域
shanghai.magedu.org. IN NS ns1.shanghai.magedu.org.
shanghai.magedu.org. IN NS ns2.shanghai.magedu.org.
shenzhen.magedu.org. IN NS ns1.shenzhen.magedu.org.
shenzhen.magedu.org. IN NS ns2.shenzhen.magedu.org.
ns1.shanghai.magedu.org. IN A 1.1.1.1
ns2.shanghai.magedu.org. IN A 1.1.1.2
ns1.shenzhen.magedu.org. IN A 1.1.1.3
ns2.shenzhen.magedu.org. IN A 1.1.1.4
#上面内容 可简写成:
shanghai IN NS ns1.shanghai
shanghai IN NS ns2.shanghai
shenzhen IN NS ns1.shenzhen
shenzhen IN NS ns2.shenzhen
ns1.shanghai IN A 1.1.1.1
ns2.shanghai IN A 1.1.1.2
ns1.shenzhen IN A 1.1.1.3
ns2.shenzhen IN A 1.1.1.4环境说明
| hostname | IP | service | role | OS |
|---|---|---|---|---|
| dns-master | 10.0.0.103 | bind9 | 主DNS | Ubuntu |
| dns-slave | 10.0.0.104 | bind9 | 从DNS | Ubuntu |
| sh-dns-master | 10.0.0.18 | bind9 | 上海子域 主DNS | CentOS |
| sh-dns-slave | 10.0.0.28 | bind9 | 上海子域 从DNS | CentOS |
主DNS配置
- 主DNS上配置子域(也就是在父域上设置)
# 区域数据库文件
# /var/cache/bind/xiangzheng.com.zone.bind
$TTL 600
@ IN SOA ns1 mail.xiangzheng.com. (
2015042202 ;
1H ;
10M ;
1D ;
12H ;
)
@ NS ns1
@ NS ns2
sh NS ns3 #加一条NS记录设置sh为新的子域,并命名为ns3(sh的完整写法其实是 sh.xiangzheng.com.)
ns1 A 10.0.0.103
ns2 A 10.0.0.104
ns3 A 10.0.0.18 #将ns3设为A记录,并指向IP地址,即sh子域的IP地址
@ A 10.0.0.77
# named 配置文件
# /etc/bind/named.conf
include "/etc/bind/named.conf.options";
include "/etc/bind/named.conf.local";
include "/etc/bind/named.conf.default-zones";
# named 子配置文件
# /etc/bind/named.conf.options
options {
directory "/var/cache/bind";
...
dnssec-validation auto;
listen-on-v6 { any; };
listen-on port 53 { localhost; };
allow-query { any; };
allow-transfer { 10.0.0.104; }; #设置只允许从服务器进行区域传输
};
# 区域数据库配置文件
# /etc/bind/named.conf.default-zones
// prime the server with knowledge of the root servers
zone "." {
type hint;
file "/usr/share/dns/root.hints";
};
zone "xiangzheng.com" {
type master; #类型为master
file "/var/cache/bind/xiangzheng.com.zone.bind";
};
...从DNS配置
# named 配置文件
# /etc/bind/named.conf
include "/etc/bind/named.conf.options";
include "/etc/bind/named.conf.local";
include "/etc/bind/named.conf.default-zones";
# named 子配置文件
# /etc/bind/named.conf.options
options {
directory "/var/cache/bind";
...
dnssec-validation auto;
listen-on-v6 { any; };
listen-on port 53 { localhost; };
allow-query { any; };
allow-transfer { none; }; #设置不允许其它主机进行区域传输
};
# 区域数据库配置文件
# /etc/bind/named.conf.default-zones
// prime the server with knowledge of the root servers
zone "." {
type hint;
file "/usr/share/dns/root.hints";
};
zone "xiangzheng.com" {
type slave; #类型为slave
masters {10.0.0.103;}; #指向主DNS的IP
file "/var/cache/bind/xiangzheng.com.zone.bind.slave"; #此文件无需创建,会自动生成,为加以区分最后可以加个.slave,
};
...上海子域 主DNS配置
# named 子配置文件
# /etc/named.conf
...
options {
listen-on port 53 { localhost; };
...
allow-query { any; };
allow-transfer { 10.0.0.28; }; #设置只允许从服务器进行区域传输
...
# 区域数据库文件
# /var/named/sh.xiangzheng.com.zone
$TTL 600
@ IN SOA ns1 mail.xiangzheng.com. (
2015042202 ;
1H ;
10M ;
1D ;
12H ;
)
@ NS ns1
@ NS ns2
ns1 A 10.0.0.18
ns2 A 10.0.0.28
@ A 10.0.0.123
# 区域数据库配置文件
# /etc/named.rfc1912.zones
...
zone "sh.xiangzheng.com" { #定义为sh子域
type master; #类型为master
file "sh.xiangzheng.com.zone";
};
...
# 修改区域数据库权限
chgrp named /var/named/sh.xiangzheng.com.zone
#检查区域数据库的语法,第一个xiangzheng.org表示在配置文件中定义的名称
named-checkzone sh.xiangzheng.com /var/named/sh.xiangzheng.com.zone
#检查配置文件语法
named-checkconf
#语法无误后 启动name服务
systemctl enable --now named
#或
#语法无误后 重启name服务
rndc reload上海子域 从DNS配置
# named 子配置文件
# /etc/named.conf
...
options {
listen-on port 53 { localhost; };
...
allow-query { any; };
allow-transfer { none; }; #设置不允许其它主机进行区域传输
...
# 区域数据库文件
不用配置,因为会自动向主DNS拉取
# 区域数据库配置文件
# /etc/named.rfc1912.zones
...
zone "sh.xiangzheng.com" {
type slave; #类型为slave
masters {10.0.0.18;}; #指向主DNS的IP
file "slaves/sh.xiangzheng.com.zone.slave";
};
...
#检查配置文件语法
named-checkconf
#语法无误后 启动name服务
systemctl enable --now named
#或
#语法无误后 重启name服务
rndc reload客户端测试
[root@client ~]# dig sh.xiangzheng.com @10.0.0.18
; <<>> DiG 9.11.36-RedHat-9.11.36-3.el8 <<>> sh.xiangzheng.com @10.0.0.18
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38203
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: bbdca7534c3c6d51455adee362c46dc32bea62fa4ede9f07 (good)
;; QUESTION SECTION:
;sh.xiangzheng.com. IN A
;; ANSWER SECTION:
sh.xiangzheng.com. 600 IN A 10.0.0.123
;; AUTHORITY SECTION:
sh.xiangzheng.com. 600 IN NS ns1.sh.xiangzheng.com.
sh.xiangzheng.com. 600 IN NS ns2.sh.xiangzheng.com.
;; ADDITIONAL SECTION:
ns1.sh.xiangzheng.com. 600 IN A 10.0.0.18
ns2.sh.xiangzheng.com. 600 IN A 10.0.0.28
;; Query time: 0 msec
;; SERVER: 10.0.0.18#53(10.0.0.18)
;; WHEN: Wed Jul 06 00:58:43 CST 2022
;; MSG SIZE rcvd: 158
[root@client ~]# host sh.xiangzheng.com 10.0.0.18
Using domain server:
Name: 10.0.0.18
Address: 10.0.0.18#53
Aliases:
sh.xiangzheng.com has address 10.0.0.123
[root@client ~]# host sh.xiangzheng.com 10.0.0.28
Using domain server:
Name: 10.0.0.28
Address: 10.0.0.28#53
Aliases:
sh.xiangzheng.com has address 10.0.0.123子域授权
每个域的名称服务器,都是通过其上级名称服务器在解析库进行授权,类似根域授权tld glue record:粘合记录,父域授权子域的记录
范例:
.com. IN NS ns1.com.
.com. IN NS ns2.com.
ns1.com. IN A 2.2.2.1
ns2.com. IN A 2.2.2.2
#magedu.org. 在.com的名称服务器上,解析库中添加资源记录
magedu.org. IN NS ns1.magedu.org.
magedu.org. IN NS ns2.magedu.org.
magedu.org. IN NS ns3.magedu.org.
ns1.magedu.org. IN A 3.3.3.1
ns2.magedu.org. IN A 3.3.3.2
ns3.magedu.org. IN A 3.3.3.3