脚本一键实现主从DNS服务器配置

    科技2024-10-05  19

    目录

    前言实验环境脚本文件实验检验

    前言

    主域名服务器通常架设在生成环境中,提供某一个或几个域名的主机名与IP地址的查询。从域名服务器可以分担域名查询的压力,提供区域数据的备份。

    实验环境

    主DNS服务器IP地址:172.16.1.10 从DNS服务器IP地址:172.16.1.11 客户机服务器IP地址:172.16.1.12

    脚本文件

    主服务器脚本

    [root@server-1 ~]# vim dns-main.sh #!/bin/bash rpm -q bind if [ $? -eq 0 ] then echo "BIND程序已安装" else yum -y install bind* fi NAMED1=/etc/named.conf NAMED2=/etc/named.rfc1912.zones ZONE=cjx.com.zone sed -i -e '/listen-on port/s/127.0.0.1/172.16.1.10/' $NAMED1 sed -i -e '/allow-query/s/localhost/any/' $NAMED1 cat >> $NAMED2 << EOF zone "cjx.com" IN { type master; file "cjx.com.zone"; allow-transfer { 172.16.1.11; }; also-notify { 172.16.1.11; }; }; zone "1.16.172.in-addr.arpa" IN { type master; file "cjx.com.zone"; allow-transfer { 172.16.1.11; }; }; EOF cp -p /var/named/named.localhost /var/named/$ZONE #复制模板修改 sed -i -e 's/@/cjx.com./2' /var/named/$ZONE sed -i -e '/NS/s/@/cjx.com./' /var/named/$ZONE sed -i -e 's/127.0.0.1/172.16.1.10/1' /var/named/$ZONE cat >> /var/named/$ZONE << EOF www IN A 172.16.1.10 1 IN PTR www.cjx.com EOF systemctl start named netstat -anptu | grep named cat >> /etc/resolv.conf << EOF nameserver 172.16.1.10 nameserver 172.16.1.11 EOF

    从服务器

    [root@server-2 ~]# vim dns-slave.sh #!/bin/bash rpm -q bind if [ $? -eq 0 ] then echo "BIND程序已安装" else yum -y install bind* fi NAMED1=/etc/named.conf NAMED2=/etc/named.rfc1912.zones ZONE=cjx.com.zone sed -i -e '/listen-on port/s/127.0.0.1/172.16.1.11/' $NAMED1 sed -i -e '/allow-query/s/localhost/any/' $NAMED1 cat >> $NAMED2 << EOF zone "cjx.com" IN { type slave; masters { 172.16.1.10; }; allow-notify { 172.16.1.10; }; file "slaves/cjx.com.zone"; }; zone "1.16.172.in-addr.arpa" IN { type slave; masters { 172.16.1.10; }; allow-notify { 172.16.1.10; }; file "slaves/cjx.com.zone" ; }; EOF cat >> /var/named/$ZONE << EOF www IN A 172.16.1.10 1 IN PTR www.cjx.com EOF systemctl start named netstat -anptu | grep named cat >> /etc/resolv.conf << EOF nameserver 172.16.1.10 nameserver 172.16.1.11 EOF

    实验检验

    客户机检验脚本如下

    [root@client-1 ~]# vim test.sh #!/bin/bash echo "nameserver 172.16.1.10" > /etc/resolv.conf nslookup www.cjx.com echo "nameserver 172.16.1.11" > /etc/resolv.conf nslookup www.cjx.com

    检验效果如下

    [root@client-1 ~]# bash test.sh Server: 172.16.1.10 Address: 172.16.1.10#53 Name: www.cjx.com Address: 172.16.1.10 Server: 172.16.1.11 Address: 172.16.1.11#53 Name: www.cjx.com Address: 172.16.1.10
    Processed: 0.010, SQL: 8