Centos使用Bind配置智能DNS的教程

作者:袖梨 2022-06-30

主DNS服务器:

[root@Master /]# cat /etc/redhat-release
CentOS release 6.4 (Final)
[root@Master /]# uname -a
Linux Master 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@Master /]# ifconfig | grep "inet addr"
          inet addr:1.1.1.19  Bcast:1.1.1.255  Mask:255.255.255.0
[root@MASTER /]#  ifconfig eth0:0 192.168.1.19 netmask 255.255.255.0 

从DNS服务器:


[root@Slave ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@Slave ~]# uname -a
Linux Slave 2.6.32-573.18.1.el6.x86_64 #1 SMP Tue Feb 9 22:46:17 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@Slave ~]# ifconfig | grep "inet addr"
          inet addr:1.1.1.20  Bcast:1.1.1.255  Mask:255.255.255.0
主DNS服务器编译:


[root@Master /]# mkdir /byrd/tools -p   
[root@Master /]# mkdir /byrd/service   
[root@Master /]# cd /byrd/tools/
[root@MASTER tools]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.28.tar.gz  
[root@MASTER tools]# tar zxf mysql-5.6.28.tar.gz
[root@MASTER tools]# cd mysql-5.6.28
[root@MASTER mysql-5.6.28]# cmake . -DCMAKE_INSTALL_PREFIX=/byrd/service/mysql-5.6.28 -DMYSQL_DATADIR=/byrd/service/mysql-5.6.28/data -DMYSQL_UNIX_ADDR=/byrd/service/mysql-5.6.28/tmp/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
[root@MASTER mysql-5.6.28]# make && make install
[root@MASTER mysql-5.6.28]# groupadd mysql
[root@MASTER mysql-5.6.28]# useradd -s /sbin/nologin -g mysql mysql
[root@MASTER mysql-5.6.28]# ln -s /byrd/service/mysql-5.6.28 /usr/local/mysql
[root@MASTER mysql-5.6.28]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
[root@MASTER mysql-5.6.28]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@MASTER mysql-5.6.28]# mv /etc/my.cnf /etc/my.cnf.backup
[root@MASTER mysql-5.6.28]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
[root@MASTER mysql-5.6.28]# /etc/init.d/mysqld start
[root@MASTER mysql-5.6.28]# cd ..
[root@Master tools]# wget ftp://ftp.isc.org/isc/bind9/9.9.8/bind-9.9.8.tar.gz   
[root@Master tools]# tar zxf bind-9.9.8.tar.gz   
[root@Master tools]# cd bind-9.9.8   
[root@Master bind-9.9.8]# ./configure -h 
[root@MASTER bind-9.9.8]# ./configure --prefix=/byrd/service/bind-9.9.8 --with-dlz-mysql=/usr/local/mysql --with-libtool --enable-threads --enable-ipv6 --with-dlz-filesystem=yes 'CFLAGS= -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' 'CPPFLAGS= -DDIG_SIGCHASE'
[root@Master bind-9.9.8]# make && make install
/usr/bin/install -c -m 644 ./bind.keys /byrd/service/bind-9.9.8/etc
[root@Master bind-9.9.8]# ln -s /byrd/service/bind-9.9.8 /usr/local/named
[root@Master bind-9.9.8]# groupadd -r named -g 25
[root@Master bind-9.9.8]# useradd named -u 25 -g named -d /byrd/service/bind-9.9.8/ -s /sbin/nologin

主DNS服务器配置:

[root@Master bind-9.9.8]# cd /usr/local/named/etc
[root@Master etc]# mkdir /var/named
[root@Master etc]# chown -R named:named /var/named/
[root@Master etc]# chown -R named:named /usr/local/named
[root@Master etc]# chmod 700 /usr/local/named/etc/
[root@Master etc]# chown -R named.named /usr/local/named/etc/
[root@Master etc]# cd /var/named/
[root@MASTER named]# /usr/local/named/bin/dig > named.root    #wget https://www.internic.net/domain/named.root
[root@MASTER named]# chown -R root.named /var/named/named.root
[root@Master named]# /usr/local/named/sbin/rndc-confgen > /usr/local/named/etc/rndc.conf    #/usr/local/named/sbin/rndc-confgen -r /dev/urandom > /usr/local/named/etc/rndc.conf
[root@Master named]# tail /usr/local/named/etc/rndc.conf | head -9 |sed s/#//g > /usr/local/named/etc/named.conf
[root@Master named]# cd -
[root@Master etc]# chown -R named.named  /usr/local/named/var/
[root@MASTER etc]# mkdir {/var/named/data,/var/named/zones} -p
[root@MASTER etc]# chown -R named.named {/var/named/data,/var/named/zones}
[root@MASTER etc]# grep -v "#" /var/local/named/etc/named.conf    
[root@MASTER zones]# cat /usr/local/named/etc/named.conf
// named.conf
options {
        listen-on port 53 { any; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { any; };
        recursion yes;
        bindkeys-file "/usr/local/named/etc/bind.key";
        allow-transfer { 1.1.1.20; };
#        forwarders {114.114.114.114;};
};
logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
        channel gsquery {
                file "data/query.log"   versions 3 size 20m;
                severity info;
                print-time yes;
                print-category yes;
                print-severity yes;
        };
        category queries { gsquery; };
};
key "rndc-key" {
        algorithm hmac-md5;
        secret "ovH1P5Jg000GLBWJYifXRQ==";
};
 
controls {
        inet 127.0.0.1 port 953
                allow { 127.0.0.1; } keys { "rndc-key"; };
};
include "/var/named/named.rfc1912.zones";
[root@MASTER zones]# cat /var/named/named.rfc1912.zones
zone "localhost" IN {
        type master;
        file "zones/named.localhost";
        allow-update { none; };
};
zone "1.0.0.127.in-addr.arpa" IN {
        type master;
        file "zones/named.loopback";
        allow-update { none; };
};
zone "111com.net " IN {
        type master;
        file "zones/111com.net .zone";
        allow-update { none; };
        allow-transfer { 1.1.1.20; };
        notify yes;
        also-notify { 1.1.1.20; };   #允许slave主机以外的备份
};
zone "1.168.192.in-addr.arpa" {
        type master;
        file "zones/192.168.1.zone";
        allow-update { none; };
        notify yes;
        allow-transfer { 1.1.1.20; };
};
[root@MASTER zones]# cat /var/named/zones/111com.net .zone
$TTL 600
@        IN       SOA         ns1.111com.net .      admin.111com.net . (
                              2015050702       ; serial
                              1D               ; refresh
                              1H               ; retry
                              1W               ; expire
                              3H )             ; minimum
          IN      NS          ns1
          IN      NS          ns2.111com.net .
          IN      A           1.1.1.19
ns1       IN      A           1.1.1.19
ns2       IN      A           1.1.1.20
www       IN      A           192.168.1.19
bbs       IN      A           192.168.1.92
qwer      IN      A           1.2.3.4
test      IN      CNAME       www
blog      IN      CNAME       www
[root@MASTER zones]# cat /var/named/zones/192.168.1.zone
$TTL 1D
@               IN      SOA             ns1.111com.net .     admin.111com.net . (
                                        2015050701       ; serial
                                        10               ; refresh
                                        1H               ; retry
                                        1W               ; expire
                                        3H )             ; minimum
@               IN      NS              ns1.111com.net .
@               IN      NS              ns2.111com.net .
                        PTR             localhost.
19              IN      PTR             ns1.111com.net .
20              IN      PTR             ns2.111com.net .
19              IN      PTR             www.111com.net .
19              IN      PTR             ww.111com.net .
[root@MASTER zones]# cat named.localhost
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
        AAAA    ::1
        PTR     localhost.
[root@MASTER zones]# cat named.loopback
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
        AAAA    ::1
        PTR     localhost.

[root@MASTER zones]# /usr/local/named/sbin/named -c /usr/local/named/etc/named.conf -4u named

正解、反解测试:

[root@MASTER zones]# /usr/local/named/bin/dig www.111com.net
 
;; QUESTION SECTION:
;www.111com.net .                   IN      A
;; ANSWER SECTION:
www.111com.net .            600     IN      A       192.168.1.19
;; AUTHORITY SECTION:
111com.net .                600     IN      NS      ns1.111com.net .
;; ADDITIONAL SECTION:
ns1.111com.net .            600     IN      A       1.1.1.19
 
[root@MASTER zones]# /usr/local/named/bin/dig blog.111com.net
 
;; QUESTION SECTION:
;blog.111com.net .                  IN      A
;; ANSWER SECTION:
blog.111com.net .           600     IN      CNAME   www.111com.net .
www.111com.net .            600     IN      A       192.168.1.19
 
[root@MASTER zones]# /usr/local/named/bin/dig -x 192.168.1.19
 
;; QUESTION SECTION:
;19.1.168.192.in-addr.arpa.     IN      PTR
;; ANSWER SECTION:
19.1.168.192.in-addr.arpa. 86400 IN     PTR     www.111com.net .
19.1.168.192.in-addr.arpa. 86400 IN     PTR     ns1.111com.net .
;; AUTHORITY SECTION:
1.168.192.in-addr.arpa. 86400   IN      NS      ns1.111com.net .
;; ADDITIONAL SECTION:
ns1.111com.net .            600     IN      A       1.1.1.19
 
[root@MASTER zones]# /usr/local/named/bin/dig -x 192.168.1.92
 
;; QUESTION SECTION:
;92.1.168.192.in-addr.arpa.     IN      PTR
;; ANSWER SECTION:
92.1.168.192.in-addr.arpa. 86400 IN     PTR     bbs.111com.net .
;; AUTHORITY SECTION:
1.168.192.in-addr.arpa. 86400   IN      NS      ns1.111com.net .

从DNS服务器编译:


[root@Slave bind-9.9.8]# yum install vim get openssl openssl-devel lrzsz dos2unix gcc gcc-c++ -y
[root@Slave ~]# mkdir /byrd/tools -p
[root@Slave ~]# mkdir /byrd/service
[root@Slave ~]# cd /byrd/tools/
[root@Slave tools]# wget ftp://ftp.isc.org/isc/bind9/9.9.8/bind-9.9.8.tar.gz
[root@Slave tools]# tar zxf bind-9.9.8.tar.gz
[root@Slave tools]# cd bind-9.9.8
[root@Slave bind-9.9.8]# ./configure --prefix=/byrd/service/bind-9.9.8  --with-libtool --enable-threads --enable-ipv6 'CFLAGS= -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' 'CPPFLAGS= -DDIG_SIGCHASE'
[root@Slave bind-9.9.8]# make && make install
[root@Slave bind-9.9.8]# /usr/local/named/sbin/rndc-confgen -r /dev/urandom > /usr/local/named/etc/rndc.conf
[root@Slave etc]# tail /usr/local/named/etc/rndc.conf | head -9 |sed s/#//g > /usr/local/named/etc/named.conf
[root@Slave etc]# mkdir {/var/named/data,/var/named/slaves/} -p
[root@Slave etc]# chown -R named.named {/var/named/data,/var/named/slaves/}
从DNS服务器配置:

[root@Slave named]# cat /usr/local/named/etc/named.conf
options {
        listen-on port 53 { any; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { any; };
        recursion yes;
        bindkeys-file "/usr/local/named/etc/bind.key";
};
logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
        channel gsquery {
                file "data/query.log"   versions 3 size 20m;
                severity info;
                print-time yes;
                print-category yes;
                print-severity yes;
        };
        category queries { gsquery; };
};
key "rndc-key" {
        algorithm hmac-md5;
        secret "IZMCwXjEaNM1nkSQ+1qQxA==";
};
 
controls {
        inet 127.0.0.1 port 953
                allow { 127.0.0.1; } keys { "rndc-key"; };
};
 
include "/var/named/named.rfc1912.zones";
[root@Slave named]# cat /var/named/named.rfc1912.zones
zone "111com.net " IN {
        type slave;
        file "slaves/111com.net .zone";
        masters { 1.1.1.19; };
        allow-transfer { none; };
};
 
zone "1.168.192.in-addr.arpa" {
        type slave;
        file "slaves/192.168.1.zone";
        masters { 1.1.1.19; };
        allow-transfer { none; };
};

验证从服务器同步:


[root@MASTER zones]# cat 111com.net .zone
$TTL 60
@        IN       SOA         ns1.111com.net .      admin.111com.net . (
                              2015050703       ; serial
          IN      NS          ns1
          IN      NS          ns2.111com.net .
          IN      A           1.1.1.19
ns1       IN      A           1.1.1.19
test      IN      CNAME       www
 
 
 23 21:43:21 MASTER named[38968]: client 1.1.1.20#35403 (111com.net ): transfer of '111com.net /IN': AXFR-style IXFR started
 23 21:43:21 MASTER named[38968]: client 1.1.1.20#35403 (111com.net ): transfer of '111com.net /IN': AXFR-style IXFR ended
 23 21:43:21 MASTER named[38968]: client 1.1.1.20#58007: received notify for zone '111com.net '
 
[root@Slave named]# /usr/local/named/sbin/named-compilezone -f raw -F text -o /tmp/111com.net .txt 111com.net  /var/named/slaves/111com.net .zone
zone 111com.net /IN: loaded serial 2015050703
[root@Slave named]# cat /tmp/111com.net .txt
111com.net .                                      60 IN SOA         ns1.111com.net . admin.111com.net . 2015050703 86400 3600 604800 10800
111com.net .                                      60 IN NS          ns1.111com.net .
111com.net .                                      60 IN NS          ns2.111com.net .
111com.net .                                      60 IN A           1.1.1.19
bbs.111com.net .                                  60 IN A           192.168.1.92
blog.111com.net .                                 60 IN CNAME       www.111com.net .
ns1.111com.net .                                  60 IN A           1.1.1.19
ns2.111com.net .                                  60 IN A           1.1.1.20
qwer.111com.net .                                 60 IN A           1.2.3.4
test.111com.net .                                 60 IN CNAME       www.111com.net .
www.111com.net .                                  60 IN A           192.168.1.19
 
 
 23 21:43:21 Slave named[25033]: transfer of '111com.net /IN' from 1.1.1.19#53: connected using 1.1.1.20#35403
 23 21:43:21 Slave named[25033]: zone 111com.net /IN: transferred serial 2015050703
 23 21:43:21 Slave named[25033]: transfer of '111com.net /IN' from 1.1.1.19#53: Transfer status: success
 23 21:43:21 Slave named[25033]: transfer of '111com.net /IN' from 1.1.1.19#53: Transfer completed: 1 messages, 12 records, 298 bytes, 0.002 secs (149000 bytes/sec)
 23 21:43:21 Slave named[25033]: zone 111com.net /IN: sending notifies (serial 2015050703)
 
[root@MASTER zones]# tail -3 111com.net .zone
q         IN      A           11.11.111.11
a.q       IN      A           22.22.22.22
b.q       IN      A           33.33.33.33
[root@MASTER zones]# head -3 /var/named/zones/111com.net .zone 
                              2015050704       ; serial
[root@MASTER zones]# /usr/local/named/sbin/rndc reload
 
[root@Slave etc]# /usr/local/named/sbin/named-compilezone -f raw -F text -o /tmp/111com.net .txt 111com.net  /var/named/slaves/111com.net .zone
zone 111com.net /IN: loaded serial 2015050704
dump zone to /tmp/111com.net .txt...done
OK
[root@Slave etc]# cat /tmp/111com.net .txt
111com.net .                                      60 IN SOA         ns1.111com.net .111com.net . admin.111com.net . 2015050704 86400 3600 604800 10800
111com.net .                                      60 IN NS          ns1.111com.net .
111com.net .                                      60 IN NS          ns2.111com.net .
111com.net .                                      60 IN A           1.1.1.19
bbs.111com.net .                                  60 IN A           192.168.1.92
blog.111com.net .                                 60 IN CNAME       www.111com.net .
ns1.111com.net .                                  60 IN A           1.1.1.19
ns2.111com.net .                                  60 IN A           1.1.1.20
q.111com.net .                                    60 IN A           11.11.111.11
a.q.111com.net .                                  60 IN A           22.22.22.22
b.q.111com.net .                                  60 IN A           33.33.33.33
qwer.111com.net .                                 60 IN A           1.2.3.4
test.111com.net .                                 60 IN CNAME       www.111com.net .
www.111com.net .                                  60 IN A           192.168.1.19
yum版本编译参数:

0
--build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --target=x86_64-redhat-linux-gnu --program-prefix= --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/libexec --sharedstatedir=/var/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-libtool --localstatedir=/var --enable-threads --enable-ipv6 --enable-filter-aaaa --with-pic --disable-static --disable-openssl-version-check --enable-rpz-nsip --enable-rpz-nsdname --with-dlopen=yes --with-dlz-ldap=yes --with-dlz-postgres=yes --with-dlz-mysql=yes --with-dlz-filesystem=yes --with-gssapi=yes --disable-isc-spnego --with-docbook-xsl=/usr/share/sgml/docbook/xsl-stylesheets --enable-fixed-rrset build_alias=x86_64-redhat-linux-gnu host_alias=x86_64-redhat-linux-gnu target_alias=x86_64-redhat-linux-gnu 'CFLAGS= -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' 'CPPFLAGS= -DDIG_SIGCHASE'

启动脚本:{使用yum版本的脚本,稍作修改}

Shell


[root@MASTER init.d]# cat named
#!/bin/bash
#
# named           This shell script takes care of starting and stopping
#                 named (BIND DNS server).
#
# chkconfig: - 13 87
# description: named (BIND) is a Domain Name Server (DNS)
# that is used to resolve host names to IP addresses.
# probe: true
 
### BEGIN INIT INFO
# Provides: $named
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $network $syslog
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: start|stop|status|restart|try-restart|reload|force-reload DNS server
# Description: control ISC BIND implementation of DNS server
### END INIT INFO
 
# Source function library.
. /etc/rc.d/init.d/functions
 
[ -r /etc/sysconfig/named ] && . /etc/sysconfig/named
 
RETVAL=0
export KRB5_KTNAME=${KEYTAB_FILE:-/etc/named.keytab}
 
named='named'
#if [ -x /usr/sbin/named-sdb ]; then
#        named='named-sdb'
#fi
 
# Don't kill named during clean-up
NAMED_SHUTDOWN_TIMEOUT=${NAMED_SHUTDOWN_TIMEOUT:-25}
 
if [ -n "$ROOTDIR" ]; then
   ROOTDIR=`echo $ROOTDIR | sed 's#//*#/#g;s#/$##'`;
   rdl=`/usr/bin/readlink $ROOTDIR`;
   if [ -n "$rdl" ]; then
      ROOTDIR="$rdl";
   fi;
fi
 
PIDFILE="/usr/local/named/var/run/named/named.pid"
 
ROOTDIR_MOUNT='/etc/named /etc/pki/dnssec-keys /var/named /etc/named.conf
/etc/named.dnssec.keys /etc/named.rfc1912.zones /etc/rndc.conf /etc/rndc.key
/usr/lib64/bind /usr/lib/bind /etc/named.iscdlv.key /etc/named.root.key'
umount_chroot_conf()
{
  if [ -n "$ROOTDIR" ]; then
    for all in $ROOTDIR_MOUNT; do
      # Check if file is mount target. Do not use /proc/mounts because detecting
      # of modified mounted files can fail.
      if mount | grep -q '.* on '"$ROOTDIR$all"' .*'; then
        umount "$ROOTDIR$all"
        # Remove temporary created files
        [ -f "$all" ] && rm -f "$ROOTDIR$all"
      fi
    done
  fi
}
 
check_pidfile() {
  PID="`pidofproc -p "$ROOTDIR$PIDFILE" "$named"`"
  if [ -n "$PID" ] && [ "`ps -p "$PID" --no-headers -o comm`" != "$named" ]; then
    rm -f $ROOTDIR$PIDFILE &> /dev/null
  fi
}
 
pidofnamed() {
        pidofproc -p "$ROOTDIR$PIDFILE" "$named";
}
 
# Check if all what named needs running
start()
{
  [ "$EUID" != "0" ] && exit 4
 
  # Source networking configuration.
  [ -r /etc/sysconfig/network ] && . /etc/sysconfig/network
 
  # Check that networking is up
#  [ "${NETWORKING}" = "no" ] && exit 1
 
 
#  [ -x /usr/sbin/"$named" ] || exit 5
 
  if [ ! -s /usr/local/named/etc/rndc.key -a ! -s /usr/local/named/etc/rndc.conf ]; then
    # Generate rndc.key if doesn't exist AND there is no rndc.conf
    echo -n $"Generating /usr/local/named/etc/rndc.key:"
    if /usr/local/named/sbin/rndc-confgen -a -r /dev/urandom > /dev/null 2>&1; then
      chmod 640 /usr/local/named/etc/rndc.key
      chown root.named /usr/local/named/etc/rndc.key
      [ -x /sbin/restorecon ] && /sbin/restorecon /usr/local/named/etc/rndc.key
      success $"/usr/local/named/etc/rndc.key generation"
      echo
    else
      failure $"/usr/local/named/etc/rndc.key generation"
      echo
    fi
  fi
 
  # Handle -c option
  previous_option='unspecified';
  for a in $OPTIONS; do
    if [ $previous_option = '-c' ]; then
      named_conf=$a;
    fi;
    previous_option=$a;
  done;
 
  named_conf=${named_conf:-/usr/local/named/etc/named.conf};
 
  # check if named is running before mounting files/dirs
  echo -n $"Starting named: "
  check_pidfile
  if [ -n "`pidofnamed`" ]; then
    echo -n $"named: already running"
    success
    echo
    exit 0;
  fi;
 
  if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
    OPTIONS="${OPTIONS} -t ${ROOTDIR}"
    ckcf_options="$ckcf_options -t ${ROOTDIR}";
    [ -s /etc/localtime ] && cp -fp /etc/localtime ${ROOTDIR}/etc/localtime;
  fi
 
  RETVAL=0
  # check if configuration is correct
  if [ -x /usr/local/named/sbin/named-checkconf ] && [ -x /usr/local/named/sbin/named-checkzone ] && /usr/local/named/sbin/named-checkconf $ckcf_options ${named_conf} >/dev/null 2>&1; then
 
    daemon --pidfile "$ROOTDIR$PIDFILE" /usr/local/named/sbin/"$named" -4u named ${OPTIONS};
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
      rm -f /var/run/{named,named-sdb}.pid;
      ln -s "$ROOTDIR$PIDFILE" /var/run/"$named".pid;
    fi;
 
  else
    named_err="`/usr/local/named/sbin/named-checkconf $ckcf_options $named_conf 2>&1`";       
    echo
    echo "Error in named configuration:";
    echo "$named_err";
    failure
    echo
    [ -x /usr/bin/logger ] && echo "$named_err" | /usr/bin/logger -pdaemon.error -tnamed;
    umount_chroot_conf
    exit 2;
  fi;
  echo
  if [ $RETVAL -eq 0 ]; then
    touch /var/lock/subsys/named;
  else
    umount_chroot_conf
    exit 7;
  fi
  return 0;
}
 
stop() {
  [ "$EUID" != "0" ] && exit 4
 
  # Stop daemons.
  echo -n $"Stopping named: "
  check_pidfile
  [ -x /usr/sbin/rndc ] && /usr/sbin/rndc stop >/dev/null 2>&1;
  RETVAL=$?
  # was rndc successful?
  [ "$RETVAL" -eq 0 ] ||
    killproc -p "$ROOTDIR$PIDFILE" "$named" -TERM >/dev/null 2>&1
 
  timeout=0
  RETVAL=0
  while pidofnamed &>/dev/null; do
    if [ $timeout -ge $NAMED_SHUTDOWN_TIMEOUT ]; then
      RETVAL=1
      break
    else
      sleep 2 && echo -n "."
      timeout=$((timeout+2))
    fi;
  done
 
  umount_chroot_conf
 
  # remove pid files
  if [ $RETVAL -eq 0 ]; then
    rm -f /var/lock/subsys/named
    rm -f /var/run/{named,named-sdb}.pid
  fi;
 
  if [ $RETVAL -eq 0 ]; then
    success
  else
    failure
    RETVAL=1
  fi;
  echo
  return $RETVAL
}
 
 
rhstatus() {
  [ -x /usr/sbin/rndc ] && /usr/sbin/rndc status;
  check_pidfile
  status -p "$ROOTDIR$PIDFILE" -l named /usr/sbin/"$named";
  return $?
}
 
restart() {
        stop
        start
}
 
reload() {
        [ "$EUID" != "0" ] && exit
 
        echo -n $"Reloading "$named": "
        check_pidfile
        p=`pidofnamed`
        RETVAL=$?
        if [ "$RETVAL" -eq 0 ]; then
            /usr/sbin/rndc reload >/dev/null 2>&1 || /bin/kill -HUP $p;
            RETVAL=$?
        fi
        [ "$RETVAL" -eq 0 ] && success $"$named reload" || failure $"$named reload"
        echo
        return $RETVAL
}
 
checkconfig() {
        ckcf_options='-z';
        if [ -n "${ROOTDIR}" -a "x${ROOTDIR}" != "x/" ]; then
                ckcf_options="$ckcf_options -t ${ROOTDIR}";
                if ! [ -e "$ROOTDIR/$PIDFILE" ]; then
                        mount_chroot_conf
                fi
        fi;
        if [ -x /usr/sbin/named-checkconf ] && [ -x /usr/sbin/named-checkzone ] && /usr/sbin/named-checkconf $ckcf_options ${named_conf} ; then
                RETVAL=0
        else
                RETVAL=1
        fi
        if ! [ -e "$ROOTDIR/$PIDFILE" ]; then
                umount_chroot_conf
        fi
        return $RETVAL
}
 
# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        status)
                rhstatus;
                RETVAL=$?
                ;;
        restart)
                restart
                ;;
        condrestart|try-restart)
                if [ -e /var/lock/subsys/named ]; then restart; fi
                ;;
        reload)
                reload
                ;;
        force-reload)
                if ! reload; then restart; fi
                ;;
        checkconfig|configtest|check|test)
                checkconfig
                RETVAL=$?
                ;;
        *)
                echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
                [ "x$1" = "x" ] && exit 0
                exit 2
esac
exit $RETVAL

相关文章

精彩推荐