BIND 主从配置

BIND 主从配置

环境:
master:172.31.182.144
slave:172.31.182.147

一、安装
yum install bind bind-chroot  -y

(源码包:https://downloads.isc.org/isc/bind9/9.14.8/bind-9.14.8.tar.gz)

二、master配置

[root@master named]# cat /etc/named.conf |grep -Ev "//|^$"
options {
    listen-on port 53 { 172.31.182.144; };
    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";
    recursing-file  "/var/named/data/named.recursing";
    secroots-file   "/var/named/data/named.secroots";
    /*
     - If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
     - If you are building a RECURSIVE (caching) DNS server, you need to enable
       recursion.
     - If your recursive DNS server has a public IP address, you MUST enable access
       control to limit queries to your legitimate users. Failing to do so will
       cause your server to become part of large scale DNS amplification
       attacks. Implementing BCP38 within your network would greatly
       reduce such attack surface
    */
    recursion yes;
    dnssec-enable yes;
    dnssec-validation yes;
    /* Path to ISC DLV key */
    bindkeys-file "/etc/named.iscdlv.key";
    managed-keys-directory "/var/named/dynamic";
    pid-file "/run/named/named.pid";
    session-keyfile "/run/named/session.key";
};
logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};
zone "adfile.wifi8.com" {
        type master;
        file "adfile.wifi8.com.hosts";
        allow-transfer {172.31.182.147;};
	notify yes;
	also-notify { 172.31.182.147; }; //指定slave server的IP位址
        };
zone "." IN {
    type hint;
    file "named.ca";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

zone文件:

[root@master named]# cat /var/named/adfile.wifi8.com.hosts
$TTL 180
@       IN SOA  ns1.test.com. root.adfile.wifi8.com. ( ;
                                        22190928       ; serial
                                        10S      ; refresh
                                        1H      ; retry
                                        1M      ; expire
                                        44H )    ; minimum
                IN      NS      ns1.test.com.
                IN      NS      ns2.test.com.
ns1             IN      A       172.31.182.144
ns2             IN      A       172.31.182.147
adfile.wifi8.com. IN A 10.254.33.32
adfile.wifi8.com. IN A 10.254.33.34

各参数解析:http://dns-learning.twnic.net.tw/bind/intro6.html

启动:
systemctl  restart  named.service

三、slave配置

[root@node02 named]# cat /etc/named.conf |grep -Ev "//|^$"
options {
    listen-on port 53 { 172.31.182.147; };
    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";
    recursing-file  "/var/named/data/named.recursing";
    secroots-file   "/var/named/data/named.secroots";
    /*
     - If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
     - If you are building a RECURSIVE (caching) DNS server, you need to enable
       recursion.
     - If your recursive DNS server has a public IP address, you MUST enable access
       control to limit queries to your legitimate users. Failing to do so will
       cause your server to become part of large scale DNS amplification
       attacks. Implementing BCP38 within your network would greatly
       reduce such attack surface
    */
    recursion yes;
    dnssec-enable yes;
    dnssec-validation yes;
    /* Path to ISC DLV key */
    bindkeys-file "/etc/named.iscdlv.key";
    managed-keys-directory "/var/named/dynamic";
    pid-file "/run/named/named.pid";
    session-keyfile "/run/named/session.key";
};
logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};
zone "adfile.wifi8.com" {
        type slave;
        file "adfile.wifi8.com.hosts";
        masters { 172.31.182.144; };
        };
zone "." IN {
    type hint;
    file "named.ca";
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

启动后自动同步master解析配置:
systemctl  restart  named.service

添加域名脚本:

#################master:#################
#!/bin/bash
read -p "Please enter the domain name you need to add:" DOMAIN
read -p "Please enter the domain name corresponding to the IP record:" IP
HOSTS_DIR=/mnt/sscp/data/named/hosts
NAMED_CONFIG_DIR=/mnt/sscp/data/named/conf/named.conf
#Create domain record file
cat >${HOSTS_DIR}/${DOMAIN}.hosts<<EOF
\$TTL 180
@       IN SOA  ns1.sscp.mtr.com. root.${DOMAIN}. ( ;
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        44H )    ; minimum
                IN      NS      ns1.sscp.mtr.com.
                IN      NS      ns2.sscp.mtr.com.
ns1             IN      A       128.164.250.26
ns2             IN      A       128.164.250.27
${DOMAIN}. IN A ${IP}
EOF
#Add named config
cat >>${NAMED_CONFIG_DIR}<<EOF
zone "${DOMAIN}" IN{
        type master;
        file "${DOMAIN}.hosts";
        allow-transfer {128.164.250.27;};
};
EOF
# Checkconf named config
/mnt/sscp/app/named/sbin/named-checkconf
#Restart named server
/mnt/sscp/app/named/sbin/rndc -s 127.0.0.1 reload
if [ $? = 0 ];then
  echo "Added successfully!!"
else
  echo "Add failed!! Please check"
fi


#################slave:#################
#!/bin/bash
read -p "Please enter the domain name you need to add:" DOMAIN
HOSTS_DIR=/mnt/sscp/data/named/hosts
NAMED_CONFIG_DIR=/mnt/sscp/data/named/conf/named.conf
#Add named config
cat >>${NAMED_CONFIG_DIR}<<EOF
zone "${DOMAIN}" IN{
        type slave;
        file "${DOMAIN}.hosts";
        masters { 128.164.250.26; };
};
EOF
# Checkconf named config
/mnt/sscp/app/named/sbin/named-checkconf
#Restart named server
/mnt/sscp/app/named/sbin/rndc -s 127.0.0.1 reload
if [ $? = 0 ];then
  echo "Added successfully!!"
else
  echo "Add failed!! Please check"
fi


踩坑:
1、最后需要在主DNS服务器上的/var/named/ZONE_NAME.zone 文件里添加将该从服务的NS记录;
2、同时若想要实现主从服务器的数据同步,在修改好主服务器的/var/named/ZONE_NAME.zone 文件时,必须将该文件里的 序列号 增大才行,增大并保存退出后,主服务器会自动向从服务器推送(push)修改后的文件内容;

 

 

posted @ 2019-08-06 17:24  西门运维  阅读(1563)  评论(0编辑  收藏  举报