Ubuntu server DNSサーバの構築

はじめに

今回は、Ubuntu server 20.04 を使用し、DNSサーバを構築していきます。
外部向けには作らず、内部向けのサーバとなります。

要件

VirtualBox

  • OS:Ubuntu server 20.04
  • CPU:1
  • メモリ:1024MB
  • HDD:10GB(可変サイズ)
  • ネットワーク:ブリッジアダプタ

DNSサーバ

構築

今回は、DNSサーバの構築のためVirtualBox関連は省く。

BIND とは

BIND(Berkeley Internet Name Domain) とは、Unix / Linux系のサーバにおいてDNSサーバを構築するためのデファクトスタンダードになりつつあるDNSサーバソフトウェア。
現在のバージョンは、バージョン9(BIND9)。

インストール

apt install -y bind9 bind9utils

ディレクトリの移動

cd /etc/bind/

設定ファイルの編集

BIND9 が参照するファイルを定義する。

vim named.conf

-------------以下、confファイル-----------------

// This is the primary configuration file for the BIND DNS server named.
//
// Please read /usr/share/doc/bind9/README.Debian.gz for information on the
// structure of BIND configuration files in Debian, *BEFORE* you customize
// this configuration file.
//
// If you are just adding zones, please do that in /etc/bind/named.conf.local

include "/etc/bind/named.conf.options";
include "/etc/bind/named.conf.local";
include "/etc/bind/named.conf.default-zones";

// ゾーンファイルを追加
include "/etc/bind/named.conf.zones";

オプションファイルの編集

問い合わせを許可するネットワーク、名前解決問い合わせのフォワード先、再起問い合わせの許可等の設定をする。

vim named.conf.options

-------------以下、optionファイル-----------------

// ACL(Access Contorol List)で、ネットワークの範囲を定義する。
acl internal-network {
        192.168.1.0/24;
};

options {
        directory "/var/cache/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 {
                8.8.8.8;
        };

       // 問い合わせを受け付けるネットワークおよびホスト
        allow-query {
                localhost;
                internal-network;
        };

       // 再起問い合わせを許可(キャッシュDNSサーバ(リゾルバ)として動作させる)。無効にする場合は、「none」。
        recursion yes;

        //========================================================================
        // If BIND logs error messages about the root key being expired,
        // you will need to update your keys.  See https://www.isc.org/bind-keys
        //========================================================================
        dnssec-validation auto;

        listen-on-v6 { any; };
};

ドメイン定義ファイルの編集

vim named.conf.zone

-------------以下、ドメイン定義-----------------

zone "infra-test.work" IN {       // 「" "」で指定したものがドメインになる。
        type    master;
        file    "/etc/bind/infra-test.work.lan";
};

zone "1.168.192.in-addr.arpa" IN {
        type    master;
        file    "/etc/bind/db.1.168.192";
};

ゾーンファイルの編集

vim infra-test.work.lan

-------------以下、正引きのゾーンファイル-----------------

$TTL 86400
@       IN      SOA     dns.infra-test.work. root.infra-test.work. (
        2021013001      ;Serial
        3600            ;Refresh
        1800            ;Retry
        604800          ;Expire
        865400          ;Minimum TTL
)

        IN      NS      dns.infra-test.work.
dns     IN      A       192.168.1.254
www     IN      A       192.168.1.101


vim db.1.168.192

-------------以下、逆引きのゾーンファイル-----------------

$TTL 86400
@       IN      SOA     dns.infra-test.work. root.infra-test.work. (
        2021013001      ;Serial
        3600            ;Refresh
        1800            ;Retry
        604800          ;Expire
        86400           ;Minimum TTL
)

        IN      NS      dns.infra-test.work.
254     IN      PTR     dns.infra-test.work.
101     IN      PTR     www.infra-test.work.

サービスの再起動

systemctl restart named
systemctl enable named

追記(2021/04/19)

今回は、「内部向けDNSサーバ」として構築しているが、外部向けとして構築する場合は「キャッシュDNSサーバ(リゾルバ)」と「コンテンツDNSサーバ(権威DNSサーバ)」は2つのサーバに分ける必要がある。
外部向けDNSサーバを構築する場合は、オプションファイル「named.conf.option」の [recursion] [query] [forwarders] の設定を変更する。