目次
はじめに
RaspberryPiは消費電力が非常に少ないので常時起動しておくサーバに最適です。
そこで今回は、RaspberryPiにDnsmasqをインストールし、ローカルDNSサーバ、DHCPサーバを構築してみます。
ローカルDNSサーバとは
その名の通り、ローカルなIPアドレスにドメインを振るサーバです。
例えば、192.168.1.10というアドレスにmain.localというようなドメインを割り当てることができます。
ローカルネットワークに大量に機器が接続されている場合には、IPアドレスではなくドメインでアクセスできるようになるため、利便性の向上が見込めます。
前提
- ルータのIPアドレス:192.168.0.1
- RaspberryPiのIPアドレス:192.168.0.2
Dnsmasqのインストール
まず、パッケージリストの更新を行い、Dnsmasqをインストールします。
sudo apt-get update
sudo apt-get install dnsmasqインストールを続行するか質問されるのでyを入力します。
Do you want to continue? [Y/n] y自動的にDnsmasqのサービスが開始されてしまうので一旦停止します。
sudo service dnsmasq stopDnsmasqの設定
デフォルトの設定ファイルをバックアップします。
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.old設定ファイルを編集します。
sudo vi /etc/dnsmasq.conf/etc/dnsmasq.confの下のほうにある#conf-file=/etc/dnsmasq.more.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/etc/dnsmasq.more.confを作成し、編集します。
sudo vi /etc/dnsmasq.more.confdomain-needed
bogus-priv
local = /local/
domain = local
expand-hosts
dhcp-range = 192.168.0.100, 192.168.0.200, 12h
dhcp-option = option:router, 192.168.0.1
dhcp-option = option:dns-server, 192.168.0.2, 192.168.0.1
dhcp-option = option:ntp-server, 192.168.0.1
dhcp-host = aa:aa:aa:aa:aa:aa, hoge, 192.168.0.10, infinite
dhcp-host = bb:bb:bb:bb:bb:bb, test各行の説明をしていきます。
domain-needed- ローカルホスト名を上位DNSに転送しない
bogus-priv- ローカルIPアドレスの逆引きを上位DNSに転送しない
local = /local/- ショートドメイン名を補完するドメイン名(hoge -> hoge.local)
domain = local- ローカルドメイン名
expand-hosts- ショートホスト名を補完する
dhcp-range = 192.168.0.100, 192.168.0.200, 12h- DHCPで割り当てするアドレスの範囲とリース時間
- 192.168.0.100 ~ 192.168.0.200までのアドレスをDHCPで12時間割り当てる
dhcp-option = option:router, 192.168.0.1- DHCPクライアントに通知するルータのIPアドレス
dhcp-option = option:dns-server, 192.168.0.2, 192.168.0.1- DHCPクライアントに通知するDNSサーバのIPアドレス
- ここでは自分自身とルータのDNSサーバを指定
dhcp-option = option:ntp-server, 192.168.0.1- DHCPクライアントに通知するNTPサーバのIPアドレス
dhcp-host = aa:aa:aa:aa:aa:aa, hoge, 192.168.0.10, infinite- MACアドレスとホスト名、IPアドレスを紐付けする(リース時間無限)
dhcp-host = bb:bb:bb:bb:bb:bb, test- 指定されたMACアドレスを持つ機器に、ホスト名をつける(IPアドレスはDHCPで動的割付)
RaspberryPiのネットワークインタフェースを設定
/etc/network/interfacesを編集します。
sudo vi /etc/network/interfacesRaspberryPiのIPアドレスを固定します。
auto eth0
iface eth0 inet static
address 192.168.0.2
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
dns-nameservers 127.0.0.1 192.168.0.1
dns-search local他のDHCPサーバを停止する
ルータにDHCPサーバ機能が付加されている場合は、その機能をOFFにしてください。
設定方法については各ルータの設定マニュアルをみてください。
Dnsmasqの起動
sudo service dnsmasq start動作確認
ping hogeこれで正常に応答が帰ってくれば完了です。
