Deprecated: Return type of Requests_Cookie_Jar::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 63

Deprecated: Return type of Requests_Cookie_Jar::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 73

Deprecated: Return type of Requests_Cookie_Jar::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 89

Deprecated: Return type of Requests_Cookie_Jar::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 102

Deprecated: Return type of Requests_Cookie_Jar::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Cookie/Jar.php on line 111

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 40

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 51

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 68

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 82

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 91

Deprecated: Return type of AIOSEO\Plugin\Common\Models\Model::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 409

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174
CentOS7で内向き専用DNSサーバを立てる | reyfox blog

reyfox blog

reyfox tech info

CentOS DHCP DNS Linux ネットワーク

CentOS7で内向き専用DNSサーバを立てる

投稿日:

内向きDNSとは、LAN内でだけドメイン名を解決できるDNSサーバを言います。
今回は、LAN内だけでドメイン名を解決するDNSサーバを立て、デフォルトのDNSとして動作するように設定します。
なお、内向き以外のDNS(例: google.com等)は、ルータのDNSもしくはGooglePublicDNSへ問い合わせるよう設定します。

今回使用する内向き用のドメインは「reyfox.lo」とします。
なお、.localを使用しようと思いましたが.localは予約済みのドメインのようなので、.loとしています。

以下の設定例では、192.168.xxx.としています。xxxはお使いのネットワークに置き換えてください。

インストール

yum で bind 関係をインストールします。


yum install bind bind-chroot bind-utils

設定

chrootにて動作させるので下記のコマンドを一度だけ実行しておきます。


/usr/libexec/setup-named-chroot.sh /var/named/chroot on

コンフィグファイルの編集


vi /etc/named.conf

/etc/named.conf


options {
	#listen-on port 53 { 127.0.0.1;192.168.xxx.2; };
	#listen-on-v6 port 53 { ::1; };
	directory 	"/var/named";
	dump-file 	"/var/named/data/cache_dump.db";
	statistics-file "/var/named/data/named_stats.txt";
	memstatistics-file "/var/named/data/named_mem_stats.txt";
	recursing-file  "/var/named/data/named.recursing";
	secroots-file   "/var/named/data/named.secroots";
	allow-transfer { none; };
	allow-query     { localhost; localnets; };
	allow-recursion { localhost; localnets; };
	allow-query-cache { localhost; localnets; };
	/*
	 - If you are building an AUTHORITATIVE DNS server, do NOT enable recursion.
	 - If you are building a RECURSIVE (caching) DNS server, you need to enable
	   recursion.
	 - If your recursive DNS server has a public IP address, you MUST enable access
	   control to limit queries to your legitimate users. Failing to do so will
	   cause your server to become part of large scale DNS amplification
	   attacks. Implementing BCP38 within your network would greatly
	   reduce such attack surface
	*/
	recursion yes;

	dnssec-enable yes;
	dnssec-validation yes;

	/* Path to ISC DLV key */
	bindkeys-file "/etc/named.iscdlv.key";

	managed-keys-directory "/var/named/dynamic";

	pid-file "/run/named/named.pid";
	session-keyfile "/run/named/session.key";
	forwarders{
            192.168.xxx.1;
	    8.8.8.8;
   	};
	forward only;
	version "unknown";
};

logging {
        channel default_debug {
                file "data/default.log" versions 5 size 10M;
                severity dynamic;
                print-time yes;
                print-severity yes;
                print-category yes;
        };
        category lame-servers { null; };
};

view "internal" {
        match-clients { localnets; };
        match-destinations { localnets; };
	recursion yes;
zone "." IN {
	type hint;
	file "named.ca";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

include "/etc/named/named.reyfox.lo.zone";

};

allow-query { localhost; localnets; };
allow-recursion { localhost; localnets; };
allow-query-cache { localhost; localnets; };
このあたりの設定は、ローカルからのみクエリ受付や再起問い合わせを有効化しています。

recursion yes;
これは再起問い合わせをOnにします。

dnssec-enable yes;
dnssec-validation yes;
これはDNSSECという機能を有効にするかの設定です。
yesにしてもnoにしてもあまり変わらなかったので、とりあえずyesにしてあります。

forwarders セクションでこのDNSサーバに無いときにフォワードさせるアドレスを指定します。
今回は、プライマリDNSをルータのDNSへ、セカンダリDNSをGooglePublicDNSと設定しました。
ここで、プライマリDNSもGooglePublicDNSに設定するのもありかとは思いましたが、SERV FAIL(DNSが引けない)が多発したのでルータのものに変更しています。

include “/etc/named/named.reyfox.lo.zone”;
ここで、reyfox.lo のゾーンを定義したファイルを読み込みます。

ゾーンファイルの編集

ゾーンファイルは /etc/named/named.reyfox.lo.zone になります。


zone "reyfox.lo" {
        type master;
        allow-update { localhost; localnets; };
	file "reyfox.lo.db";
};
# 以下は逆引きが必要な場合のみ
zone "xxx.168.192.in-addr.arpa" {
        type master;
        file "xxx.168.192.in-addr.arpa.db";
        allow-update { localhost; localnets; };
};

逆引きとは、IPアドレスから名前を引くときに設定します。
通常の名前解決は nas.reyfox.lo → 192.168.xxx.10 であり、これは正引きと呼ばれています。
この逆が逆引きで設定すると 192.168.xxx.10 を名前解決したときにドメイン名が表示されます。

正引き、逆引きの方法

Macだとdigコマンドが便利です。
Windowsでもnslookup を使用することが出来ます。
digコマンドで@[IPアドレス] を付与すると、そのサーバに問い合わせるようになります。
現時点ではまだDNSサーバが起動してないので、起動後にテストしてみてください。

正引き方法
>dig nas.reyfox.lo @192.168.xxx.2

;; ANSWER SECTION:
nas.reyfox.lo. 86400 IN A 192.168.xxx.11

逆引き方法
>dig -x 192.168.xxx.10 @192.168.xxx.2

;; ANSWER SECTION:
11.xxx.168.192.in-addr.arpa. 86400 IN PTR nas.reyfox.lo.

ゾーンファイルの編集(正引き)

chroot しているので、実際のゾーンファイルの場所は以下になります。


vi /var/named/reyfox.lo.db

$TTL    86400
@       IN      SOA     reyfox.lo.     root.reyfox.lo. (
                                      2019011506 ; Serial
                                      28800      ; Refresh
                                      14400      ; Retry
                                      3600000    ; Expire
                                      86400 )    ; Minimum
        IN NS    reyfox.lo.
        IN MX 10 reyfox.lo.
@       IN A     192.168.xxx.2
*       IN A     192.168.xxx.2
nuc     IN A     192.168.xxx.2
esxi    IN A     192.168.xxx.3
centos  IN A     192.168.xxx.4
nas     IN A     192.168.xxx.11

レンタルDNSなどでDNSをちょっといじったことがある人ならわかるかと思いますが、各サブドメインに対応したIPアドレスを付与していきます。
*はエイリアスとなり、定義されていない場合すべてこのレコードで解決するという意味になります。
今回は固定IPに割り当てているホストを設定しました。
例、 nuc.reyfox.lo なら 192.168.xxx.2 を返すように設定。
アップデート時は、Serial 部分の数値を上げるようにしましょう。
Serial は通常西暦月日時間が一般的に用いられています。

ゾーンファイルの編集(逆引き)

これもまた chroot しているので、実際のゾーンファイルの場所は以下になります。
逆引きが必要ではない場合この設定は不要です。


vi /var/named/xxx.168.192.in-addr.arpa.db

$TTL    86400
@       IN      SOA     reyfox.lo.     root.reyfox.lo. (
                                      2019011506 ; Serial
                                      28800      ; Refresh
                                      14400      ; Retry
                                      3600000    ; Expire
                                      86400 )    ; Minimum
      IN NS   reyfox.lo.
2     IN PTR  reyfox.lo.
2     IN PTR nuc.reyfox.lo.
3     IN PTR esxi.reyfox.lo.
4     IN PTR centos.reyfox.lo.
11    IN PTR nas.reyfox.lo.

逆引きしたいIPアドレスの末尾の数字を最初に持ってきてそれに対応するホスト名を記入します。
ホスト名の最後に . が必要なので忘れないようにつけましょう。

ルートゾーンのアップデート(任意)

DNSのルートゾーンは世界に13台しかありませんがその情報がアップデートされる場合もあります。
そのときに対応するため、月毎で実行する cron を設定します。


vi /etc/cron.monthly/named.root_update

#!/bin/bash

new=`mktemp`
errors=`mktemp`

dig . ns @198.41.0.4 +bufsize=1024 > $new 2> $errors

if [ $? -eq 0 ]; then
    sort_new=`mktemp`
    sort_old=`mktemp`
    diff_out=`mktemp`
    sort $new > $sort_new
    sort /var/named/chroot/var/named/named.ca > $sort_old
    diff --ignore-matching-lines=^\; $sort_new $sort_old > $diff_out
    if [ $? -ne 0 ]; then
        (
         echo '-------------------- old named.root --------------------'
         cat /var/named/chroot/var/named/named.ca
         echo
         echo '-------------------- new named.root --------------------'
         cat $new
         echo '---------------------- difference ----------------------'
         cat $diff_out
        ) | mail -s 'named.root updated' root
        cp -f $new /var/named/chroot/var/named/named.ca
        chown named. /var/named/chroot/var/named/named.ca
        chmod 644 /var/named/chroot/var/named/named.ca
        which systemctl > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            systemctl restart named-chroot > /dev/null
        else
            /etc/rc.d/init.d/named restart > /dev/null
        fi
    fi
    rm -f $sort_new $sort_old $diff_out
else
    cat $errors | mail -s 'named.root update check error' root
fi
rm -f $new $errors

毎月1日に実行される cron で動く shell の内容となります。
差分やエラーがあれば root 宛にメールで通知されます。

忘れずに実行権限をつけておきましょう。


chmod 700 /etc/cron.monthly/named.root_update

設定ファイルのチェック


named-checkzone reyfox.lo /var/named/reyfox.lo.db
named-checkzone xxx.168.192.in-addr.arpa /var/named/xxx.168.192.in-addr.arpa.db

上記を実行して
OK
と出ればエラーはありません。

named の調整と起動

設定ファイルが出来たのでファイルのオーナ、グループ変更とbind の起動、自動起動の設定を行います。


chown named:named /var/named -R

systemctl start named-chroot.service
systemctl enable named-chroot.service

上記で設定は終わりです。
LAN内のLinuxとは別のマシンから、dig コマンドを実行して名前が引けることを確認してみてください。
例: dig nas.reyfox.lo @192.168.xxx.2

DHCPサーバからDNSサーバを通知させる

CentOS7にDHCPサーバをインストールしルータのDHCPサーバを停止

上記記事でDHCPサーバを設定していますが、
option domain-name-servers 192.168.xxx.2;
という部分で、DNSサーバを通知させています。(192.168.xxx.2は今回のDNSサーバを参照)
ルータによってはDNSサーバの通知を設定できるルータもありますが
ルータによりけりなので、DHCPサーバをLinuxに持たせそこからDNSサーバを通知する設定にしています。

これにより、LAN内にPCが接続された場合自動的にDHCPでローカルアドレスが発行され、DNSサーバもLinuxのDNSサーバを参照するようになります。

例: Macでの接続例

ルータは末尾1、DNSサーバは今回の末尾2、PCへの付与アドレスは末尾50となっています。

-CentOS, DHCP, DNS, Linux, ネットワーク

執筆者:

関連記事

CentOS7の初期設定

CentOS7 を Intel NUC にインストールしましたが今の所家庭内ネットワークからのみの想定で、外部からのアクセスはすべてルータでフィルタリングしているため、利便性のため下記の設定を行います …

CentOS7上でiperfを使いネットワークの速度を測定

iperf というソフトを使うと、ネットワークの速度を測定できます。主にLAN内の速度(スループット)を測るために使用します。 iperf のインストール yum install epel-relea …

CentOS7にDHCPサーバをインストールしルータのDHCPサーバを停止

現在販売されている市販のルータにはDHCPサーバという家庭内LANのIPを自動で払い出してくれる機能がだいたい標準で装備されています。ただ、製品によってはDHCPで払い出しているマシンに固定IPを割り …

PHPMyAdminの設置

PHPをインストールしましたが、DBを操作するのに便利なのがPHPMyAdminです。もちろんコマンドラインからも行えますがユーザの追加やユーザ専用のDBを作るとき何度もパスワードを入力したりするのが …

grafanaにCPU使用率及びメモリ使用率グラフを追加

grafana+prometheusでサーバ監視をしグラフィカルなダッシュボードを作る 上記記事でgrafana+prometheusでダッシュボードを作成しました。今回はここにCPU使用率とメモリ使 …


Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Utils/Database.php on line 535

Deprecated: json_decode(): Passing null to parameter #1 ($json) of type string is deprecated in /var/www/blog.reyfox.com/htdocs/wp/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/Model.php on line 174