Linux 自动化部署DNS服务器

1.首先配置主DNS服务器的IP地址,DNS地址一个写主dns的IP地址,一个写从dns的地址,这里也可以不写,在测试的时候在/etc/resolv.conf中添加记录。 

2.配置从dns的IP地址

3. 运行脚本,选择1,部署主DNS服务器   

4.运行完脚本之后,主DNS就部署完成了,查看配置文件 /etc/named.conf 

5. 查看区域文件 区域文件在 /var/named/  

6.本地测试主DNS服务器是否部署成功

 

7. 在从DNS服务器上运行脚本,选择2,部署从DNS服务

8.查看 

9.查看 /var/named/slaves/目录下,可以看到生成两个文件,说明从dns配置成功,从主dns获得区域文件

10.在另一个客户端测试,在/etc/resolv.conf添加如下两条记录

11. 测试,下图可以看到主、从DNS都正常 

 

源码如下:  

#!/bin/bash
#该脚本用于自动化部署主DNS服务器与从DNS服务器
#作者:雨中落叶
#博客:https://www.cnblogs.com/yuzly/
echo "****************************
1.部署主dns服务器
2.部署从dns服务器
****************************"
read -p "请输入部署选项:" Num
case $Num in
1)
  #关闭防火墙,避免影响实验
  service iptables stop &>/dev/null
  setenforce 0 &>/dev/null
  named="/etc/named.conf"
  #定义函数
  alterDNS(){
    #配置主文件
    rm -fr $named
    touch $named
    #获得本机IP
    ETH=$(ifconfig |grep "^eth" |awk \'{print $1}\')
    lIP=$(ifconfig $ETH |grep "inet addr"|awk  \'{print $2}\'|awk -F: \'{print $2}\')
    read -p "请输入你要创建的正向区域名:" AreaName
    read -p "请输入从DNS服务器的IP地址:" slaveIP
    fan=$(echo $lIP |awk -F. \'{print $3"."$2"."$1}\')
    cat >>$named<<OK
options {
        listen-on port 53 { $lIP; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        allow-query     { any; };
};
zone "." IN {
        type hint;
        file "named.ca";
};
zone "$AreaName" IN {
        type master;
        file "$AreaName.zone";
        allow-transfer { $slaveIP; };
};
zone "$fan.in-addr.arpa" IN {
        type master;
        file "$fan.in-addr.arpa.zone";
        allow-transfer { $slaveIP; };
};
OK
#配置正向解析文件
  touch /var/named/$AreaName.zone
cat >>/var/named/$AreaName.zone<<OK
\$TTL 86400
@       IN SOA  $AreaName. admin.$AreaName. (
                                        2019032211       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
@    IN    NS    ns1.$AreaName.
    IN    NS    ns2.$AreaName.
    IN    MX 10    mail.$AreaName.
ns1    IN    A    $lIP
ns2    IN    A    $slaveIP
www    IN    A    $lIP
mail    IN    A    $lIP
news    IN    CNAME    www.$AreaName.
OK
#配置反向解析文件
touch /var/named/$fan.in-addr.arpa.zone
fourthlIP=$(echo $lIP |awk -F. \'{print $4}\')
fourthslaveIP=$(echo $slaveIP |awk -F. \'{print $4}\')
cat >>/var/named/$fan.in-addr.arpa.zone<<OK
\$TTL 86400
@       IN SOA  $AreaName. admin.$AreaName. (
                                        2019032211       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
@           IN      NS      ns1.$AreaName.
            IN      NS      ns2.$AreaName.
$fourthlIP      IN      PTR     ns1.$AreaName.
$fourthslaveIP   IN      PTR     ns2.$AreaName.
$fourthlIP         IN      PTR     www.$AreaName. 
$fourthlIP        IN      PTR     mail.$AreaName.
$fourthlIP        IN      PTR     news.$AreaName.   
OK
}
if [ ! -f $named ]
 then
    echo "当前电脑没有安装DNS服务,正在安装,请稍等....."
    mount /dev/sr0 /mnt &>/dev/null
    #搭建本地yum仓库
    cd /etc/yum.repos.d
    cat >>yuzly.repo<<OK
[yuzly]
name=yuzly
baseurl=file:///mnt
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
OK
    yum install bind -y &>/dev/null
    #调用函数
    alterDNS
    service named start &>/dev/null
 else
    alterDNS
    service named start &>/dev/null
 fi
;;
2)
    #关闭防火墙
    service iptables stop &>/dev/null
    setenforce 0 &>/dev/null
    #修改配置文件
    #获得本机IP
    ETH=$(ifconfig |grep "^eth" |awk \'{print $1}\')
    lIP=$(ifconfig $ETH |grep "inet addr"|awk  \'{print $2}\'|awk -F: \'{print $2}\')
    
    named="/etc/named.conf"
    read -p "请输入主服务器创建的正向区域名:" AreaName
    read -p "请输入主DNS服务器的IP地址:" IP
    fan=$(echo $IP |awk -F. \'{print $3"."$2"."$1}\')    
    setArea(){
    cat >>$named<<OK  
options {
        listen-on port 53 { $lIP; };
        directory       "/var/named";
        allow-query     { any; };
};
zone "$AreaName" IN {
        type slave;
        masters { $IP; };
        file "slaves/$AreaName.zone";
};
zone "$fan.in-addr.arpa" IN {
        type slave;
        masters { $IP; };
        file "slaves/$fan.in-addr.arpa.zone";
};
OK
    service  named start &>/dev/null
}
    if [ ! -f $named ]
    then
       echo "当前电脑没有安装DNS服务,正在安装,请稍等....."
       mount /dev/sr0 /mnt &>/dev/null
       #搭建本地yum仓库
       cd /etc/yum.repos.d
       cat >>yuzly.repo<<OK
[yuzly]
name=yuzly
baseurl=file:///mnt
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
OK
       yum install bind -y &>/dev/null
       rm -fr $named
       touch $named
       #调用函数
       setArea 
   else
      rm -fr $named
      touch $named
      setArea    
  fi
;;
*)
   echo "请输入正确的数字!"
;;
esac

 

 

——————————————————————————————————-

 Linux DNS服务配置:https://www.cnblogs.com/yuzly/p/10581688.html 

版权声明:本文为yuzly原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yuzly/p/10581390.html