查询反向解析: dig -x server-ip
[iyunv@localhost ~]# dig -x 8.8.8.8 #反向解析google的域名服务
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.4 <<>> -x 8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12388
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 2#权威应答
;; QUESTION SECTION:
;8.8.8.8.in-addr.arpa. IN PTR
;; ANSWER SECTION:
8.8.8.8.in-addr.arpa. 62709 IN PTR google-public-dns-a.google.com.#google公共dns
;; AUTHORITY SECTION:
8.8.8.in-addr.arpa. 692 IN NS ns3.google.com. #google的反向解析
8.8.8.in-addr.arpa. 692 IN NS ns1.google.com.
8.8.8.in-addr.arpa. 692 IN NS ns4.google.com.
8.8.8.in-addr.arpa. 692 IN NS ns2.google.com.
;; ADDITIONAL SECTION:
ns1.google.com. 292900 IN A 216.239.32.10 #google的A记录
ns3.google.com. 263708 IN A 216.239.36.10
;; Query time: 19 msec
;; SERVER: 124.207.160.106#53(124.207.160.106)
;; WHEN: Wed Sep 16 23:30:39 2015
;; MSG SIZE rcvd: 186
追踪解析过程: dig -t 资源记录类型 名称 +trace
[iyunv@localhost ~]# dig -t A www.baidu.com +trace
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.4 <<>> -t A www.baidu.com +trace
;; global options: +cmd
. 247435 IN NS i.root-servers.net.
. 247435 IN NS m.root-servers.net.
. 247435 IN NS f.root-servers.net.
. 247435 IN NS k.root-servers.net.
. 247435 IN NS b.root-servers.net.
. 247435 IN NS c.root-servers.net.
. 247435 IN NS a.root-servers.net.
. 247435 IN NS e.root-servers.net.
. 247435 IN NS h.root-servers.net.
. 247435 IN NS j.root-servers.net.
. 247435 IN NS l.root-servers.net.
. 247435 IN NS g.root-servers.net.
. 247435 IN NS d.root-servers.net. #首先找13台根域
;; Received 496 bytes from 124.207.160.106#53(124.207.160.106) in 1193 ms
com. 172800 IN NS f.gtld-servers.net.
com. 172800 IN NS j.gtld-servers.net.
com. 172800 IN NS d.gtld-servers.net.
com. 172800 IN NS l.gtld-servers.net.
com. 172800 IN NS g.gtld-servers.net.
com. 172800 IN NS e.gtld-servers.net.
com. 172800 IN NS c.gtld-servers.net.
com. 172800 IN NS m.gtld-servers.net.
com. 172800 IN NS b.gtld-servers.net.
com. 172800 IN NS i.gtld-servers.net.
com. 172800 IN NS h.gtld-servers.net.
com. 172800 IN NS k.gtld-servers.net.
com. 172800 IN NS a.gtld-servers.net. #然后找.com.一级域
;; Received 503 bytes from 192.5.5.241#53(192.5.5.241) in 115 ms
baidu.com. 172800 IN NS dns.baidu.com.
baidu.com. 172800 IN NS ns2.baidu.com.
baidu.com. 172800 IN NS ns3.baidu.com.
baidu.com. 172800 IN NS ns4.baidu.com.
baidu.com. 172800 IN NS ns7.baidu.com. #再找baidu.com.二级域
;; Received 201 bytes from 192.42.93.30#53(192.42.93.30) in 252 ms www.baidu.com. 1200 IN CNAME www.a.shifen.com.
a.shifen.com. 1200 IN NS ns4.a.shifen.com.
a.shifen.com. 1200 IN NS ns3.a.shifen.com.
a.shifen.com. 1200 IN NS ns1.a.shifen.com.
a.shifen.com. 1200 IN NS ns2.a.shifen.com.
a.shifen.com. 1200 IN NS ns5.a.shifen.com. #接着找到www三级域及到达。www.baidu.com.
;; Received 228 bytes from 61.135.165.235#53(61.135.165.235) in 12 ms
[iyunv@localhost named]# named-checkconf
[iyunv@localhost named]# named-checkzone "0.168.192.zone" /var/named/0.168.192.zone
zone 0.168.192.zone/IN: 107.0.168.192.zone/NS 'ns.hong.com' (out of zone) has no addresses records (A or AAAA)
zone 0.168.192.zone/IN: loaded serial 0
OK
显示OK说明配置语法没有问题,这个OK很重要啊。。我的心都跟着跳啊。。。
接下来我们设置区域文件的权限:
文件属主: root
文件属组: named
权限为: 640
设置防火墙: /etc/sysconfig/iptables
添加三条记录把53和953端口放开
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-AINPUT -m state --state NEW -m tcp -p tcp --dport 53 -j ACCEPT #添加记录通过53和952端口
-AINPUT -m state --state NEW -m udp -p udp --dport 53 -j ACCEPT
-AINPUT -m state --state NEW -m tcp -p tcp --dport 953 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
重启防火墙: service iptables restart 即可
启动bind服务:
service named start
配置host 文件:
/etc/resolv.conf 添加服务器ip地址
# Generated by NetworkManager
domain DHCP
search DHCP HOST
nameserver 124.207.160.106
nameserver 219.239.26.42
nameserver 144.144.144.144
nameserver 192.168.0.107
至此搭建成功,测试一下
我们使用本地windows系统测试一下
C:\Users\f>nslookup
默认服务器: bjdns.drpeng.com.cn
Address: 124.207.160.106
> server 192.168.0.108
默认服务器: [192.168.0.108]
Address: 192.168.0.108
> set q=PTR
> 192.168.0.108
服务器: [192.168.0.108]
Address: 192.168.0.108
108.0.168.192.in-addr.arpa name = mail.hong.com #反向解析成功
108.0.168.192.in-addr.arpa name = ns.hong.com
108.0.168.192.in-addr.arpa name = www.hong.com
0.168.192.in-addr.arpa nameserver = ns.hong.com
ns.hong.com internet address = 192.168.0.108
>#解析成功
> server 192.168.0.108
默认服务器: [192.168.0.108]
Address: 192.168.0.108
> set q=A
> www.hong.com
服务器: [192.168.0.108]
Address: 192.168.0.108
> set q=NS #查询NS记录
> hong.com
服务器: [192.168.0.108]
Address: 192.168.0.108
hong.com nameserver = ns.hong.com
ns.hong.com internet address = 192.168.0.108
>
> set q=CNAME #别名记录
> ftp.hong.com
服务器: [192.168.0.108]
Address: 192.168.0.108
ftp.hong.com canonical name = mail.hong.com
hong.com nameserver = ns.hong.com
ns.hong.com internet address = 192.168.0.108
>
测试没有问题,即可正向解析又可以反向解析。。
rndc的使用:
rndc(Remote Name Domain Controller)远程名称域控制器
rndc 通过一个 TCP 连接与名字服务器通信,发送经过数字签名认证的命令。在当前版本的rndc 和 named 中,唯一
支持的认证算法是 HMAC-MD5,在连接的两端使用共享密钥。它为命令请求和名字服务器的响应提供 TSIG类型的认
证。所有经由通道发送的命令都必须被一个服务器所知道的 key_id 签名。
监听端口:tcp:953
语法:
rndc [-b address] [-c config] [-s server] [-p port][-k key-file ] [-y key] [-V] command
command is one of the following:
reload Reload configuration file and zones. #重新加载配置文件和区域文件
reconfig Reload configuration file and new zones only. #重新加载配置文件和新的区域文件
freeze Suspend updates to all dynamic zones.
stats Write server statistics to the statistics file. #将服务器统计信息写入统计文件中
stop Save pending updates to master files and stop the server.
halt Stop the server without saving pending updates.
flush Flushes all of the server's caches. #清空DNS缓存
status Display status of the server. #显示bind服务器的工作状态
[iyunv@localhost named]# rndc status
version: 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.4
CPUs found: 1
worker threads: 1
number of zones: 21
debug level: 0
xfers running: 0
xfers deferred: 0
soa queries in progress: 0
query logging is OFF
recursive clients: 0/0/1000
tcp clients: 0/100
server is up and running
zone "hong.com" IN {
type slave;
file "slaves/hong.com.zone";
masters{ 192.168.0.108; };
};
zone "0.168.192.in-addr.arpa" IN {
type slave;
file "slaves/0.168.192.zone";
masters{ 192.168.0.108; };
};
正向区域:
$TTL 86400
@ IN SOA ns1.hong.com. admin.hong.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
@ IN NS ns1
@ IN NS ns2 #添加NS记录
@ IN MX 10 mail
ns1 IN A 192.168.0.108
ns2 IN A 192.168.0.106 #添加ns2的A记录
mail IN A 192.168.0.108
www IN A 192.168.0.108
www IN A 192.168.0.106
ftp IN CNAME www
反向区域:
$TTL 86400
@ IN SOA ns1.hong.com. admin.hong.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
@ IN NS ns1.hong.com.
@ IN NS ns2.hong.com.#添加NS记录
108 IN PTR ns1.hong.com.
106 IN PTR ns2.hong.com.#添加ns2PTR记录
108 IN PTR www.hong.com.
106 IN PTR www.hong.com.#www记录
108 IN PTR mail.hong.com.
添加完记录同样检测一下语法:
1
2
3
4
5
6
7
8
[iyunv@localhost named]# named-checkconf
[iyunv@localhost named]# named-checkzone "hong.com.zone" /var/named/hong.com.zone
zone hong.com.zone/IN: loaded serial 0
OK
[iyunv@localhost named]# named-checkzone "0.168.192.zone" /var/named/0.168.192.zone
zone 0.168.192.zone/IN: loaded serial 0
OK
[iyunv@localhost named]#
没有问题,在主DNS上测试一下是否有记录
[iyunv@localhost named]# dig -t NS hong.com @192.168.0.108
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.4 <<>> -t NS hong.com @192.168.0.108
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24224
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 2
;; QUESTION SECTION:
;hong.com. IN NS
;; ANSWER SECTION:
hong.com. 86400 IN NS ns1.hong.com.
hong.com. 86400 IN NS ns2.hong.com. #已经有ns2记录
;; ADDITIONAL SECTION:
ns1.hong.com. 86400 IN A 192.168.0.108 #A记录也已经存在
ns2.hong.com. 86400 IN A 192.168.0.106
;; Query time: 1 msec
;; SERVER: 192.168.0.108#53(192.168.0.108)
;; WHEN: Thu Sep 17 06:34:08 2015
;; MSG SIZE rcvd: 94
反向解析:
[iyunv@localhost named]# dig -x 192.168.0.108 @192.168.0.108
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.4 <<>> -x 192.168.0.108 @192.168.0.108
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15453
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 2, ADDITIONAL: 2
;; QUESTION SECTION:
;108.0.168.192.in-addr.arpa. IN PTR
;; ANSWER SECTION:
108.0.168.192.in-addr.arpa. 86400 IN PTR ns1.hong.com.
108.0.168.192.in-addr.arpa. 86400 IN PTR www.hong.com.
108.0.168.192.in-addr.arpa. 86400 IN PTR mail.hong.com.
;; AUTHORITY SECTION:
0.168.192.in-addr.arpa. 86400 IN NS ns2.hong.com.#辅助ns2已经存在
0.168.192.in-addr.arpa. 86400 IN NS ns1.hong.com.
;; ADDITIONAL SECTION:
ns1.hong.com. 86400 IN A 192.168.0.108
ns2.hong.com. 86400 IN A 192.168.0.106 #对应的A记录
;; Query time: 1 msec
;; SERVER: 192.168.0.108#53(192.168.0.108)
;; WHEN: Thu Sep 17 06:36:23 2015
;; MSG SIZE rcvd: 171
辅助DNS上查看一下是否同步
[iyunv@localhost slaves]# dig -t axfr hong.com @192.168.0.108
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> -t axfr hong.com @192.168.0.108
;; global options: +cmd
hong.com. 86400 IN SOA ns1.hong.com. admin.hong.com. 0 86400 3600 604800 10800
hong.com. 86400 IN NS ns1.hong.com.
hong.com. 86400 IN NS ns2.hong.com.
hong.com. 86400 IN MX 10 mail.hong.com.
ftp.hong.com. 86400 IN CNAME www.hong.com.
mail.hong.com. 86400 IN A 192.168.0.108
ns1.hong.com. 86400 IN A 192.168.0.108
ns2.hong.com. 86400 IN A 192.168.0.106 #可以看到都已经同步过来 www.hong.com. 86400 IN A 192.168.0.108 www.hong.com. 86400 IN A 192.168.0.106
hong.com. 86400 IN SOA ns1.hong.com. admin.hong.com. 0 86400 3600 604800 10800
;; Query time: 1 msec
;; SERVER: 192.168.0.108#53(192.168.0.108)
;; WHEN: Wed Sep 16 18:42:44 2015
;; XFR size: 11 records (messages 1, bytes 263)
我们可以查看到辅助DNS服务器上是否有区域文件:
1
2
[iyunv@localhost slaves]# ls
0.168.192.zone hong.com.zone #正向反向区域文件已经存在
进入区域文件:
$ORIGIN .
$TTL 86400 ; 1 day
hong.com IN SOA ns1.hong.com. admin.hong.com. (
0 ; serial
86400 ; refresh (1 day)
3600 ; retry (1 hour)
604800 ; expire (1 week)
10800 ; minimum (3 hours)
)
NS ns1.hong.com.
NS ns2.hong.com.
MX 10 mail.hong.com.
$ORIGIN hong.com. #ORIGIN意思为每个资源记录末尾自动附加所定义的内容
ftp CNAME www
mail A 192.168.0.108
ns1 A 192.168.0.108
ns2 A 192.168.0.106
www A 192.168.0.108
A 192.168.0.106
#辅助DNS里的区域文件是不能手动修改的。。由主DNS服务器同步而来。。