#author("2025-05-03T15:42:58+00:00","default:sysosa","sysosa") #author("2026-01-24T01:41:53+00:00","default:sysosa","sysosa") ここでは dns は samba の active directory、dhcpは isc dhcp を使っている sambaに参加すれば自動的にdnsに登録されるので便利ではあるが、dhcpに登録したら即dnsも引けて欲しい。 そこでdnsmasqを使ってみることにした。 ***インストール [#h358e83d] #code(nonumber){{ [root@c ~]# yum install dnsmasq }} ***設定 [#l41cab9d] 左側は isc dhcp の設定、右側はdnsmasqの設定 並べてみるとこんな感じでしょうか 「port=0」はdhcpd機能のみ #code(nonumber){{ /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 [#dd0961a7] 既にtftpサーバを構築していて、tftpをそのまま利用するなら新たに「pxe-service」を加えればいいみたい。 &color(red){*};左側は isc dhcp の設定、右側はdnsmasqの設定を示す。 #code(nonumber){{ /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サービスを停止して、 #code(nonumber){{ [root@c ~]# systemctl stop tftp.socket [root@c ~]# systemctl disable tftp.socket }} dnsmasq.confを書き換えて、 &color(red){*};左側は isc dhcp の設定、右側はdnsmasqの設定を示す。 #code(nonumber){{ /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を行う #code(nonumber){{ [root@c ~]# systemctl restart dnsmasq }} ***設定-PXEboot-UEFI [#tb60d0b6] 上記はBIOSなマシンのPXEbootでしたが、UEFIベースのマシンでは下記のようにする #code(nonumber){{ /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 [#ce74d4ad] dnsmasqはdhcpの役割だけでなく、DNSの機能も持つ。ここではDNSの設定方法を記す &color(white,blue){留意}; 当初「dhcp-host」のエントリーのみでDNSも引いてくれると思ったのですが、間違いみたい... きちんと「/etc/hosts」にも登録する必要があるようでした... 「/etc/hosts」にエントリーを書くか、別のファイルに書いても構わない。その際は #code(nonumber){{ addn-hosts=/etc/dnsmasq.conf.hosts }} として、「/etc/dnsmasq.conf.hosts」にエントリーを書く。 その際、「/etc/hosts」の内容を無視したいのなら追加で #code(nonumber){{ no-hosts }} としておく。これで/etc/hostsの内容はDNSエントリーには加わらない。 っで、肝心の「/etc/hosts」とか「/etc/dnsmasq.conf.hosts」の中身ですが #code(nonumber){{ 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」としては #code(nonumber){{ port=53 # port=0ならdhcpdのみ機能します domain-needed # domainが付かない名前の解決に上位DNSは使わない bogus-priv # 逆引きzoneの解決に上位DNSは使わない expand-hosts # domain名を付ける domain=sybyl.local }} もし既存でsambaのdnsが機能しているのでそれを停止します。 #code(nonumber){{ [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 : }} 上記のように設定ファイルを修正して #code(nonumber){{ [root@c ~]# systemctl start samba }} その後に、dnsmasqを再起動させる #code(nonumber){{ [root@c ~]# systemctl start dnsmasq }} 実際にdns検索にdnsmasqが使われているかは「lsof」コマンドで確かめる #code(nonumber){{ [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対応 [#h32f3333] adのDNSを外すなら、adが持っていたDNS機能をdnsmasqに加える必要がある。 SRVレコードらがそれに該当する #code(nonumber){{ 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>+https://technet.microsoft.com/ja-jp/library/cc985025.aspx]] テスト #code(nonumber){{ [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つ以上あって、片方のみで使いたい [#s6749fbe] nicが2つ(eth0とeth1)あって、片方は上位LAN(eth0)、もう片方はローカルLAN(eth1)で、dnsmasqが動くマシンが ルーター的な役割を持つ場合。内部のローカルLAN(eth1)だけで運用したいなら #code(nonumber){{ interface=eth1 }} とする。あるいは #code(nonumber){{ except-interface=eth0 }} とする ***うまく起動しない [#o2655d32] うなく起動しない場合があって、dnsmasq.serviceを修正 #code(nonumber){{ [root@c ~]# systemctl edit --full dnsmasq.service [Unit] Description=DNS caching server. After=network.target network-online.target [Service] ExecStart=/usr/sbin/dnsmasq -k [Install] WantedBy=multi-user.target }} 「After」ルールに既存の「network.target」のほかに「network-online.target」を追加した ***log [#ta31dd79] syslog経由でログを出したいので、まず「/etc/rsyslog.conf」を修正 #code(nonumber){{ *.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にて #code(nonumber){{ log-facility=local5 log-dhcp }} でdnsmasqからログはsyslogのlocal5にわたる。内容は「log-dhcp」の内容となる。 ***MACアドレスを変更したのにipが付与されない [#a7d851a5] 以前のMACアドレスにそのipを付与してるのが原因 「journal -u dnsmasq」で 「&color(red){not using configured address <ip アドレス> because it is leased to <MACアドレス>};」と 表記されたこの問題かと思われ 解決方法はdnsmasqに保持されいているMACとipの対応表から該当行を削除してdnsmasqを起動する #code(nonumber){{ systemctl stop dnsmasq vi /var/lib/dnsmasq/dnsmasq.leases (問題のipアドレスを含む行を削除) systemctl start dnsmasq }} 場合によっては「/var/lib/misc/dnsmasq.leases」の時もある. ***CentOS8でdnamsq 2.88を使ってみる [#b48ef05c] 標準で入っているのは dnsmasq-2.79 である. これを 2.88に上げてみた #code(nonumber){{ [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」の中身を参照しつつコンパイル/差し替えを行う #code(nonumber){{ [root@c ~]# tar xf dnsmasq-2.88.tar.xz [root@c ~]# cd dnsmasq-2.88/ }} #code(diff,nonumber){{ --- 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 = }} #code(diff,nonumber){{ --- 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して差し替え #code(nonumber){{ [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 }} ログから #code(nonumber){{ 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 }} ***Web Proxy Auto-Discovery Protocol (WPAD) / Proxy Auto-Config (PAC) [#v496d5d1] Webプロキシ自動検出プロトコルを受信したら dnamsq が proxy設定情報の入った pac ファイルをありかを提示します 基本、要求プロトコルを出すクライアントしか使えないです. windowsは確か対応. macOSは存じませんが多分そうじゃないかなと. っでdnsmasq側の設定とpacファイルを提供するwebブラウザの設定が必要(コンテンツも) dnamsq側は dnsmasq.confに #code(nonumber){{ dhcp-option=252,"http://192.168.0.3/cerius.pac" }} と記載してdnsmasqを再起動します. あとは 192.168.0.3 でhttpサービスを起動して「/cerius.pac」を定義します. proxyを提供するマシンが squid(192.168.0.136:3128) なら #code(nonumber){{ function FindProxyForURL(url, host) { if ( isInNet(host, "192.168.0.0", "255.255.255.0") ) { return "DIRECT"; } else { return "PROXY 192.168.0.136:3128"; } } }} とします