以前介紹過BIND的基本使用啦。關於BIND的入門級使用方法見:http://www.cnblogs.com/anpengapple/p/5877661.html簡易教程系列,本篇只講BIND安裝。html
原本源碼安裝不想介紹了,可是最近重裝的時候,仍是爆出來好多問題。唉,apt方式裝習慣了,生成配置文件的腳本都是按照apt的目錄和用戶來的,源碼方式有些坑。因此,仍是介紹一下吧(有些部分是直接照前面扒下來的)。安全
首先,由於須要開啓DNSSec,因此必需要安裝openssl。若是不須要裝,或者已經安裝好了openssl,能夠跳過這步。多線程
****************安裝openssl的分割線****************app
openssl儘可能使用源碼安裝,先從官網(https://www.openssl.org/)下載到最新穩定版(個人是1.0.2h,沒用最新版,應該沒什麼區別),而後,跟一般的軟件稍微有點不同。dom
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就死活過不去了,報這麼個錯:ide
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,拯救了我一顆破碎的心。ui
**********************************
*
* 跳過openssl安裝的同窗從這裏開始看
*
**********************************this
好,接下來進入正題BIND的安裝,仍是使用源碼。到官網(https://www.isc.org/downloads/)下載最新的穩定版本BIND(我如今用的是9.10.4-P3,由於以前的P1和P2版本最近爆出來一個嚴重的漏洞)。而後spa
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的參數視本身具體狀況而定。主要有這麼幾個:線程
裝完以後,爲了啓動方便和安全性(其實就是爲了啓動方便),咱們最好給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進程。
好啦,就醬。