加强linux操作系统DNS服务安全

简介:
  • 拓扑图:

  • 拓扑图.png




  • 代码:


  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    #!/bin/bash
    #2017-12-3
    #by-duwentao
     
    domain_name= "hetian.com"
    rip=` ifconfig  eth0| grep  Mask| awk  -F  ":"  '{print $2}' | awk  -F  " "  '{print $1}' | awk  -F  "."  '{print $3"."$2"."$1}' `
    ip=` ifconfig  eth0| grep  Mask| awk  -F  ":"  '{print $2}' | awk  -F  " "  '{print $1}' `
    ip_last=` ifconfig  eth0| grep  Mask| awk  -F  ":"  '{print $2}' | awk  -F  " "  '{print $1}' | awk  -F  "."  '{print $4}' `
    named= "/etc/named.conf"                                   #主配文件
    Forward_parsing_file= "/var/named/$domain_name.zone"              #正向解析文件
    Reverse_parsing_file= "/var/named/$rip.in-addr.arpa.zone"  #反向解析文件
     
     
    #check the yum allocation of right and wrong 
    function  Check_Yum( )
      {
         yum clean all> /dev/null  2>&1   #判断yum配置文件的格式是否正确
         if  [ $? - ne  '0'  ]; then
             echo  -e  "\033[31m yum allocation is wrong \033[0m"
                     Repair_Yum
     
         else
             #定义变量用来查看yum有多少安装包
             Yum_Num=`yum repolist all| grep  repolist| awk  -F  ":"  '{print $2}' | sed  s '/,//' g| sed  s '/ //' g`
         
             if  [ $Yum_Num - eq   0 ]; then
                 echo  -e  "\033[31m yum allocation is wrong \033[0m"
                 Repair_Yum
             else 
                 echo  -e  "\033[32m yum allocation is right \033[0m"  
         
             fi
         fi
      }
      
    #repair yum
    function  Repair_Yum( )
    {
        dvd_file= "/etc/yum.repos.d/dvd.repo"
        read  -p  "Do you want to repair yum y/n:"  check
     
        if  "$check"  "y"  -o  "$check"  "yes"  -o  "$check"  "Y"  ]; then
         mount  /dev/cdrom  /mnt  1> /dev/null  2>&1  
         if  [ -f $dvd_file ]; then
            rm  -rf  $dvd_file
            touch  $dvd_file
    cat  >> $dvd_file << +END+
    [dvd]
    name=dvd
    baseurl= file : ///mnt
    gpgcheck=0
    +END+
             else
            touch  $dvd_file
    cat  >> $dvd_file << +END+
    [dvd]
    name=dvd
    baseurl= file : ///mnt
    gpgcheck=0
    +END+
         echo  -e  "\033[31m Please run it again and see the repair results \033[0m"
         exit  0
             fi
             
        elif  "$check"  "n"  -o  "$check"  "N"  -o  "$check"  "no"  ]; then
         echo  -e  "\033[31m yum allocation is wrong,Please configure it manually \033[0m"
         exit 
        else
         while  [[ 1 < 2 ]]
         do
             echo  -e  "\033[31m Your input is wrong,please try again \033[0m"
             Repair_Yum
         done
        fi   
     
     
    }
     
     
    #install dns server
    function  Install_DNS_Server( )
    {
         #把原有的卸载干净
         yum remove bind* -y
         rm  -rf  /var/named/ *
         rm  -rf   /etc/named .conf.rpm*
         #开始安全dns服务
         yum  install  bind bind-libs bind-utils -y
         /etc/init .d /named  restart >  /dev/null  2>&1
         if  [ $? - eq  0 ]; then
         
             echo  -e  "\033[32m DNS server install sucessful \033[0m"
         else
             echo  -e  "\033[31m DNS server install failed try again \033[0m"
         fi
         
    }
     
     
    #Edit named configuration file
     
    function  Edit_Conf ( )
    {
     
    #修改
    sed  -i  '/^include/d'  $named                 
    sed  -i s '/127.0.0.1/any/' g $named
    sed  -i s '/localhost/any/' g $named 
    sed  -i  '/recursion/i version "I do not tell you";'  $named     #隐藏版本号
    sed  -i s '/recursion yes/recursion no/' g $named              #关闭rescursion功能,防止spoofig
    sed  -i  '/recursion/i fetch-glue no;'  $named                   #关闭glue fetching功能
     
    ##Edit named configuration file
    cat  >> $named << +END+
     
    zone  "$domain_name"  IN{
         type  master;
         file  "$Forward_parsing_file" ;
      
    };
      
    zone  "$rip.in-addr.arpa"  IN{
         type  master;
         file  "$Reverse_parsing_file" ;
      
    };
      
    include  "/etc/named.rfc1912.zones" ;
    include  "/etc/named.root.key" ;
      
    +END+
     
     
    #Edit Forward and Reverse configuration file
    #Edit Forward  configuration file
    if  [ ! -f $Forward_parsing_file  ]; then
         touch  $Forward_parsing_file
    else
         rm  -rf $Forward_parsing_file
         touch  $Forward_parsing_file
    fi
     
    cat  >> $Forward_parsing_file << +END+
    \$TTL 1D
    @   IN SOA  $domain_name.  root.$domain_name. (
                         0   ; serial
                         1D  ; refresh
                         1H  ; retry
                         1W  ; expire
                         3H )   ; minimum
    @     IN     NS     dns.smile.com.
    dns   IN     A      $ip
    mail  IN     A      $ip
    www   IN     A      $ip
      
    +END+
     
    #Edit Reverse configuration file
     
    if  [ ! -f $Reverse_parsing_file  ]; then
             touch  $Reverse_parsing_file
    else
             rm  -rf $Reverse_parsing_file
             touch  $Reverse_parsing_file
    fi
      
     
    cat  >> $Reverse_parsing_file << +END+
      
    \$TTL 86400
    @   IN SOA  $rip. in -addr.arpa. root.$domain_name. (
                         0   ; serial
                         1D  ; refresh
                         1H  ; retry
                         1W  ; expire
                         3H )   ; minimum
    @     IN     NS     dns.$domain_name.
    $ip_last   IN    PTR     www.$domain_name.
    $ip_last   IN    PTR     mail.$domain_name.
      
    +END+
     
    #修改权限
    chgrp  named $named        
    chgrp  named $Reverse_parsing_file
    chgrp  named $Forward_parsing_file
     
    echo  "nameserver $ip"  >>  /etc/resolv .conf
    /etc/init .d /named  restart
     
     
    }
     
    #copy
    function  copy( )
    {
          scp  -r  /root/shell/DNS_Server_masters .sh root@172.24.10.106: /root
          if  [ $? - ne  '0'  ]; then
                  echo  -e  "\033[31m 拷贝失败 \033[0m" 
          else
                  echo  -e  "\033[32m 拷贝成功 \033[0m" 
          fi
         
         ssh  -l root 172.24.10.106  '/bin/bash /root/DNS_Server_masters.sh'
    }
     
    #chenge_DNS_slave
    function  chenge_DNS_slave ( )
    {
     
    #修改
    sed  -i  '/^include/d'  $named
    sed  -i s '/127.0.0.1/any/' g $named
    sed  -i s '/localhost/any/' g $named
    sed  -i  '/recursion/i version "I do not tell you";'  $named     #隐藏版本号
    sed  -i s '/recursion yes/recursion no/' g $named                #关闭rescursion功能,防止spoofig
    sed  -i  '/recursion/i fetch-glue no;'  $named                   #关闭glue fetching功能
     
    ##Edit named configuration file
    cat  >> $named << +END+
     
    zone  "$domain_name"  IN{
         type  slave;
         file  "$Forward_parsing_file" ;
         masters {172.24.10.105;};
    };
      
    zone  "$rip.in-addr.arpa"  IN{
         type  slave;
         file  "$Reverse_parsing_file" ;
         masters {172.24.10.105;};
    };
      
    include  "/etc/named.rfc1912.zones" ;
    include  "/etc/named.root.key" ;
      
    +END+
     
    chmod  g+w   /var/named/
    echo  "nameserver $ip"  >>  /etc/resolv .conf
    service named restart
    }
     
    #echo menu
    function  menu ( )
    {
     
             echo  -e  "\033[32m 1.检查yum是否配置正确 \033[0m"
            echo  -e  "\033[32m 2.安装DNS服务 \033[0m"
             echo  -e  "\033[32m 3.修改DNS-master服务的配置文件 \033[0m"
         echo  -e  "\033[32m 4.拷贝文件到目标主机并且进入目标主机\033[0m"
         echo  -e  "\033[32m 5.修改目标机的主配文件\033[0m"
             echo  -e  "\033[32m exit.输入exit或者ctrl+c退出 \033[0m"
             echo  -e  "\033[32m help.请输入1|2|3|help|exit \033[0m" 
     
         read  -p  "请选择你菜单:"  menu
     
     
             case  $menu  in
             1) Check_Yum
                ;;
             2) Install_DNS_Server
                ;;
             3) 
               if  [ `rpm -qa | grep  bind-9.8| wc  -l` - ne  1 ]; then
                echo  -e  "\033[31m 请确保已经安装DNS服务 \033[0m"
               fi
               Edit_Conf
               ;;
             4)copy
               ;;
                     5) chenge_DNS_slave
              ;;
             help)
                echo  -e  "\033[32m help.请输入1|2|3|4 \033[0m"
                menu
                ;;
             exit exit  0
                 ;;
             *)
                echo  -e  "\033[31m 输入有误,请重新输入 \033[0m"
               menu
               
               ;;
     
             esac
     
    }
     
    while  [[ 1 < 2 ]]
    do
    hostname =` hostname `
         if  "$hostname"  =   "DNS_SERVER_1"   ]; then
     
                     echo  "当前设备是DNS_SERVER_1:请选择你菜单:" 
             menu
             else
                     echo  "当前设备是$hostname:请选择你菜单:"
                  menu
             fi
    done

执行结果:

优酷视频


微信公众号:

qrcode_for_gh_0cd223682950_344.jpg



本文转自 天道酬勤VIP 51CTO博客,原文链接:http://blog.51cto.com/tdcqvip/2047790

相关文章
|
5月前
|
Linux 网络安全 Docker
盘古栈云,创建带ssh服务的linux容器
创建带ssh服务的linux容器
381 146
|
6月前
|
缓存 安全 Linux
六、Linux核心服务与包管理
在没有网络的情况下,使用系统安装光盘是获取RPM包的常用方法。场景二:配置本地文件镜像源 (使用系统安装光盘/ISO)(检查RPM包的GPG签名以保证安全) 或。YUM/DNF包管理工具 (yum/dnf)(此处可以放置您为本主题制作的思维导图)处理依赖问题的危险选项 (应极力避免)(覆盖文件、替换已安装包)。(list) 则是列出文件。(query file) 是。(假设系统安装光盘已挂载到。信息 (verbose)。(upgrade) 选项。(all) 已安装的包。(package) 选项
493 11
|
6月前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
298 0
SonarQube Server 2025 Release 5 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
7月前
|
安全 Linux C++
PVS‑Studio 7.38 for macOS, Linux & Windows - 代码质量安全静态分析
PVS‑Studio 7.38 for macOS, Linux & Windows - 代码质量安全静态分析
315 0
PVS‑Studio 7.38 for macOS, Linux & Windows - 代码质量安全静态分析
|
7月前
|
Ubuntu Unix Linux
操作系统的最强入门科普(Unix/Linux篇)
下期文章,小枣君会重点聊聊Windows和macOS那条线。敬请关注! 如果大家觉得文章不错,还请帮忙多多转发!谢谢!
|
7月前
|
监控 Ubuntu Linux
什么Linux,Linux内核及Linux操作系统
上面只是简单的介绍了一下Linux操作系统的几个核心组件,其实Linux的整体架构要复杂的多。单纯从Linux内核的角度,它要管理CPU、内存、网卡、硬盘和输入输出等设备,因此内核本身分为进程调度,内存管理,虚拟文件系统,网络接口等4个核心子系统。
458 0
|
7月前
|
Unix 物联网 Linux
都什么年代了,你还不懂啥是Linux操作系统
至于华为鸿蒙操作系统是不是独树一帜,这个留给各位阅读本文的网友们来讨论
245 0
|
7月前
|
Web App开发 缓存 Rust
|
7月前
|
安全 Linux iOS开发
linux属于什么操作系统
Linux是一种自由和开放源代码的操作系统,具有高度的灵活性和可定制性。与常见的操作系统如Windows和macOS相比,Linux具有自由、安全和稳定等优势。Linux已广泛应用于服务器、桌面电脑、超级计算机和嵌入式设备等领域,并且在未来的发展前景广阔。由于其自由和开放源代码的特性,Linux还促进了计算机技术和社区的发展,为全球的计算机用户提供了更多的选择和可能性。
|
7月前
|
安全 Ubuntu Unix
关于Linux操作系统,你必须要知道的事
我们可以看到无论是Debian还是Buildroot都有各自的特点,为客户提供了更大的选择空间和灵活性,大家可以根据自己的需求选择合适的版本来满足终端用户的体验和功能需求。从平技术将会一直关注更多更安全、灵敏、易于开发的Linux版本,做好适配工作,不断为客户带来“简单开发、方便应用”的使用体验。

相关产品

  • 云解析DNS