2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

軽量高速な権威 DNS サーバー Knot DNS を構築してみた

Last updated at Posted at 2025-05-27

一般的な LAN 内で使用する権威サーバーとしての構築例です。

環境

サーバー IP アドレス: 192.168.182.10
AlmaLinux 9.5
Knot DNS 3.4.6
SELinux 有効

インストール

公式が用意している Copr のリポを有効化してインストールします。

# dnf copr enable @cznic/knot-dns-latest
# dnf install knot

ログの設定

ログローテーションの定義ファイルを作成します。

/etc/logrotate.d/knot
/var/log/knot.log
{
        daily
        rotate 7
        missingok
        create 
        dateyesterday
        postrotate
                knotc -b reload
        endscript
}

ログファイルを作成します。

# touch /var/log/knot.log
# chown knot:knot /var/log/knot.log

設定

ゾーンファイル格納用のディレクトリを作成します。

# mkdir /var/lib/knot/zonefile/

以下の定義ファイルを作成します。
今回、example.com ゾーンは後述するコマンドで、182.168.192.in-addr.arpa ゾーンはゾーンファイルを使ってレコードを追加するようにしてみました。

/etc/knot/knot.conf
server:
    rundir: "/run/knot"
    user: knot:knot
    listen: [ 127.0.0.1@53, 192.168.182.10@53 ]
    identity:
    version:
    
log:
  - target: /var/log/knot.log
    any: info

database:
    storage: "/var/lib/knot"
    
mod-queryacl: # 通常の DNS クエリを受け付ける範囲を制限
  - id: default
    address: [192.168.182.0/24,192.168.207.0/24,127.0.0.0/8]
    interface: [192.168.182.10,127.0.0.1]
    
template:
  - id: default
    storage: "/var/lib/knot/zonefile"
    file: "%s.zone"
    semantic-checks: on
    module: mod-queryacl/default
    
zone:
  - domain: example.com

  - domain: 182.168.192.in-addr.arpa
    file: 182.168.192.in-addr.arpa.zone
    reverse-generate: example.com # example.com ゾーンの A/AAAA から PTR を生成

以下がゾーンファイルです。

/var/lib/knot/zonefile/182.168.192.in-addr.arpa.zone
$ORIGIN 182.168.192.in-addr.arpa.
$TTL 3600
@      IN      SOA     dns1.example.com. hostmaster.example.com. (
                  0          ; Serial
                  900        ; refresh
                  600        ; retry
                  86400      ; expire
                  900        ; minimum
)

@                                IN NS        dns1.example.com.
10                               IN PTR       dns1.example.com.
12                               IN PTR       dns-r.example.com.

起動、RR追加

定義ファイルをチェック、FW にて通信を許可し、Knot DNS を起動します。

# chown knot:knot -R /var/lib/knot/zonefile/
# knotc conf-check
# firewall-cmd --add-service=dns --permanent
# firewall-cmd --reload
# systemctl enable knot
# systemctl start knot

RR追加

Knot DNS では以下のようなコマンドを使用して RR を追加します。

# knotc zone-begin example.com # トランザクションを開始
# knotc zone-set example.com  @ 3600 SOA  dns1 hostmaster 1 86400 900 691200 3600 # SOAレコード追加
# knotc zone-set example.com @ 3600 NS dns1 # NSレコード追加
# knotc zone-set example.com dns1 3600 A 192.168.182.10 # Aレコード追加
# knotc zone-set example.com dns2 3600 A 192.168.182.11 # Aレコード追加
# knotc zone-set example.com dns-r 3600 A 192.168.182.12 # Aレコード追加
# knotc zone-diff example.com # diffを確認
# knotc zone-commit example.com #コミット
# knotc zone-begin 182.168.192.in-addr.arpa
# knotc zone-set 182.168.192.in-addr.arpa 15 900 PTR dhcp.example.com.
# knotc zone-commit 182.168.192.in-addr.arpa

設定した内容は、knot.conf で指定したゾーンファイルにも反映されます。
このファイルを編集して再読み込みや再起動を実施すると、その内容が反映されます。
ゾーンファイルを編集した場合でも、reverse-generateオプションは機能します。(example.com. ゾーンの内容から動的に生成されるため)

/var/lib/knot/zonefile/example.com.zone
;; Zone dump (Knot DNS 3.4.6)
example.com.            3600    SOA     dns1.example.com. hostmaster.example.com. 1 86400 900 691200 3600
example.com.            3600    NS      dns1.example.com.
dns-r.example.com.      3600    A       192.168.182.12
dns1.example.com.       3600    A       192.168.182.10
dns2.example.com.       3600    A       192.168.182.11
+ test.example.com.     3600    A       192.168.182.21
;; Written 5 records
;; Time 2025-05-07 09:10:15 JST
# knotc zone-reload
# knotc zone-read --
[182.168.192.in-addr.arpa.] 182.168.192.in-addr.arpa. 3600 NS dns1.example.com.
[182.168.192.in-addr.arpa.] 182.168.192.in-addr.arpa. 3600 SOA dns1.example.com. hostmaster.example.com. 2 900 600 86400 900
[182.168.192.in-addr.arpa.] 10.182.168.192.in-addr.arpa. 3600 PTR dns1.example.com.
[182.168.192.in-addr.arpa.] 11.182.168.192.in-addr.arpa. 3600 PTR dns2.example.com.
[182.168.192.in-addr.arpa.] 12.182.168.192.in-addr.arpa. 3600 PTR dns-r.example.com.
[182.168.192.in-addr.arpa.] 15.182.168.192.in-addr.arpa. 900 PTR dhcp.example.com.
[182.168.192.in-addr.arpa.] 21.182.168.192.in-addr.arpa. 3600 PTR test.example.com.
[example.com.] example.com. 3600 NS dns1.example.com.
[example.com.] example.com. 3600 SOA dns1.example.com. hostmaster.example.com. 1 86400 900 691200 3600
[example.com.] dns-r.example.com. 3600 A 192.168.182.12
[example.com.] dns1.example.com. 3600 A 192.168.182.10
[example.com.] dns2.example.com. 3600 A 192.168.182.11
[example.com.] test.example.com. 3600 A 192.168.182.21

確認

以下の通り解決が可能です。

Resolve-DnsName dns1.example.com. -server 192.168.182.10

Name                                           Type   TTL   Section    IPAddress
----                                           ----   ---   -------    ---------
dns1.example.com                               A      3600  Answer     192.168.182.10


Resolve-DnsName example.com. -server 192.168.182.10

Name                        Type TTL   Section    PrimaryServer               NameAdministrator           SerialNumber
----                        ---- ---   -------    -------------               -----------------           ------------
example.com                 SOA  3600  Authority  dns1.example.com            hostmaster.example.com      1


Resolve-DnsName example.com. -server 192.168.182.10 -type NS

Name                           Type   TTL   Section    NameHost
----                           ----   ---   -------    --------
example.com                    NS     3600  Answer     dns1.example.com

Name       : dns1.example.com
QueryType  : A
TTL        : 3600
Section    : Additional
IP4Address : 192.168.182.10


Resolve-DnsName 192.168.182.10 -server 192.168.182.10

Name                           Type   TTL   Section    NameHost
----                           ----   ---   -------    --------
10.182.168.192.in-addr.arpa    PTR    3600  Answer     dns1.example.com


Resolve-DnsName 182.168.192.in-addr.arpa. -server 192.168.182.10

Name                        Type TTL   Section    PrimaryServer               NameAdministrator           SerialNumber
----                        ---- ---   -------    -------------               -----------------           ------------
182.168.192.in-addr.arpa    SOA  900   Authority  dns1.example.com            hostmaster.example.com      2
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?