在Linux上配置DNS服务

1、DNS服务简介

DNS 是计算机域名系统 (Domain Name System 或Domain Name Service) 的缩写,它是由解析器和域名服务器组成的。域名服务器是指保存有该网络中全部主机的域名和对应IP地址,并具备将域名转换为IP地址功能的服务器。linux

2、DNS安装配置

准备工做
1.配置DNS服务器所需的安装包数据库

DNS服务程序包:bind
DNS相关库:bind-libs
DNS客户端:bind-utils
限制DNS在一个目录中:bind-chroot
关闭防火墙:iptables -F
关闭selinux: setenforce 0
2.编辑配置文件vim

全局配置文件/etc/named.confcentos

options {
        listen-on port 53 { localhost; };      #括号内改成localhost是将本机ip监听在53端口上,也能够写上本机IP,注意最后的;号
        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是指容许任何人经过你的服务器来解析DNS,也能够指定IP。
logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};

zone "." IN {
        type hint;
        file "named.ca";
};
#上面两个就是DNS解析域名的模板,能够在下面接着写也能够写在下面的文件中/etc/named.rfc1912.zones
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

咱们在/etc/named.rfc1912.zones中写入DNS解析域缓存

zone "lpx123.com" IN {      #正向解析域名lpx123.com
        type master;      #主域名
        file "lpx123.com.zone";      #域名对应的文件
};

zone "252.18.172.in-addr.arpa" IN {     #反向解析域名
        type master;     
        file "172.18.252.zone";
};

区域数据库文件存放在/var/named/中,这个目录中也有模板文件,咱们能够拷贝模板文件进行修改。安全

[root@centos7 named]# cp -p named.localhost lpx123.com.zone
[root@centos7 named]# cp -p named.localhost 172.18.252.zone

注意拷贝的时候要加上-p由于这个文件所属组是named,不加-p所属组就会变成当前用户所属组,named就没法访问。
编辑正向区域数据库配置文件服务器

$TTL 1D
@       IN SOA   nsl.lpx123.com. root.lpx123.com. (
                                        20132702        ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        IN      NS      nsl.lpx123.com.
nsl     IN      A       172.18.252.36
mail    IN      A       2.2.2.2
TTL:生命期,是指这条DNS在客户端上的缓存时间。
1D:缓存时间为1天
@:引用当前域名
IN:index记录
SOA:主从认证、受权方面的记录
nsl.lpx123.com.:主域名
root.lpx123.com.:管理员邮箱
20132702        ; serial :这是一个序列号,主从之间更新的依据。
1D      ; refresh:更新时间,从服务器多久主动请求更新一次。1D表明一天
1H      ; retry:重试时间,当从服务器更新失败后,多久再更新。1H表明1小时
1W      ; expire:失效时间,当从服务器多长时间没有成功更新时,就再也不更新。1W表明1周
3H )    ; minimum:至关于TTL值。不写默认使用全局配置。
IN      NS      nsl.lpx123.com.:NS记录,后面跟域名服务器名称
nsl     IN      A       172.18.252.36:nslA记录对应的服务器地址

编辑反向区域数据库文件网络

[root@centos7 named]# vim 172.18.252.zone 
$TTL 1D
@       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地址,后面是主机名。ide

3、测试

重启服务
[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.

4、泛域名名字解析

当咱们须要批量添加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主机上,写入的不受影响。

5、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。

相关文章
相关标签/搜索