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

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介:
  • 拓扑图:

  • 拓扑图.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

相关文章
|
8天前
|
存储 缓存 算法
分布式锁服务深度解析:以Apache Flink的Checkpointing机制为例
【10月更文挑战第7天】在分布式系统中,多个进程或节点可能需要同时访问和操作共享资源。为了确保数据的一致性和系统的稳定性,我们需要一种机制来协调这些进程或节点的访问,避免并发冲突和竞态条件。分布式锁服务正是为此而生的一种解决方案。它通过在网络环境中实现锁机制,确保同一时间只有一个进程或节点能够访问和操作共享资源。
25 3
|
10天前
|
Unix 物联网 大数据
操作系统的演化与比较:从Unix到Linux
本文将探讨操作系统的历史发展,重点关注Unix和Linux两个主要的操作系统分支。通过分析它们的起源、设计哲学、技术特点以及在现代计算中的影响,我们可以更好地理解操作系统在计算机科学中的核心地位及其未来发展趋势。
|
9天前
|
安全 Ubuntu 搜索推荐
|
11天前
|
Ubuntu Linux 网络安全
Linux中服务管理问题
【10月更文挑战第4天】
11 2
|
14天前
|
弹性计算 网络协议 Ubuntu
如何在阿里云国际版Linux云服务器中自定义配置DNS
如何在阿里云国际版Linux云服务器中自定义配置DNS
|
16天前
|
安全 Java 编译器
Java 泛型深入解析:类型安全与灵活性的平衡
Java 泛型通过参数化类型实现了代码重用和类型安全,提升了代码的可读性和灵活性。本文深入探讨了泛型的基本原理、常见用法及局限性,包括泛型类、方法和接口的使用,以及上界和下界通配符等高级特性。通过理解和运用这些技巧,开发者可以编写更健壮和通用的代码。
|
17天前
|
运维 网络协议 安全
Linux安全运维--一篇文章全部搞懂iptables
Linux安全运维--一篇文章全部搞懂iptables
27 1
|
9天前
|
安全 搜索推荐 Ubuntu
|
10天前
|
网络安全 Docker 容器
【Bug修复】秒杀服务器异常,轻松恢复网站访问--从防火墙到Docker服务的全面解析
【Bug修复】秒杀服务器异常,轻松恢复网站访问--从防火墙到Docker服务的全面解析
16 0
|
14天前
|
应用服务中间件 Linux Shell
Linux 配置 Nginx 服务的详细步骤,绝对干货
Linux 配置 Nginx 服务的详细步骤,绝对干货
43 0

相关产品

  • 云解析DNS