ここでは dns は samba の active directory、dhcpは isc dhcp を使っている
sambaに参加すれば自動的にdnsに登録されるので便利ではあるが、dhcpに登録したら即dnsも引けて欲しい。
そこでdnsmasqを使ってみることにした。
インストール †
[root@c ~]# yum install dnsmasq
設定 †
左側は isc dhcp の設定、右側はdnsmasqの設定
並べてみるとこんな感じでしょうか
「port=0」はdhcpd機能のみ
/etc/dhcp/dhcpd.conf /etc/dnsmasq.conf
port=0
authoritative; dhcp-authoritative
option domain-name "sybyl.local"; domain=sybyl.local
option domain-name-servers 192.168.0.3, 192.168.0.33; dhcp-option=option:dns-server,192.168.0.3,192.168.0.33
option routers 192.168.0.1; dhcp-option=option:router,192.168.0.1
option subnet-mask 255.255.255.0; dhcp-option=option:netmask,255.255.255.0
option broadcast-address 192.168.0.255;
default-lease-time 300;
max-lease-time 720;
range 192.168.0.128 192.168.0.160; dhcp-range=192.168.0.128,192.168.0.160,12h
log-facility local5; log-facility=local5
host cc { dhcp-host=00:0c:29:91:bc:44,192.168.0.2,cc
hardware ethernet 00:0c:29:91:bc:44;
fixed-address 192.168.0.2;
option host-name "cc.sybyl.local";
}
リース時間に関しては下記が詳しい
https://forum.openwrt.org/t/dhcp-clients-renewing-early/6367/3
設定-PXEboot †
既にtftpサーバを構築していて、tftpをそのまま利用するなら新たに「pxe-service」を加えればいいみたい。
*左側は isc dhcp の設定、右側はdnsmasqの設定を示す。
/etc/dhcp/dhcpd.conf /etc/dnsmasq.conf
host client {
hardware ethernet 00:0c:29:c6:03:b4; dhcp-host=00:0c:29:c6:03:b4;192.168.0.83:client
fixed-address 192.168.0.83;
option host-name "client";
filename "/pxeboot/pxelinux.0"; pxe-service=x86PC, "Install Linux", /pxeboot/pxelinux, 192.168.0.3
next-server 192.168.0.3;
}
dnsmasqにはtftpサーバ機能を持っているので、それを利用してPXEbootを有効にするには
まず既存のtftpサービスを停止して、
[root@c ~]# systemctl stop tftp.socket
[root@c ~]# systemctl disable tftp.socket
dnsmasq.confを書き換えて、
*左側は isc dhcp の設定、右側はdnsmasqの設定を示す。
/etc/dhcp/dhcpd.conf /etc/dnsmasq.conf
host client {
hardware ethernet 00:0c:29:c6:03:b4; dhcp-host=00:0c:29:c6:03:b4;192.168.0.83:client
fixed-address 192.168.0.83;
option host-name "client";
filename "/pxeboot/pxelinux.0"; dhcp-boot=pxelinux.0
tftp-root=/var/lib/tftpboot/pxeboot
next-server 192.168.0.3; enable-tftp
}
dnsmasqのrestartを行う
[root@c ~]# systemctl restart dnsmasq
設定-PXEboot-UEFI †
上記はBIOSなマシンのPXEbootでしたが、UEFIベースのマシンでは下記のようにする
/etc/dhcp/dhcpd.conf /etc/dnsmasq.conf
host client {
hardware ethernet 00:0c:29:c6:03:b4; dhcp-host=00:0c:29:c6:03:b4;192.168.0.83:client
fixed-address 192.168.0.83;
option host-name "client";
filename "/pxeboot/pxelinux.0";
filename "/pxeboot/grubx64.efi";
dhcp-match=set:bios, option:client-arch, 0 #x86-32
dhcp-match=set:efi-x86_64, option:client-arch, 7 #EFI x86-64
dhcp-boot=tag:bios,/pxeboot/pxelinux.0,192.168.0.3
dhcp-boot=tag:efi-x86_64,/pxeboot/grubx64.efi,192.168.0.3
next-server 192.168.0.3;
}
ここのマシンのMACアドレスで定まる定義ファイル「grub.cfg-01-00-0c-29-c6-03-b4」は、tftpサービスで「/」になる場所に置く必要がある。
tftp.tftp.serviceで
「ExecStart=/usr/sbin/in.tftpd -s /var/lib/tftpboot」
なら
「/var/lib/tftpboot/grub.cfg-01-00-0c-29-c6-03-b4」に配置する。
DNS †
dnsmasqはdhcpの役割だけでなく、DNSの機能も持つ。ここではDNSの設定方法を記す
留意
当初「dhcp-host」のエントリーのみでDNSも引いてくれると思ったのですが、間違いみたい...
きちんと「/etc/hosts」にも登録する必要があるようでした...
「/etc/hosts」にエントリーを書くか、別のファイルに書いても構わない。その際は
addn-hosts=/etc/dnsmasq.conf.hosts
として、「/etc/dnsmasq.conf.hosts」にエントリーを書く。
その際、「/etc/hosts」の内容を無視したいのなら追加で
としておく。これで/etc/hostsの内容はDNSエントリーには加わらない。
っで、肝心の「/etc/hosts」とか「/etc/dnsmasq.conf.hosts」の中身ですが
127.0.0.1 localhost localhost.localdomain
192.168.0.3 c
192.168.0.10 cc
192.168.0.2 airstation
:
こんか感じで。
あと、「/etc/dnsmasq.conf」としては
port=53 # port=0ならdhcpdのみ機能します
domain-needed # domainが付かない名前の解決に上位DNSは使わない
bogus-priv # 逆引きzoneの解決に上位DNSは使わない
expand-hosts # domain名を付ける
domain=sybyl.local
もし既存でsambaのdnsが機能しているのでそれを停止します。
[root@c ~]# vi /opt/samba/etc/smb.conf
:
- server services = s3fs, rpc, nbt, wrepl, ldap, cldap, kdc, drepl, winbindd, ntp_signd, kcc, dnsupdate, dns
+ server services = s3fs, rpc, nbt, wrepl, ldap, cldap, kdc, drepl, winbindd, ntp_signd, kcc, dnsupdate
:
上記のように設定ファイルを修正して
[root@c ~]# systemctl start samba
その後に、dnsmasqを再起動させる
[root@c ~]# systemctl start dnsmasq
実際にdns検索にdnsmasqが使われているかは「lsof」コマンドで確かめる
[root@c ~]# lsof -i:53
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dnsmasq 12975 nobody 6u IPv4 12070267 0t0 UDP *:domain
dnsmasq 12975 nobody 7u IPv4 12070268 0t0 TCP *:domain (LISTEN)
dnsmasq 12975 nobody 8u IPv6 12070269 0t0 UDP *:domain
dnsmasq 12975 nobody 9u IPv6 12070270 0t0 TCP *:domain (LISTEN)
[root@c ~]#
と確かにdnsmasqでdnsサービスは提供されているようである。
ad対応 †
adのDNSを外すなら、adが持っていたDNS機能をdnsmasqに加える必要がある。
SRVレコードらがそれに該当する
dhcp-option=ip-forward-enable,0
dhcp-option=44,192.168.0.3
dhcp-option=45,192.168.0.3
dhcp-option=netbios-nodetype,8
srv-host=_ldap._tcp.sybyl.local,c.sybyl.local,389
srv-host=_kerberos._tcp.sybyl.local,c.sybyl.local,88
srv-host=_ldap._tcp.dc._msdcs.sybyl.local,c.sybyl.local,389
srv-host=_kerberos._tcp.dc._msdcs.sybyl.local,c.sybyl.local,88
srv-host=gc._msdcs.sybyl.local,c.sybyl.local,3268
srv-host=_gc._tcp.sybyl.local,c.sybyl.local
srv-host=_ldap._tcp.Default-First-Site-Name._sites.sybyl.local,c.sybyl.local
参照先https://technet.microsoft.com/ja-jp/library/cc985025.aspx
テスト
[saber@c ~]$ nslookup
> set querytype=SRV
> _ldap._tcp.sybyl.local
Server: 127.0.0.1
Address: 127.0.0.1#53
_ldap._tcp.sybyl.local service = 0 0 389 c.sybyl.local.
>
> _kerberos._tcp.sybyl.local
Server: 127.0.0.1
Address: 127.0.0.1#53
_kerberos._tcp.sybyl.local service = 0 0 88 c.sybyl.local.
>
> _ldap._tcp.dc._msdcs.sybyl.local
Server: 127.0.0.1
Address: 127.0.0.1#53
_ldap._tcp.dc._msdcs.sybyl.local service = 0 0 389 c.sybyl.local.
>
> _kerberos._tcp.dc._msdcs.sybyl.local
Server: 127.0.0.1
Address: 127.0.0.1#53
_kerberos._tcp.dc._msdcs.sybyl.local service = 0 0 88 c.sybyl.local.
>
> gc._msdcs.sybyl.local
Server: 127.0.0.1
Address: 127.0.0.1#53
gc._msdcs.sybyl.local service = 0 0 3268 c.sybyl.local.
>
> exit
[saber@c ~]$
nicが2つ以上あって、片方のみで使いたい †
nicが2つ(eth0とeth1)あって、片方は上位LAN(eth0)、もう片方はローカルLAN(eth1)で、dnsmasqが動くマシンが
ルーター的な役割を持つ場合。内部のローカルLAN(eth1)だけで運用したいなら
とする。あるいは
とする
うまく起動しない †
うなく起動しない場合があって、dnsmasq.serviceを修正
[root@c ~]# systemctl edit --full dnsmasq.service
[Unit]
Description=DNS caching server.
After=network.target samba.service
[Service]
ExecStart=/usr/sbin/dnsmasq -k
[Install]
WantedBy=multi-user.target
「After」ルールに既存の「network.target」のほかに「samba.service」を追加した
log †
syslog経由でログを出したいので、まず「/etc/rsyslog.conf」を修正
*.info;mail.none;authpriv.none;cron.none;local6.none;local5.none /var/log/messages
:
:
local5.* /var/log/dhcpd.log
これでlocal5の内容は/var/log/dhcpd.logに集まる。
そして、dnsmasq.confにて
log-facility=local5
log-dhcp
でdnsmasqからログはsyslogのlocal5にわたる。内容は「log-dhcp」の内容となる。
MACアドレスを変更したのにipが付与されない †
以前のMACアドレスにそのipを付与してるのが原因
「journal -u dnsmasq」で
「not using configured address <ip アドレス> because it is leased to <MACアドレス>」と
表記されたこの問題かと思われ
解決方法はdnsmasqに保持されいているMACとipの対応表から該当行を削除してdnsmasqを起動する
systemctl stop dnsmasq
vi /var/lib/dnsmasq/dnsmasq.leases
(問題のipアドレスを含む行を削除)
systemctl start dnsmasq
CentOS8でdnamsq 2.88を使ってみる †
標準で入っているのは dnsmasq-2.79 である. これを 2.88に上げてみた
[root@c ~]# wget https://thekelleys.org.uk/dnsmasq/dnsmasq-2.88.tar.xz
(ついでに)
[root@c ~]# dnf download --source dnsmasq
ついでに拾った「dnsmasq-2.79-24.el8.src.rpm」の中身を参照しつつコンパイル/差し替えを行う
[root@c ~]# tar xf dnsmasq-2.88.tar.xz
[root@c ~]# cd dnsmasq-2.88/
| --- Makefile.orig 2023-01-03 00:28:01.774235610 +0900
+++ Makefile 2023-01-03 00:31:18.715458908 +0900
@@ -18,7 +18,7 @@
# Variables you may well want to override.
-PREFIX = /usr/local
+PREFIX = /usr
BINDIR = $(PREFIX)/sbin
MANDIR = $(PREFIX)/share/man
LOCALEDIR = $(PREFIX)/share/locale
@@ -26,7 +26,7 @@
DESTDIR =
CFLAGS = -Wall -W -O2
LDFLAGS =
-COPTS =
+COPTS = -DHAVE_DBUS -DHAVE_LIBIDN2 -DHAVE_DNSSEC
RPM_OPT_FLAGS =
LIBS =
|
| --- src/config.h.orig 2023-01-03 00:27:14.134213995 +0900
+++ src/config.h 2023-01-03 00:27:53.584059976 +0900
@@ -45,8 +45,8 @@
#define ETHERSFILE "/etc/ethers"
#define DEFLEASE 3600 /* default DHCPv4 lease time, one hour */
#define DEFLEASE6 (3600*24) /* default lease time for DHCPv6. One day. */
-#define CHUSER "nobody"
-#define CHGRP "dip"
+#define CHUSER "dnsmasq"
+#define CHGRP "dnsmasq"
#define TFTP_MAX_CONNECTIONS 50 /* max simultaneous connections */
#define LOG_MAX 5 /* log-queue length */
#define RANDFILE "/dev/urandom"
@@ -211,7 +211,7 @@
# elif defined(__ANDROID__)
# define LEASEFILE "/data/misc/dhcp/dnsmasq.leases"
# else
-# define LEASEFILE "/var/lib/misc/dnsmasq.leases"
+# define LEASEFILE "/var/lib/dnsmasq/dnsmasq.leases"
# endif
#endif
|
っでmakeして差し替え
[root@c dnsmasq-2.88]# make
[root@c ~]# cp -a /sbin/dnsmasq /sbin/dnsmasq.orig
[root@c ~]# systemctl stop dnsmasq.service
[root@c ~]# cp dnsmasq-2.88/src/dnsmasq
dnsmasq dnsmasq.c dnsmasq.h dnsmasq.o
[root@c ~]# cp dnsmasq-2.88/src/dnsmasq /sbin/dnsmasq
cp: overwrite '/sbin/dnsmasq'? yes
[root@c ~]# systemctl start dnsmasq.service
ログから
Jan 03 00:36:09 c.sybyl.local systemd[1]: Started DNS caching server..
Jan 03 00:36:09 c.sybyl.local dnsmasq[699868]: started, version 2.88 cachesize 150
Jan 03 00:36:09 c.sybyl.local dnsmasq[699868]: compile time options: IPv6 GNU-getopt DBus no-UBus no-i18n IDN2 DHCP DHCPv6 no-Lua TFTP (改行
no-conntrack ipset no-nftset auth cryptohash DNSSEC loop-detect inotify dumpfile
(以前の場合)
Dec 17 14:19:21 c.sybyl.local dnsmasq[14192]: started, version 2.79 cachesize 150
Dec 17 14:19:21 c.sybyl.local dnsmasq[14192]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN2 DHCP DHCPv6 no-Lua TFTP (改行
no-conntrack ipset auth DNSSEC loop-detect inotify