1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Photon OS と dnsmasq で DNS / DHCP サーバーを構築

Last updated at Posted at 2022-10-14

Photon OS は、クラウド環境に最適化された非常に軽量でミニマリストな Linux OS です。

簡単ですが、こちらの記事の冒頭で、Photon OS について紹介をしてます。

非常にシンプルな OS ですが、vSphere 環境であれば、非常に簡単にデプロイできる Linux OS なので、「ちょっと DNS サーバーを立てたい」「xxx を触るのに、DHCP サーバーが必要」という時に、サクッと構築できてしまうので、非常に重宝します。

今回は、この Photon OS を使って、簡易的な DNS サーバー兼、DHCP サーバーを構築していきます。

前提

Photon OS がデプロイ済みであり、最低限の設定として、こちら↓のあたりが行われている想定で、手順を記載しています。

また、執筆時点で、Photon OS は ver 4.0 を利用しています。

# cat /etc/photon-release
VMware Photon OS 4.0
PHOTON_BUILD_NUMBER=2f5aad892

パッケージのインストール

今回利用する dnsmasq をインストールします。

tdnf install -y dnsmasq

DHCP サーバー用の設定ファイルの修正/作成

DNS サーバーとしては、デフォルト設定のままで最低限動作しますが、DHCP サーバーとしての設定が必要となるので、設定ファイルを入れ込むための設定と、設定ファイルを作成していきます。

まず、全体の設定ファイル /etc/dnsmasq.conf 内で、DHCP サーバー用の設定ファイルを格納するためのディレクトリ /etc/dnsmasq.d/ を読み込めるよう修正します。

vi /etc/dnsmasq.conf

修正イメージは、こんな感じ。

/etc/dnsmasq.conf
# ...(略)...
# Include another lot of configuration options.
#conf-file=/etc/dnsmasq.more.conf
conf-dir=/etc/dnsmasq.d    # コメントアウト解除

# Include all the files in a directory except those ending in .bak
conf-dir=/etc/dnsmasq.d,.bak    # コメントアウト解除

# Include all files in a directory which end in .conf
conf-dir=/etc/dnsmasq.d/,*.conf    # コメントアウト解除
# ...(略)...

DHCP サーバー用の設定ファイルを作成します。

vi /etc/dnsmasq.d/dhcp.conf

設定内容の例としては、こんな感じ。

/etc/dnsmasq.d/dhcp.conf
interface=eth0
dhcp-range=192.168.1.1,192.168.1.99,12h
dhcp-option=option:netmask,255.255.255.0
dhcp-option=option:router,192.168.1.254
dhcp-option=option:dns-server,192.168.1.200
dhcp-option=option:ntp-server,192.168.1.200
dhcp-leasefile=/var/lib/misc/dnsmasq.leases

option:ntp-server の部分で、IP アドレスではなく、FQDN で指定すると、エラーするので、注意が必要です。

また、もし固定で IP アドレスを振りたい場合には、以下のようになります。

/etc/dnsmasq.d/dhcp.conf
dhcp-host=98:fe:94:3e:4e:2e,192.168.1.101

iptables でポート開放

Photon OS は、セキュリティ強化のため、デフォルトでは多くのポートが閉じられているので、DNS と DHCP で使うポートを開放しておきます。

vi /etc/systemd/scripts/ip4save

追記するルールの例としては、こんな感じ。

/etc/systemd/scripts/ip4save
-A INPUT -p udp -m udp --dport 53 -j ACCEPT
-A INPUT -i eth0 -p udp --dport 67:68 --sport 67:68 -j ACCEPT

iptables サービスを再起動させ、設定を反映させます。

systemctl restart iptables.service

その際、Docker Daemon が iptables に書き込んでいるルールが消えてしまうため、Docker も使っているなら、Docker Daemon も再起動させる必要があります。

systemctl restart docker

(Photon OS 4.0 以降) systemd-resolved との競合回避

Photon OS 4.0 以降からですが、このまま dnsmasq を起動しようとすると、systemd-resolved が既に、スタブ DNS サーバーとして、53番ポートをバインドしており、起動に失敗してしまいます。

そこで、この競合を回避するために、スタブ DNS サーバーの機能を無効にします。

vi /etc/systemd/resolved.conf

修正イメージとしては、こんな感じ。

/etc/systemd/resolved.conf
#DNSStubListener=yes  # デフォルト有効なので無効化
DNSStubListener=no

systemd-resolved サービスの再起動させ、設定を反映させます。

systemctl restart systemd-resolved.service

dnsmasq の起動

まずは、OS 再起動後も、自動でサービス起動するように設定します。

systemctl enable dnsmasq.service

実際にサービスを (再) 起動させます。

systemctl restart dnsmasq.service

ただし、dnsmasq は systemctl コマンドに結果が返らず、エラーしても気づけないため、必ず status コマンドで起動を確認しておくことをオススメします。

systemctl status dnsmasq.service

もし、何かしらエラーしていて、起動に失敗している場合、直接、dnsmasq コマンドを実行すると、エラー理由を出力してくれるのでオススメです。

dnsmasq
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?