脚本:在CentOS上使用BIND实现DNS双机系统(1主1从)
棒棒堂智能运维
2020年06月09日 14:49
收录于文集
共22篇

DNS-Host-1:

#!/usr/bin/bash ######################################## File Description #​####################################### # Creation time:2020-05-31 # Project:08 # Task: 05 # Execute example:bash reader-shell-x-x.sh # Detailed description: # About:http://linux.book.51xueweb.cn ################################################################################################## # reback start cd /etc/yum.repos.d cp CentOS-Base.repo CentOS-Base.repo.bak cp CentOS-AppStream.repo CentOS-AppStream.repo.bak cp CentOS-Extras.repo CentOS-Extras.repo.bak sed -i 's|mirrorlist=|#​mirrorlist=|g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo sed -i 's|#​baseurl=|baseurl=|g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo sed -i 's|http://mirror.centos.org|https://mirrors.aliyun.com|g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo cp -f /etc/named.conf.bak1 /etc/named.conf yum remove -y bind-utils rm -f /var/named/com-domain-area rm -f /var/named/10.10.3.area rm -f /var/named/com-domain-common rm -f /var/named/10.10.4.common yum remove -y bind yum clean all echo "[root@Project-08-Task-01 ~]# clear&#​34; sleep 3s clear # reback end #***************record shell start*************** echo -e "---------------Implementing DNS-Master---------------\n" yum install -y bind clear systemctl start named systemctl status named | head -10 systemctl enable named.service systemctl list-unit-files | grep named.service systemctl reload named systemctl stop firewalld setenforce 0 echo -e "\n&#​34; read -n1 -p "---------------Please execute Script on Server Slave---------------" echo -e "\n&#​34; echo -e "---------------Configure DNS master as the primary domain name resolution service---------------\n" #generate TSIG key on DNS master cp -f /etc/named.conf /etc/named.conf.bak1 yum install -y bind-utils tsig-keygen -a hmac-md5 area-key tsig-keygen -a hmac-md5 common-key echo -e "\n&#​34; read -p "---------------Please record the two generated keys in time---------------" echo -e "\n&#​34; #configure primary and secondary synchronization and view on DNS master sed -i '/zone &#​34;." IN {/,+3d&#​39; /etc/named.conf sed -i '/named.rfc1912.zones/d&#​39; /etc/named.conf sed -i 's/127.0.0.1/10.10.2.120/g&#​39; /etc/named.conf sed -i 's/localhost/any/g&#​39; /etc/named.conf sed -i "/allow-query/a allow-transfer {10.10.2.122;};\nalso-notify {10.10.2.122;};\nnotify yes;\nmasterfile-format text;" /etc/named.conf cat >> /etc/named.conf <<EOF key "area-key&#​34; {         algorithm hmac-md5;         secret "areaSecret&#​34;; }; key "common-key&#​34; {         algorithm hmac-md5;         secret "commonSecret&#​34;; }; EOF read -p "Please enter the secret value in the area-key: " areaSecret sed -i 's#​areaSecret#&#​39;'&#​39;$areaSecret'&#​39;'#​g' /etc/named.conf read -p "Please enter the secret value in the common-key: " commonSecret sed -i 's#​commonSecret#&#​39;'&#​39;$commonSecret'&#​39;'#​g' /etc/named.conf cat >> /etc/named.conf <<EOF view "area&#​34; {     match-clients{key area-key; 10.10.2.0/26;};     server 10.10.2.122 { keys area-key; };     zone ".&#​34; IN {         type hint;         file "named.ca&#​34;;     };     zone "domain.com&#​34; IN {         type master;         file "com-domain-area&#​34;;         allow-update {none;};     };     zone "3.10.10.in-addr.arpa&#​34; IN {         type master;         file "10.10.3.area&#​34;;     }; }; view "common&#​34; {     match-clients { key common-key; any;};     server 10.10.2.122 { keys common-key; };     zone ".&#​34; IN {         type hint;         file "named.ca&#​34;;     };     zone "domain.com&#​34; IN {         type master;         file "com-domain-common&#​34;;         allow-update {none;};     };     zone "4.10.10.in-addr.arpa&#​34; IN {         type master;         file "10.10.4.common&#​34;;     }; }; EOF #configure domain name records for specific zones on DNS master cat > /var/named/com-domain-area <<EOF \$TTL 1D @ IN SOA ns.domain.com. root.domain.com. (     1 ; serial     1D ; refresh     1H ; retry     1W ; expire 3H ) ; minimum @ IN NS ns.domain.com. @ IN NS ns1.domain.com. ns IN A 10.10.2.120 ns1 IN A 10.10.2.122 www IN A 10.10.3.200 ftp IN A 10.10.3.200 EOF cat > /var/named/10.10.3.area <<EOF \$TTL 1D @ IN SOA ns.domain.com. root.domain.com. (     1 ; serial     1D ; refresh     1H ; retry     1W ; expire 3H ) ; minimum @ IN NS ns.domain.com. @ IN NS ns1.domain.com. 120 IN PTR ns.domain.com. 122 IN PTR ns1.domain.com. 200 IN PTR www.domain.com. 200 IN PTR ftp.domain.com. EOF #configure the universal zone domain name record on DNS master cat > /var/named/com-domain-common <<EOF \$TTL 1D @ IN SOA ns.domain.com. root.domain.com. (     1 ; serial     1D ; refresh     1H ; retry     1W ; expire 3H ) ; minimum @ IN NS ns.domain.com. @ IN NS ns1.domain.com. ns IN A 10.10.2.120 ns1 IN A 10.10.2.122 www IN A 10.10.4.200 ftp IN A 10.10.4.200 EOF cat > /var/named/10.10.4.common <<EOF \$TTL 1D @ IN SOA ns.domain.com. root.domain.com. (     1 ; serial     1D ; refresh     1H ; retry     1W ; expire 3H ) ; minimum @ IN NS ns.domain.com. @ IN NS ns1.domain.com. 120 IN PTR ns.domain.com. 122 IN PTR ns1.domain.com. 200 IN PTR www.domain.com. 200 IN PTR ftp.domain.com. EOF #verify and reload the bind profile on DNS master named-checkconf /etc/named.conf named-checkzone domain.com /var/named/com-domain-area named-checkzone 3.10.10.in-addr.arpa /var/named/10.10.3.area named-checkzone domain.com /var/named/com-domain-common named-checkzone 4.10.10.in-addr.arpa /var/named/10.10.4.common systemctl reload named echo -e "\n&#​34; read -n1 -p "---------------Please execute Script on Server Slave---------------" echo -e "\n&#​34; echo -e "---------------Testing domain name resolution service on DNS master---------------\n" dig www.domain.com @10.10.2.120 dig www.domain.com @10.10.2.122 echo -e "\n&#​34; read -n1 -p "---------------Please execute Script on Server Slave---------------" #***************record shell end*****************

DNS-Host-2:

#!/usr/bin/bash ######################################## File Description #​####################################### # Creation time:2020-05-31 # Project:08 # Task: 05 # Execute example:bash reader-shell-x-x.sh # Detailed description: # About:http://linux.book.51xueweb.cn ################################################################################################## #reback start cd /etc/yum.repos.d cp CentOS-Base.repo CentOS-Base.repo.bak cp CentOS-AppStream.repo CentOS-AppStream.repo.bak cp CentOS-Extras.repo CentOS-Extras.repo.bak sed -i 's|mirrorlist=|#​mirrorlist=|g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo sed -i 's|#​baseurl=|baseurl=|g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo sed -i 's|http://mirror.centos.org|https://mirrors.aliyun.com|g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo yum remove -y bind-utils cp -f /etc/named.conf.bak1 /etc/named.conf yum remove -y bind-utils rm -f /var/named/com-domain-area rm -f /var/named/10.10.3.area rm -f /var/named/com-domain-common rm -f /var/named/10.10.4.common yum remove -y bind yum clean all echo "[root@Project-08-Task-05 ~]# clear&#​34; sleep 3s clear #reback end #***************reader shell start*************** echo -e "---------------Implementing DNS-Slave---------------\n" yum install -y bind #view information systemctl start named systemctl status named | head -10 #set the boot systemctl enable named.service #view the status of the run systemctl list-unit-files | grep named.service #reload named systemctl reload named #stop firewalld systemctl stop firewalld setenforce 0 echo -e "\n&#​34; read -n1 -p "---------------Please execute Script on Server Master---------------" echo -e "\n&#​34; echo -e "---------------Configure primary and secondary synchronization and view---------------\n" #configure primary and secondary synchronization and view on DNS slave cp -f /etc/named.conf /etc/named.conf.bak1 sed -i '/zone &#​34;." IN {/,+3d&#​39; /etc/named.conf sed -i '/named.rfc1912.zones/d&#​39; /etc/named.conf sed -i 's/127.0.0.1/10.10.2.122/g&#​39; /etc/named.conf sed -i 's/localhost/any/g&#​39; /etc/named.conf sed -i "/allow-query/a allow-transfer { none; };\nmasterfile-format text;" /etc/named.conf echo "> key \&#​34;area-key\" {&#​34; echo ">         algorithm hmac-md5;" echo ">         secret \&#​34;areaSecret\";&#​34; echo "> };&#​34; echo "> key \&#​34;common-key\" {&#​34; echo ">         algorithm hmac-md5;" echo ">         secret \&#​34;commonSecret\";&#​34; echo "> };&#​34; echo "> EOF&#​34; cat >> /etc/named.conf <<EOF key "area-key&#​34; {         algorithm hmac-md5;         secret "areaSecret&#​34;; }; key "common-key&#​34; {         algorithm hmac-md5;         secret "commonSecret&#​34;; }; EOF read -p "Please enter the secret value in the area-key generated by the DNS Master host: " areaSecret sed -i 's#​areaSecret#&#​39;'&#​39;$areaSecret'&#​39;'#​g' /etc/named.conf read -p "Please enter the secret value in the common-key generated by the DNS Master host: " commonSecret sed -i 's#​commonSecret#&#​39;'&#​39;$commonSecret'&#​39;'#​g' /etc/named.conf read -p "系统等待&#​34; cat >> /etc/named.conf <<EOF view "area&#​34; {     match-clients{key area-key; 10.10.2.0/26;};     server 10.10.2.120 { keys area-key; };     zone ".&#​34; IN {         type hint;         file "named.ca&#​34;;     };     zone "domain.com&#​34; IN {         type slave;         file "com-domain-area&#​34;;         masters{10.10.2.120;};     };     zone "3.10.10.in-addr.arpa&#​34; IN {         type slave;         file "10.10.3.area&#​34;;         masters{10.10.2.120;};     }; }; view "common&#​34; {     match-clients { key common-key; any;};     server 10.10.2.120 { keys common-key; };     zone ".&#​34; IN {         type hint;         file "named.ca&#​34;;     };     zone "domain.com&#​34; IN {         type slave;         file "com-domain-common&#​34;;         masters{10.10.2.120;};     };     zone "4.10.10.in-addr.arpa&#​34; IN {         type slave;         file "10.10.4.common&#​34;;         masters{10.10.2.120;};     }; }; EOF #check the correctness of bind master configuration file named-checkconf /etc/named.conf systemctl reload named ls /var/named cat /var/named/com-domain-area cat /var/named/com-domain-common cat /var/named/10.10.3.area cat /var/named/10.10.4.common cat /var/named/data/named.run | head -n 60 echo -e "\n&#​34; read -n1 -p "---------------Please execute Script on Server Master---------------" echo -e "\n&#​34; echo -e "---------------Testing domain name resolution service on DNS slave---------------\n" yum install -y bind-utils dig www.domain.com @10.10.2.120 dig www.domain.com @10.10.2.122 #***************reader shell end*****************