DNS主從伺服器配置實現

一步一個腳印發表於2021-01-02

主伺服器-centos7-IP:192.168.184.201

從伺服器-centos7-IP:192.168.184.202

客戶端-ubuntu1804-IP:192.168.184.150

①客戶端網路卡配置

 1 network:
 2   version: 2
 3   renderer: networkd
 4   ethernets:
 5     ens33:
 6       addresses:
 7       - 192.168.184.150/24
 8       gateway4: 192.168.184.2
 9       nameservers:
10         search: [ magedu.com,magedu.org ]
11         addresses: [ 192.168.184.201,192.168.184.202 ]

重啟網路卡

netplan apply

檢視DNS地址生效

cat /etc/resolv.conf
systemd-resolve --status

②主伺服器配置

先修改DNS主伺服器的主配置檔案/etc/named.conf,註釋掉以下兩行

vim /etc/named.conf
options {
// listen-on port 53 { localhost; }; // allow-query { any; };
需要域名解析就需要在主配置檔案上做zone(區域),一般是域解析在放在/etc/named.rfc1912.zones上面
vim /etc/named.rfc1912.zones
zone "magedu.org" IN {
        type master;
        file "magedu.org.zone";
};

需要建立/var/named/magedu.org.zone該區域檔案

vim /var/named/magedu.org.zone
$TTL 1D
@       IN SOA master admin.magedu.org. ( 20201231 1H 10M 3D 12H )
           NS  master
           NS  slave
master     A   192.168.184.201
slave      A   192.168.184.202
www        A   192.168.184.202
node1      A   192.168.184.10
db1        A   192.168.184.11

修改區域檔案的許可權和所屬組

[root@localhost named]# ll              
total 20
drwxrwx--- 2 named named   23 Jan  1 14:38 data
drwxrwx--- 2 named named   60 Jan  1 22:11 dynamic
-rw-r--r-- 1 root  root   153 Jan  1 22:38 magedu.org.zone
-rw-r----- 1 root  named 2253 Apr  5  2018 named.ca
-rw-r----- 1 root  named  152 Dec 15  2009 named.empty
-rw-r----- 1 root  named  152 Jun 21  2007 named.localhost
-rw-r----- 1 root  named  168 Dec 15  2009 named.loopback
drwxrwx--- 2 named named    6 Dec 16 00:32 slaves

[root@localhost named]# chmod 640 magedu.org.zone
[root@localhost named]# chown .named magedu.org.zone

 

重啟DNS主伺服器

systemctl restart named 或者 rndc reload

③從伺服器配置

先修改DNS從伺服器的主配置檔案/etc/named.conf,註釋掉以下兩行

vim /etc/named.conf
options { // listen-on port 53 { localhost; }; // allow-query { any; };
需要域名解析就需要在主配置檔案上做zone(區域),一般是域解析在放在/etc/named.rfc1912.zones上面
zone "magedu.org" IN {
        type slave;
        masters {192.168.184.201;};               
        file "slaves/magedu.org.zone.slave";      該檔案放在/var/named/下面,不需要去建立,同步過來自動生成
};

從伺服器開啟服務

systemctl start named

配置檔案已經同步過來

ll /var/named/slaves/
total 4
-rw-r--r-- 1 named named 405 Jan  1 23:22 magedu.org.zone.slave

④客戶端測試

dig www.magedu.org
www.magedu.org.        86400    IN    A    192.168.184.201                #我們可以發現從DNS主伺服器進行域名解析

把主伺服器關閉

systemctl stop named

客戶端進行快取清理,並再次進行域名解析

systemd-resolve --flush-caches
dig www.magedu.org
www.magedu.org.        86400    IN    A    192.168.184.202               #我們可以發現從DNS從伺服器進行域名解析

⑤主從伺服器同步

主伺服器的區域檔案進行修改

vim /var/named/magedu.org.zone
$TTL 1D
@       IN SOA master admin.magedu.org. ( 20201232 1H 10M 3D 12H )                   #此時需要在主伺服器上zone檔案中修改資料版本序列號,一般是遞增
           NS  master
           NS  slave
master     A   192.168.184.201
slave      A   192.168.184.202
www        A   192.168.184.202
node1      A   192.168.184.10
db1        A   192.168.184.11
db2        A   192.168.184.12
@          MX  10 mail              
mail       A   192.168.184.13

主伺服器再次重啟

systemctl start named

此時從伺服器同步成功,時間已經發生改變

ll /var/named/slaves/
total 4
-rw-r--r-- 1 named named 543 Jan  1 23:40 magedu.org.zone.slave

相關文章