在Linux上配置DNS服務

一、DNS服務簡介

DNS 是計算機域名系統 (Domain Name System 或Domain Name Service) 的縮寫,它是由解析器和域名服務器組成的。域名服務器是指保存有該網絡中所有主機的域名和對應IP地址,並具有將域名轉換爲IP地址功能的服務器。

二、DNS安裝配置

準備工作
1.配置DNS服務器所需的安裝包

DNS服務程序包:bind
DNS相關庫:bind-libs
DNS客戶端:bind-utils
限制DNS在一個目錄中:bind-chroot
關閉防火牆:iptables -F
關閉selinux: setenforce 0
2.編輯配置文件

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

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地址,後面是主機名。

三、測試

重啓服務
[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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章