Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2026/10/28 08:22:15.034 (台灣標準時)

自分専用の DNS サーバーを作る方法

自分の計算機で仮想機械を動かしている場合、実態のある計算機が一台だけで あっても、複数の IP アドレスとホスト名の対応付けを管理しなくてはならな いでござる。全ての実態のある計算機と仮想機械で同じ /etc/hosts を持つこ とを維持するのは面倒でござる。どのようにするのが便利なのか調べてみたで ござる。名前解決を支援してくれるソフトウェアには、 BIND, unbound, dnsmasq などがあるようでござる。 BIND は設定が複雑で面倒そうでござる。 unbound はローカルゾーンを定義して、そのローカルゾーンを含めた名前を使 わないといけないようでござる。つまり、例えば、 myhome というローカルゾー ン名を使うと、ホスト名 abc である計算機を指し示すには abc.myhome と指 定しないといけないようでござる。 /etc/resolv.conf に search myhome と いう行を加えておけば、 abc でもよいようでござるが、 /etc/resolv.conf は DHCP サーバーから与えられた情報で書き換えられることもあるので、 /etc/resolv.conf は編集しなくて済むほうが便利でござる。というわけで、 dnsmasq を試してみることにしたでござる。

dnsmasq を動かす計算機の /etc/hosts に必要な情報を記述しておくでござる。


10.20.30.40 tokyo
10.20.30.41 kanda
10.20.30.42 ochanomizu
10.20.30.43 yotsuya
10.20.30.44 shinjuku
10.20.30.45 nakano

dnsmasq は pkgsrc に含まれているでござる。以下のようにすれば、簡便にイ ンストールすることができるでござる。


# cd /usr/pkgsrc/net/dnsmasq
# make install

.....


===> Installing binary package of dnsmasq-2.93
dnsmasq-2.93: Creating group ``dnsmasq''
dnsmasq-2.93: Creating user ``dnsmasq''
useradd: Warning: home directory `/nonexistent' doesn't exist, and -m was not specified
dnsmasq-2.93: copying /usr/pkg/share/examples/dnsmasq/dnsmasq.conf.example to /usr/pkg/etc/dnsmasq.conf
===========================================================================
The following files should be created for dnsmasq-2.93:

        /etc/rc.d/dnsmasq (m=0755)
            [/usr/pkg/share/examples/rc.d/dnsmasq]

===========================================================================
# pkgclean

自分自身の /etc/hosts で解決できなかったときに参照するネームサーバーを 設定しておくでござる。 /etc/dnsmasq.conf というファイルを作成し、以下 のような内容を書き込んでおくでござる。


server=1.0.0.1
server=1.1.1.1
server=8.8.4.4
server=8.8.8.8

計算機の起動時に dnsmasq を実行するためのスクリプトをコピーするでござる。


# cp -pi /usr/pkg/share/examples/rc.d/dnsmasq /etc/rc.d

計算機の起動時に dnsmasq が実行されるように /etc/rc.conf に以下の記述 を追加するでござる。


dnsmasq=YES

dnsmasq を動かしてみるでござる。


# /etc/rc.d/dnsmasq start
Starting dnsmasq.

これで、サーバー側での設定は完了でござる。

次にクライアント側で作業するでござる。 /etc/resolv.conf にネームサーバー の IP アドレスを記述しておけばよいでござる。


nameserver 123.45.67.89

こうすれば、名前解決ができるようになるでござる。


% nslookup tokyo
Server:         123.45.67.89
Address:        123.45.67.89#53

Name:   tokyo
Address: 10.20.30.40

% nslookup kanda
Server:         123.45.67.89
Address:        123.45.67.89#53

Name:   kanda
Address: 10.20.30.41


基本的には、これでよいのでござるが、 DHCP を使って IP アドレスを取得し ている場合には、困ることがあるでござる。 DHCP サーバーが提供するネーム サーバーの設定が /etc/resolv.conf に書き込まれるようになっていると、自 前の dnsmasq サーバーを参照できないでござる。もちろん、自分で使ってい る Wi-Fi ルーターなどなら、自分で管理する権限があるので問題ないのでご ざるが、他人が管理しているネットワークを使っている場合には、 DHCP サー バーの設定を勝手に書き換えられないでござる。そのような場合には、 dhcpcd の設定を変えておけばよいようでござる。

/etc/dhcpcd.conf に以下の記述を追加するでござる。


#
# my own domain name server setting
#
static domain_name_servers=123.45.67.89 1.0.0.1 1.1.1.1 8.8.4.4 8.8.8.8

以上のようにしておけば、 DHCP サーバーが提供する情報の内容に関わらず、 常に自分の希望通りのネームサーバーの設定が /etc/resolv.conf に反映され るでござる。



Frequently accessed files

  1. Computer___Network/20260127_00.html
  2. Misc___Taiwan/20240207_00.html
  3. Computer___NetBSD/20250301_01.html
  4. Misc___Taiwan/20240819_00.html
  5. Book___Chinese/20240424_00.html
  6. Computer___TeX/20231107_00.html
  7. Computer___TeX/20240414_01.html
  8. Misc___Japan/20240718_00.html
  9. Misc___Japan/20240610_00.html
  10. Computer___TeX/20240411_00.html
  11. Misc___Taiwan/20240903_01.html
  12. Computer___TeX/20230726_01.html
  13. Computer___Network/20241214_00.html
  14. Computer___Python/20230803_00.html
  15. Computer___Network/20230516_00.html
  16. Computer___NetBSD/20230119_00.html
  17. Computer___NetBSD/20250113_00.html
  18. Computer___FreeBSD/20220621_0.html
  19. Computer___TeX/20240414_00.html
  20. Computer___NetBSD/20250307_00.html
  21. Computer___Network/20240130_00.html
  22. Computer___TeX/20240410_00.html
  23. Computer___Python/20250330_00.html
  24. Computer___NetBSD/20220818_1.html
  25. Computer___NetBSD/20240805_03.html
  26. Computer___TeX/20230503_00.html
  27. Computer___Network/20220413_1.html
  28. Computer___Python/20240101_00.html
  29. Computer___NetBSD/20240101_03.html
  30. Computer___Hardware/20240820_00.html


HTML file generated by Kinoshita Daisuke.