這當然又是老闆給的課題,要把 DNS Server 放到 VM 上,因為沒有在 Ubuntu 上安裝過 DNS Server,當然要記錄一下囉!

參考:SmallKen's BlogCommunity Ubuntu DocumentationRaining PacketsUbuntu Forums

這次用的作業系統為:Ubuntu 10.10 Server,Bind 版本為:9.7.1.dfsg.P2-2ubuntu0.2

Ubuntu10.10 Server安裝DNS Server

一開始,一定是安裝 bind9 套件囉!執行下列的指令來進行安裝:

$ sudo apt-get install bind9

BIND9Ubuntu 預設的設定檔位置是在:
/etc/bind

接下來就是編輯 named.conf 設定檔,因為是要轉到 VM 上,所以就偷懶去舊主機上複製貼上囉!不過發現 named.conf 會 include 幾個檔案:
/etc/bind/named.conf.options全域 options 的設定放在此
/etc/bind/named.conf.local
/etc/bind/named.default-zones像一些制式的 zone 就放在這裡!若 named.conf 內有重複,會出現錯誤訊息,服務也會起不來

就可以知道 Ubuntu 把 BIND 的設定檔分成好幾個,這樣就不會讓 named.conf 太長啦!

編輯 /etc/bind/named.options,加入下列幾行:

options {
        directory "/var/cache/bind";
        zone-statistics yes;
        statistics-file "/var/cache/bind/named.stats";
        managed-keys-directory "/etc/bind";

        // If there is a firewall between you and nameservers you want
        // to talk to, you may need to fix the firewall to allow multiple
        // ports to talk.  See http://www.kb.cert.org/vuls/id/800113

        // If your ISP provided one or more IP addresses for stable
        // nameservers, you probably want to use them as forwarders.
        // Uncomment the following block, and insert the addresses replacing
        // the all-0's placeholder.

        // forwarders {
        //      0.0.0.0;
        // };

        forwarders {
                168.95.1.1; 139.175.10.20; 203.133.1.6;
        };
        allow-query { any; };
        allow-transfer { none; };
        auth-nxdomain no;    # conform to RFC1035
        listen-on-v6 { any; };
};

加下列兩行,主要是為了給 Cacti 分析 BIND 用的:
zone-statistics yes;
statistics-file "/var/cache/bind/named.stats";

而加入下列這行,主要是不讓記錄檔出現錯誤訊息,還有些動作要做,後面會提:
managed-keys-directory "/etc/bind";

若要減輕 BIND 的載重,可以將非所轄網域查詢轉到其它 DNS Server 上,設定如下:
forwarders {
    168.95.1.1; 139.175.10.20; 203.133.1.6;
};

不允許全域轉送(Master 跟 Slave 之間的資料傳送),加入下列這一行:
allow-transfer { none; };

完成後便可以存檔離開。

依照 /etc/bind/named.conf 中所需的 zone file,就從舊主機上直接複製囉!哈!

啟動 BIND 後就可以查詢記錄囉!

不過,在 /var/log/syslog 上會看到錯誤的訊息,雖然無傷大雅,但還是希望不要出現:

managed-keys-zone ./IN: loading from master file managed-keys.bind failed: file not found
managed-keys-zone ./IN: loaded serial 0

1.在 /etc/bind/named.conf.options 加上 managed-keys-directory "/etc/bind";

2.在 /etc/bind 上建立檔案 managed-keys.bind

$ sudo touch /etc/bind/managed-keys.bind

在重新啟的 BIND 之後,就不會出現找不到檔案的錯誤訊息了。

接下來想要對 BIND 記錄檔作分類,我是建了一個設定檔,/etc/bind/named.conf.logging,在 /etc/bind/named.conf 中 include 進來。

編輯 /etc/bind/named.conf.logging,加入下列:

logging {
  channel default_log {
    file "/var/log/named/dns-default.log" versions 10 size 1m;
    severity info;
  };
  channel lamer_log {
    file "/var/log/named/dns-lamer.log" versions 3 size 1m;
    severity info;
    print-severity yes;
    print-time     yes;
    print-category yes;
  };
  channel query_log {
    file "/var/log/named/dns-query.log" versions 10 size 200m;
    print-time      yes;

    severity info;
  };
  channel security_log {
    file "/var/log/named/dns-security.log" versions 3 size 1m;
    severity info;
    print-severity yes;
    print-time     yes;
    print-category yes;
  };
category lame-servers { lamer_log; };
category security { security_log; };
category queries { query_log; };
category default { default_log; };
};

在做記錄檔分類這件事,可花了我好大的工夫去查問題,因為我預設放記錄檔的位置是在 /var/log 底下,但一啟動服務,記錄檔便會出現錯誤訊息,而記錄還是寫在 /var/log/syslog 中。

logging channel 'default_log' file '/var/log/dns-default.log': permission denied
logging channel 'lamer_log' file '/var/log/dns-lamer.log': permission denied
logging channel 'query_log' file '/var/log/dns-query.log': permission denied
logging channel 'security_log' file '/var/log/dns-security.log': permission denied
[ 5471.979439] type=1400 audit(1300692734.657:10): apparmor="DENIED" operation="mknod" parent=2596 profile="/usr/sbin/named" name="/var/log/dns-default.log" pid=2598 comm="named" requested_mask="c" denied_mask="c" fsuid=103 ouid=103
[ 5471.979524] type=1400 audit(1300692734.657:11): apparmor="DENIED" operation="mknod" parent=2596 profile="/usr/sbin/named" name="/var/log/dns-lamer.log" pid=2598 comm="named" requested_mask="c" denied_mask="c" fsuid=103 ouid=103
[ 5471.979603] type=1400 audit(1300692734.657:12): apparmor="DENIED" operation="mknod" parent=2596 profile="/usr/sbin/named" name="/var/log/dns-query.log" pid=2598 comm="named" requested_mask="c" denied_mask="c" fsuid=103 ouid=103
[ 5471.979682] type=1400 audit(1300692734.657:13): apparmor="DENIED" operation="mknod" parent=2596 profile="/usr/sbin/named" name="/var/log/dns-security.log" pid=2598 comm="named" requested_mask="c" denied_mask="c" fsuid=103 ouid=103
isc_log_open '/var/log/dns-default.log' failed: permission denied
type=1400 audit(1300692734.667:14): apparmor="DENIED" operation="mknod" parent=2596 profile="/usr/sbin/named" name="/var/log/dns-default.log" pid=2598 comm="named" requested_mask="c" denied_mask="c" fsuid=103 ouid=103
[ 5471.980816] type=1400 audit(1300692734.667:15): apparmor="DENIED" operation="mknod" parent=2596 profile="/usr/sbin/named" name="/var/log/dns-default.log" pid=2598 comm="named" requested_mask="c" denied_mask="c" fsuid=103 ouid=103
[ 5471.981086] type=1400 audit(1300692734.667:16): apparmor="DENIED" operation="mknod" parent=2596 profile="/usr/sbin/named" name="/var/log/dns-default.log" pid=2598 comm="named" requested_mask="c" denied_mask="c" fsuid=103 ouid=103

想說檔案都已經給所有人有寫的權限了,怎麼還是會發生這個問題呢?後來上網找了之後,才知道 apparmor 就是有啟動 SELinux 啦!是 Linux Kernel 2.6.36 有加入 AppArmor Security 這個這個新功能啦!

按照文章去檢視 /etc/apparmor.d/usr.sbin.named 這個檔案內容,才知道預設 /var/log/named 才有讀寫的權限。不改設定的話,就將記錄檔改放在 /var/log/named 底下囉!

# some people like to put logs in /var/log/named/ instead of having
# syslog do the heavy lifting.
/var/log/named/** rw,
/var/log/named/ rw,

所以,要建立 /var/log/named 目錄及給予相關的權限:

$ sudo mkdir /var/log/named
$ sudo chown bind /var/log/named

重新啟動 BIND,果然一試就成功啦!

20110321

arrow
arrow
    創作者介紹
    創作者 游蜂 的頭像
    游蜂

    暉獲無度的步烙閣

    游蜂 發表在 痞客邦 留言(0) 人氣()