• BIND简易教程(0):在Ubuntu下源码安装BIND(其实跟前面的教程没太大关系)


    之前介绍过BIND的基本使用啦。关于BIND的入门级使用方法见:http://www.cnblogs.com/anpengapple/p/5877661.html简易教程系列,本篇只讲BIND安装。

    本来源码安装不想介绍了,但是最近重装的时候,还是爆出来好多问题。唉,apt方式装习惯了,生成配置文件的脚本都是按照apt的目录和用户来的,源码方式有些坑。所以,还是介绍一下吧(有些部分是直接照前面扒下来的)。

    首先,因为需要开启DNSSec,所以必须要安装openssl。如果不需要装,或者已经安装好了openssl,可以跳过这步。

    ****************安装openssl的分割线****************

    openssl尽量使用源码安装,先从官网(https://www.openssl.org/)下载到最新稳定版(我的是1.0.2h,没用最新版,应该没什么区别),然后,跟通常的软件稍微有点不一样。

    tar zxvf openssl-1.0.2h.tar.gz
    cd openssl-1.0.2h
    sudo ./config --prefix=/usr/local    (注意这里是config而不是configure)
    sudo make depend    (注意这里必须先depend)
    sudo make
    sudo make install

    因为少写了一句depend坑了我一个礼拜。。。openssl装上没有报错,但是在安装BIND的时候./configure就死活过不去了,报这么个错:

    checking for OpenSSL library... using OpenSSL from /usr/local/lib and /usr/local/include
    checking whether linking with OpenSSL works... no
    configure: error: Could not run test program using OpenSSL from
    /usr/local/lib and /usr/local/include.
    Please check the argument to --with-openssl and your
    shared library configuration (e.g., LD_LIBRARY_PATH).

    找这个破玩意我都快哭了。后来还是自己无限次安装openssl,然后在一个角落里看到的一句make depend,拯救了我一颗破碎的心。

    **********************************
    *
    * 跳过openssl安装的同学从这里开始看
    *
    **********************************

    好,接下来进入正题BIND的安装,还是使用源码。到官网(https://www.isc.org/downloads/)下载最新的稳定版本BIND(我现在用的是9.10.4-P3,因为之前的P1和P2版本最近爆出来一个严重的漏洞)。然后

    tar zxvf bind-9.10.4-P2.tar.gz
    cd bind-9.10.4-P2
    sudo ./configure --sysconfdir=/etc/bind --with-libtool
    sudo make
    sudo make install

    configure的参数视自己具体情况而定。主要有这么几个:

    • --prefix=/usr/local:named的默认位置就是这里,可以不写,如果放其他目录下要写。
    • --sysconfdir:我习惯把所有配置文件放在/etc/bind下面,就加上--sysconfdir,如果不加的话,name.conf默认在/etc下面,很乱。
    • --with-libtool:据说加上之后能让编译出来的文件是.so而非.a,占用空间较小。不过我加上这个参数之后make编译会报错,我对这种编译方式不是很熟,所以就放弃了。
    • --enable-threads:很多网页上介绍了这个参数,其实没必要,带这个参数是让BIND以多线程方式启动,默认线程数等于CPU核心数。现在这个参数是默认的。
    • --without-openssl:如果不想要安装openssl,需要使用这个参数。

    装完之后,为了启动方便和安全性(其实就是为了启动方便),我们最好给BIND建立用户,然后弄个启动脚本。
    建立bind用户:

    groupadd bind
    useradd -g bind -d /usr/local/sbin bind

    注意这里的-d是用户主目录。我们这里是默认安装的BIND,named和rndc都是安装在/usr/local/sbin中的。包括我后面写的自动启动脚本中,有很多地方写到这个目录,如果指定了别的目录,或者以后BIND默认安装到其他目录了,也需要修改(话说,apt方式是默认装在/usr/sbin中,从别的地方拿来的启动脚本不能用我也是各种尴尬,后来各种改)。

    好了,最后是BIND的启动脚本:

    下面这个脚本放在 /etc/init.d/bind9:

    #!/bin/sh -e
    
    ### BEGIN INIT INFO
    # Provides:          bind9
    # Required-Start:    $remote_fs
    # Required-Stop:     $remote_fs
    # Should-Start:      $network $syslog
    # Should-Stop:       $network $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Start and stop bind9
    # Description:       bind9 is a Domain Name Server (DNS)
    #        which translates ip addresses to and from internet names
    ### END INIT INFO
    
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    
    # for a chrooted server: "-u bind -t /var/lib/named"
    # Don't modify this line, change or create /etc/default/bind9.
    OPTIONS=""
    RESOLVCONF=no
    
    test -f /etc/default/bind9 && . /etc/default/bind9
    
    test -x /usr/local/sbin/rndc || exit 0
    
    . /lib/lsb/init-functions
    PIDFILE=/var/run/named/named.pid
    
    check_network() {
        if [ -x /usr/bin/uname ] && [ "X$(/usr/bin/uname -o)" = XSolaris ]; then
        IFCONFIG_OPTS="-au"
        else
        IFCONFIG_OPTS=""
        fi
        if [ -z "$(/sbin/ifconfig $IFCONFIG_OPTS)" ]; then
           #log_action_msg "No networks configured."
           return 1
        fi
        return 0
    }
    
    case "$1" in
        start)
        log_daemon_msg "Starting domain name service..." "bind9"
    
        modprobe capability >/dev/null 2>&1 || true
    
        # dirs under /var/run can go away on reboots.
        mkdir -p /var/run/named
        chmod 775 /var/run/named
        chown root:bind /var/run/named >/dev/null 2>&1 || true
    
        if [ ! -x /usr/local/sbin/named ]; then
            log_action_msg "named binary missing - not starting"
            log_end_msg 1
        fi
    
        if ! check_network; then
            log_action_msg "no networks configured"
            log_end_msg 1
        fi
    
        if start-stop-daemon --start --oknodo --quiet --exec /usr/local/sbin/named 
            --pidfile ${PIDFILE} -- $OPTIONS; then
            if [ "X$RESOLVCONF" != "Xno" ] && [ -x /sbin/resolvconf ] ; then
            echo "nameserver 127.0.0.1" | /sbin/resolvconf -a lo.named
            fi
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
    
        stop)
        log_daemon_msg "Stopping domain name service..." "bind9"
        if ! check_network; then
            log_action_msg "no networks configured"
            log_end_msg 1
        fi
    
        if [ "X$RESOLVCONF" != "Xno" ] && [ -x /sbin/resolvconf ] ; then
            /sbin/resolvconf -d lo.named
        fi
        pid=$(/usr/local/sbin/rndc stop -p | awk '/^pid:/ {print $2}') || true
        if [ -z "$pid" ]; then      # no pid found, so either not running, or error
            pid=$(pgrep -f ^/usr/local/sbin/named) || true
            start-stop-daemon --stop --oknodo --quiet --exec /usr/local/sbin/named 
                --pidfile ${PIDFILE} -- $OPTIONS
        fi
        if [ -n "$pid" ]; then
            sig=0
            n=1
            while kill -$sig $pid 2>/dev/null; do
            if [ $n -eq 1 ]; then
                echo "waiting for pid $pid to die"
            fi
            if [ $n -eq 11 ]; then
                echo "giving up on pid $pid with kill -0; trying -9"
                sig=9
            fi
            if [ $n -gt 20 ]; then
                echo "giving up on pid $pid"
                break
            fi
            n=$(($n+1))
            sleep 1
            done
        fi
        log_end_msg 0
        ;;
    
        reload|force-reload)
        log_daemon_msg "Reloading domain name service..." "bind9"
        if ! check_network; then
            log_action_msg "no networks configured"
            log_end_msg 1
        fi
    
        /usr/local/sbin/rndc reload >/dev/null && log_end_msg 0 || log_end_msg 1
        ;;
    
        restart)
        if ! check_network; then
            log_action_msg "no networks configured"
            exit 1
        fi
    
        $0 stop
        $0 start
        ;;
        
        status)
            ret=0
        status_of_proc -p ${PIDFILE} /usr/local/sbin/named bind9 2>/dev/null || ret=$?
        exit $ret
        ;;
    
        *)
        log_action_msg "Usage: /etc/init.d/bind9 {start|stop|reload|restart|force-reload|status}"
        exit 1
        ;;
    esac
    
    exit 0

    注意里面的named和rndc的目录位置!

    下面这个脚本放在 /etc/default/bind9:

    # run resolvconf?
    RESOLVCONF=no
    
    # startup options for the server
    OPTIONS="-u bind"

    如果之前没有建立bind用户,只想用root启动,那么这里最后一行写成

    OPTIONS=""

    启动脚本写好之后,最后用

    sudo chmod 755 /etc/init.d/bind9

    更改权限,然后就可以用

    sudo service bind9 start|stop|restart|status

    来控制bind了,而不用每次named启动再kill进程。

    好啦,就酱。

  • 相关阅读:
    【leetcode】590. N-ary Tree Postorder Traversal
    【leetcode】589. N-ary Tree Preorder Traversal
    【leetcode】402. Remove K Digits
    【leetcode】42. Trapping Rain Water
    【leetcode】32. Longest Valid Parentheses
    【leetcode】870. Advantage Shuffle
    【leetcode】22. Generate Parentheses
    BEC translation exercise 2
    New Concept English three (50)
    BEC translation exercise 1
  • 原文地址:https://www.cnblogs.com/anpengapple/p/5942659.html
Copyright © 2020-2023  润新知