■仮想マシン内にbind9を構築する
$ env LANG=C apt-cache show bind9 | grep Suggests | sort | uniq
Suggests: dnsutils, bind9-doc, resolvconf, ufw
$ env LANG=C aptitude show bind9 | grep Suggests
Suggests: dnsutils, bind9-doc, resolvconf, ufw
★インストール
$ sudo apt-get install bind9
$ whereis -b named
named: /usr/sbin/named
$ ls /usr/sbin/named*
/usr/sbin/named /usr/sbin/named-checkzone
/usr/sbin/named-checkconf /usr/sbin/named-compilezone
■設定ファイルの確認
1.named.conf.optionsを読み込む
2.named.confの初期設定を読み込む
3.named.conf.localを読み込む
※named.confは変更しない。
$ grep include /etc/bind/named.conf
include "/etc/bind/named.conf.options";
include "/etc/bind/named.conf.local";
■オプションを設定する
※重要:再帰的な問い合わせを行わない。
※上位DNSへの問い合わせの設定も行わない。
※IPv6はリッスンしない。
★仮想NAT(/dev/vmnet8)の設定
vmguest(dns)、vmhost、vmrouterの3つのローカル専用DNSを設定する。
$ sudo grep -v "//\|^\$" /etc/bind/named.conf.options
options {
directory "/var/cache/bind";
auth-nxdomain no;
listen-on { 127.0.0.1;192.168.72.0/24; };
recursion no;
};
★稼動状況の確認
$ ps -ef | grep "bind\|named" | grep -v grep
bind 3204 1 0 20:17 ? 00:00:00 /usr/sbin/named -u bind
★ログの確認
※namedのログのうち、startingで始まるログの開始箇所を確認する
$ sudo grep named /var/log/syslog | grep starting
Nov 20 21:31:42 vm-amddebian named[4722]: starting BIND 9.6-ESV-R4 -u bind
Nov 20 21:33:00 vm-amddebian named[4769]: starting BIND 9.6-ESV-R4 -u bind
★時刻で引きなおす
$ sudo grep named /var/log/syslog | grep "^Nov 20 21:3[34]:" | grep -v zone | awk -F\] '{print $2}'
received control channel command 'stop -p'
shutting down: flushing changes
stopping command channel on 127.0.0.1#953
stopping command channel on ::1#953
no longer listening on 127.0.0.1#53
no longer listening on 192.168.72.188#53
exiting
starting BIND 9.6-ESV-R4 -u bind
built with '--prefix=/usr' '--build=x86_64-linux-gnu' '--host=x86_64-linux-gnu' '--mandir=/usr/share/man'
'--infodir=/usr/share/info' '--sysconfdir=/etc/bind' '--localstatedir=/var/run/bind' '--enable-threads'
'--enable-largefile' '--with-libtool' '--enable-shared' '--enable-static' '--with-openssl=/usr' '--with-gssapi=/usr'
'--with-gnu-ld' '--with-dlz-postgres=no' '--with-dlz-mysql=no' '--with-dlz-bdb=yes' '--with-dlz-filesystem=yes'
'--with-dlz-ldap=yes' '--with-dlz-stub=yes' '--enable-ipv6' 'build_alias=x86_64-linux-gnu' 'host_alias=x86_64-linux-gnu'
'CFLAGS=-fno-strict-aliasing -DDIG_SIGCHASE -DNS_RUN_PID_DIR=0 -O2' 'LDFLAGS=' 'CPPFLAGS=' 'CXXFLAGS=-g -O2'
'FFLAGS=-g -O2'
adjusted limit on open files from 1024 to 1048576
found 1 CPU, using 1 worker thread
using up to 4096 sockets
loading configuration from '/etc/bind/named.conf'
using default UDP/IPv4 port range: [1024, 65535
: using default UDP/IPv6 port range: [1024, 65535
: listening on IPv4 interface lo, 127.0.0.1
: listening on IPv4 interface eth0, 192.168.72.188
: command channel listening on 127.0.0.1
: command channel listening on ::1#953
: running
★ユーザがbindだから、root権限は変?
$ sudo ls -ld /var/cache/bind/
drwxrwxr-x 2 root bind 4096 2011-07-06 01:27 /var/cache/bind/
$ id bind
uid=107(bind) gid=112(bind) 所属グループ=112(bind)
$ grep root /etc/init.d/bind9
chown root:bind /var/run/bind/run >/dev/null 2>&1 || true
⇒chroot環境の場合は変更が必要。
★待ちうけポートの確認
※実態は仮想NATです(/dev/vmnet8)
$ netstat -an | grep :53
tcp 0 0 192.168.72.188:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN
udp 0 0 192.168.72.188:53 0.0.0.0:*
udp 0 0 127.0.0.1:53 0.0.0.0:*
★ポート953はrndc用ですが、今回は使いません。
$ netstat -an | grep :953
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN
tcp6 0 0 ::1:953 :::* LISTEN
★ルーティングを確認します。
※基本的に別のセグメントからの通信も仮想ホストのdebianのIPを通ります。
$ env LANG=C netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.72.0 * 255.255.255.0 U 0 0 0 eth0
default 192.168.72.2 0.0.0.0 UG 0 0 0 eth0
★nxdomain noはこのまま。
$ man named.conf
---
auth-nxdomain
これが yes の場合、 AA ビットは、常に NXDOMAIN の応答にセットされます。
たとえサーバが実際には信頼できるものではなくてもです。デフォルトでは、
yes になっています。古くからあるソフトウェアが嫌うので、自分のしている
ことに確信が持てないでいるのであれば、 auth-nxdomain をオフにしてはいけ
ません。
---
★外向きのDNSサーバに問い合わせると引ける
$ nslookup google.co.jp 192.168.1.254
Server: 192.168.1.254
Address: 192.168.1.254
Non-authoritative answer:
Name: google.co.jp
Address: 72.14.203.147
Name: google.co.jp
Address: 72.14.203.105
Name: google.co.jp
Address: 72.14.203.103
Name: google.co.jp
Address: 72.14.203.104
Name: google.co.jp
Address: 72.14.203.106
Name: google.co.jp
Address: 72.14.203.99
★「example.com」や「example.org」の実在を確認(同一IPを指す)
※example.jpは見つからないようです。
$ nslookup example.com 192.168.1.254
Server: 192.168.1.254
Address: 192.168.1.254
Non-authoritative answer:
Name: example.com
Address: 192.0.43.10
★再帰問い合わせが出来ない事を確認する
※サーバを指定しない場合は「/etc/resolv.conf」のnameserverの設定に依存する
※「recursion no」でもnameserverの検索が外向きのDNSを参照していると意味が無い。
$ nslookup google.co.jp
Server: 127.0.0.1
Address: 127.0.0.1
** server can't find google.co.jp: NXDOMAIN
★テスト中はサーバを指定する
$ nslookup google.co.jp 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
** server can't find google.co.jp: NXDOMAIN
$ nslookup google.co.jp 192.168.72.188
Server: 192.168.72.188
Address: 192.168.72.188#53
** server can't find google.co.jp: NXDOMAIN
★resolv.confの設定例
$ cat /etc/resolv.conf
domain example.com
search example.com dns.example.com
nameserver 127.0.0.1
■named.conf.localを設定する
※正引きと逆引きのファイル参照の設定
1.正引きをファイル参照する
2.逆引きをファイル参照する
3.上記で解決しない場合は、RFC1918に従う
$ sudo grep -v "//\|^\$" /etc/bind/named.conf.local
zone "example.com" {
type master;
file "/etc/bind/db.example.com";
};
zone "72.168.192.in-addr.arpa" {
type master;
file "/etc/bind/72.168.192.in-addr.arpa";
};
include "/etc/bind/zones.rfc1918";
★include文は以下の参照を行う為、最後に記述する
$ grep 168.192 /etc/bind/zones.rfc1918
zone "168.192.in-addr.arpa" { type master; file "/etc/bind/db.empty"; };
■正引きの設定
※TTLは86400が一般的
※シリアルは設定日付と2桁の変更回数
$ cat /etc/bind/db.example.com
$TTL 3600
@ IN SOA example.com. labunix.dns.example.com. (
2011111907 ; Serial
3600 ; Refresh
900 ; Retry
3600000 ; Expire
3600 ); ; Minimum
@ IN NS example.com.
@ IN NS dns.example.com.
@ IN A 192.168.72.188
dns IN A 192.168.72.188
;
vmhost IN A 192.168.72.1
vmrouter IN A 192.168.72.2
★「.」の有無によって結果が異なる点に注意
※「-norecurse」で明示的に再帰的問い合わせを禁止する
※またサーバを指定し、resolv.confに影響されないようにする
※この場合は、-querytypeでも-typeでも結果は変わらない
$ nslookup -norecurse -querytype=NS example.com 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
example.com nameserver = example.com.
example.com nameserver = dns.example.com.
$ nslookup -norecurse -querytype=A example.com 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: example.com
Address: 192.168.72.188
$ nslookup -norecurse -querytype=A dns.example.com 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: dns.example.com
Address: 192.168.72.188
$ nslookup -norecurse -querytype=A localhost 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: localhost
Address: 127.0.0.1
$ nslookup -norecurse -type=A vmhost 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: vmhost.example.com
Address: 192.168.72.1
$ nslookup -norecurse -querytype=A vmrouter 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: vmrouter.example.com
Address: 192.168.72.2
■逆引きの設定
$ cat /etc/bind/72.168.192.in-addr-arpa
$TTL 3600
@ IN SOA example.com. labunix.dns.example.com. (
2011111906 ; Serial
3600 ; Refresh
900 ; Retry
3600000 ; Expire
3600 ); ; Minimum
IN NS example.com.
IN NS dns.example.com.
;
188 IN PTR example.com.
188 IN PTR dns.example.com.
1 IN PTR vmhost.example.com.
2 IN PTR vmrouter.example.com.
;
$ nslookup -norecurse -querytype=PTR 192.168.72.2 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
2.72.168.192.in-addr.arpa name = vmrouter.example.com.
$ nslookup -norecurse -querytype=PTR 192.168.72.1 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
1.72.168.192.in-addr.arpa name = vmhost.example.com.
$ nslookup -norecurse -querytype=PTR 192.168.72.188 127.0.0.1
Server: 127.0.0.1
Address: 127.0.0.1#53
188.72.168.192.in-addr.arpa name = dns.example.com.
188.72.168.192.in-addr.arpa name = example.com.
⇒すべての設定の確認が取れたら完了です。
オプション無しでexample.comを問い合わせ、仮想マシン内のDNSから応答があればOKです。
※今回はホストOS側のiptablesで制限、ログを出すようにした他、
tcpdumpで監視しながら作業しました。
「ただ単純に動くだけ」の設定なので、外に公開する事の無い様にご注意下さい。