@ IN SOA nsl.lpx123.com. root.lpx123.com. (
20132703 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
@ NS nsl.lpx123.com.
36 PTR nsl.lpx123.com.
100 PTR www.lpx123.com.
~
反向区域数据库文件和正向区域数据库文件差不多, 区别就在多了一个 RPT 记录
PTR 的格式: 前面是对应的 IP 地址, 后面是主机名.
三, 测试
重启服务
[root@centos7 named]# systemctl restart named
我们在另一台机器上进行测试
首先我们要设置下 DNS 服务器地址:
- [root@localhost ~]# vim /etc/resolv.conf
- nameserver 172.18.252.36 #把里面的内容都注释掉添加一个 DNS 服务器地址
- [root@localhost ~]# dig nsl.lpx123.com @172.18.252.36
- ; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> nsl.lpx123.com @172.18.252.36
- ;; global options: +cmd
- ;; Got answer:
- ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 821
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1 #aa 代表的是权威, 是指这个解析是通过这个域名的本身服务器解析出来的而不是通过转发解析出来的.
- ;; OPT PSEUDOSECTION:
- ; EDNS: version: 0, flags:; udp: 4096
- ;; QUESTION SECTION:
;nsl.lpx123.com. IN A
- ;; ANSWER SECTION:
- nsl.lpx123.com. 86400 IN A 172.18.252.36 #解析出来的 A 记录对应的地址
- ;; AUTHORITY SECTION:
lpx123.com. 86400 IN NS nsl.lpx123.com.
;; Query time: 1 msec
;; SERVER: 172.18.252.36#53(172.18.252.36)
;; WHEN: Wed May 23 01:50:46 CST 2018
- ;; MSG SIZE rcvd: 73
- [root@localhost ~]# nslookup 172.18.252.36 #反向解析
- Server: 172.18.252.36
- Address: 172.18.252.36#53
36.252.18.172.in-addr.arpa name = nsl.lpx123.com.
四, 泛域名名字解析
当我们需要批量添加 DNS 解析时就可以用通配符来写如下
- [root@centos7 named]# vim lpx123.com.zone
- $GENERATE 1-100 server$ A 3.3.3.$ #在正向域名解析中添加一条这样的记录
这样就添加了从 server1.lpx123.com 到 server100.lpx.com 的记录, 与之对应的 ip 分别也是从 3.3.3.1 到 3.3.3.100
- [root@centos7 named]# nslookup server1.lpx123.com
- Server: 172.18.252.36
- Address: 172.18.252.36#53
- Name: server1.lpx123.com
- Address: 3.3.3.1
- [root@centos7 named]# nslookup server2.lpx123.com
- Server: 172.18.252.36
- Address: 172.18.252.36#53
- Name: server2.lpx123.com
- Address: 3.3.3.2
- [root@centos7 named]# nslookup server100.lpx123.com
- Server: 172.18.252.36
- Address: 172.18.252.36#53
- Name: server100.lpx123.com
- Address: 3.3.3.100
还有就是我们有的时候多输入了一个 w 也能访问到我们要访问的网站, 或者输错了也能访问到, 这就是用到了泛域名解析
写法如下
- *.lpx123.com. A 4.4.4.4
- [root@centos7 named]# nslookup www.lpx123.com
- Server: 172.18.252.36
- Address: 172.18.252.36#53
Name: www.lpx123.com
- Address: 4.4.4.4
- [root@centos7 named]# nslookup dns.lpx123.com
- Server: 172.18.252.36
- Address: 172.18.252.36#53
Name: dns.lpx123.com
- Address: 4.4.4.4
- [root@centos7 named]# nslookup nsl.lpx123.com
- Server: 172.18.252.36
- Address: 172.18.252.36#53
Name: nsl.lpx123.com
Address: 172.18.252.36
只要是我们没写的 lpx123.com 的域名全部都解析到 4.4.4.4 主机上, 写入的不受影响.
五, DNS 主从
我们已经搭建好了主 DNS 服务器, 所以我们现在只需要再搭建一个从 DNS 服务器就可以了
1. 我们首先准备好搭建环境, 安装好软件包 (同上)
2. 编辑全局配置文件 (同主配置文件)
3. 写区域数据库文件 / etc/named.conf
- options {
- listen-on port 53 { localhost; }; #这里还是改为 localhost
- listen-on-v6 port 53 { ::1; };
- directory "/var/named";
- dump-file "/var/named/data/cache_dump.db";
- statistics-file "/var/named/data/named_stats.txt";
- memstatistics-file "/var/named/data/named_mem_stats.txt";
- allow-query { any; }; #这里改为 any
我们在模板后面添加
- zone "lpx123.com" IN {
- type slave; #代表为从域名
- file "slaves/lpx123.com.zone"; #复制主域名库文件后的存放位置
- masters { 172.18.252.36; }; #主域名的 IP 地址
- };
启动服务, 我们会看到在 / var/named/slaves 目录下有一个文件, 这就是从域名库文件, 我们用另一台机器看能否解析
- [root@centos6 ~]# dig nsl.lpx123.com @172.18.250.216
- ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6 <<>> nsl.lpx123.com @172.18.250.216
- ;; global options: +cmd
- ;; Got answer:
- ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32433
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;nsl.lpx123.com. IN A
;; ANSWER SECTION:
nsl.lpx123.com. 86400 IN A 172.18.252.36
;; AUTHORITY SECTION:
lpx123.com. 86400 IN NS nsl.lpx123.com.
;; Query time: 4 msec
;; SERVER: 172.18.250.216#53(172.18.250.216)
;; WHEN: Thu May 24 21:03:47 2018
;; MSG SIZE rcvd: 62
解析成功
指定传输机器
我们发现搭建从服务器时主服务器并没有同意我们就当了从服务器并获取到了区域库文件, 这对主 DNS 来说是不安全的, 所以我们加个指定传输机器
在 / etc/named.conf 中添加一条
allow-transfer { 172.18.250.216;}; #括号内填写从服务器 ip 地址
测试
我们用从服务器能获取数据
- [root@localhost slaves]# dig -t axfr lpx123.com @172.18.252.36
- ; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7 <<>> -t axfr lpx123.com @172.18.252.36
- ;; global options: +cmd
lpx123.com. 86400 IN SOA nsl.lpx123.com. root.lpx123.com. 20132702 86400 3600 604800 10800
lpx123.com. 86400 IN NS nsl.lpx123.com.
- *.lpx123.com. 86400 IN A 4.4.4.4
- mail.lpx123.com. 86400 IN A 2.2.2.2
用别的机器就不能抓取数据, 但对与通过域名正常获取没影响
- [root@centos6 ~]# dig -axfr l.lpx123.com @172.18.252.36
- ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6 <<>> -t axfr lpx123.com @172.18.252.36
- ;; global options: +cmd
; Transfer failed.
- [root@centos6 ~]# dig nsl.lpx123.com @172.18.252.36
- ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6 <<>> nsl.lpx123.com @172.18.252.36
- ;; global options: +cmd
- ;; Got answer:
- ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12384
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;nsl.lpx123.com. IN A
;; ANSWER SECTION:
nsl.lpx123.com. 86400 IN A 172.18.252.36
;; AUTHORITY SECTION:
lpx123.com. 86400 IN NS nsl.lpx123.com.
;; Query time: 1 msec
;; SERVER: 172.18.252.36#53(172.18.252.36)
;; WHEN: Thu May 24 21:23:47 2018
;; MSG SIZE rcvd: 62
PS: 我们对主 DNS 做了设置, 从 DNS 也要做设置不然别人也可以从你的从 DNS 抓取数据, 只不过从 DNS 要是没有从从 DNS 那括号里就可以改为 none.
来源: http://www.bubuko.com/infodetail-2614596.html