大网数据广告活动
服务器运维问答

dnsmasq reads addn-hosts config but ignores it, falls back to system's /etc/hosts

· 52CE 我爱测

I'm running dnsmasq installed as a NetworkManager plugin on my local Fedora 39 machine. All I need from dnsmasq is basically support for additional hosts files that I can organise by topics. My configs are: /etc/dnsmasql.conf : u…

问题原文

原帖链接 · vrs · CC BY-SA 4.0

I'm running dnsmasq installed as a NetworkManager plugin on my local Fedora 39 machine. All I need from dnsmasq is basically support for additional hosts files that I can organise by topics.

My configs are:

  1. /etc/dnsmasql.conf:
user=dnsmasq
group=dnsmasq
interface=lo
bind-interfaces
conf-dir=/etc/NetworkManager/dnsmasq.d,*.conf
conf-dir=/etc/dnsmasq.d,.rpmnew,.rpmsave,.rpmorig
  1. /etc/NetworkManager/conf.d/00-use-dnsmasq.conf
[main]
dns=dnsmasq
  1. /etc/NetworkManager/dnsmasq.d/00-add-hosts.conf
log-queries
no-hosts
addn-hosts=/etc/hosts.d
address=/development/127.0.0.1
  1. /etc/hosts.d is a directory with the hosts files that contain entries I want to "sinkhole":
0.0.0.0 1.example.com
0.0.0.0 2.example.com
...

dnsmasq is up and running, I see the daemon in my processes:

ps aux | grep dnsmasq

dnsmasq    61161  0.4  0.0 228096  4480 ?        S    16:08   0:00 /usr/sbin/dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/run/NetworkManager/dnsmasq.pid --listen-address=127.0.0.1 --cache-size=400 --clear-on-reload --conf-file=/dev/null --enable-dbus=org.freedesktop.NetworkManager.dnsmasq --conf-dir=/etc/NetworkManager/dnsmasq.d

I also see in the dnsmasq debug logs, that hosts files from /etc/hosts.d are read too:

Dec 30 16:18:40 fedora dnsmasq[61823]: chown of PID file /run/NetworkManager/dnsmasq.pid failed: Operation not permitted
Dec 30 16:18:40 fedora dnsmasq[61823]: DBus support enabled: connected to system bus
Dec 30 16:18:40 fedora dnsmasq[61823]: warning: no upstream servers configured
Dec 30 16:18:40 fedora dnsmasq[61823]: read /etc/hosts.d/private - 10 names
Dec 30 16:18:40 fedora dnsmasq[61823]: setting upstream servers from DBus
Dec 30 16:18:40 fedora dnsmasq[61823]: using nameserver 192.168.0.1#53(via wlp2s0)

But when I try to access domains from the addn-hosts hosts files, they aren't resolved to 0.0.0.0 as I'd expect. These hosts files seem to be ignored.

On the other hand, anything that I add to system's /etc/hosts works as expected. Even though I used no-hosts options explicitly in the dnsmasq config and I see that it's added to the daemon invocation.

I alos see that dnsmasq config is used, because the address=/development/127.0.0.1 line works as expected:

$ ping -c 3 anything.development
PING anything.development (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.091 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.175 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.200 ms

It's just addn-hosts that is ignored somehow.

Here's my /etc/resolv.conf:

# Generated by NetworkManager
nameserver 127.0.0.1
options edns0 trust-ad

Previously I thought that it's systemd-resolved.service that interferes (as it previously added 127.0.0.53 to my resolv.conf file), but now that I disabled the service:

systemctl status systemd-resolved.service
○ systemd-resolved.service - Network Name Resolution
     Loaded: loaded (/usr/lib/systemd/system/systemd-resolved.service; disabled; preset: enabled)
    Drop-In: /usr/lib/systemd/system/service.d
             └─10-timeout-abort.conf
     Active: inactive (dead)

I'm not sure what's wrong with my configs.

已采纳解答

原帖链接 · Tai Viinikka · CC BY-SA 4.0

You've done a good job of describing the situation but you can be more particular about testing the name resolution you're seeing. Do you have dig or nslookup as command-line tools on your system? If so, please use them to query the on-machine nameserver directly rather than whatever you did.

But when I try to access domains from the addn-hosts hosts files, they aren't resolved to 0.0.0.0 as I'd expect. These hosts files seem to be ignored.

I'm seeing that you don't have an upstream name server defined in your config (and yet obviously you are using an upstream name server.) I suspect that the resolution for the blacklisted domains is actually coming from your upstream. So the first question I'd ask is: Does my local dnsmasq instance correctly respond?

Here's what success looks like for me:

tai@fay:~ $ dig adserver.com          

; <<>> DiG 9.16.44-Raspbian <<>> adserver.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 18567
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;adserver.com.                  IN      A

;; Query time: 259 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Dec 31 16:12:15 EST 2023
;; MSG SIZE  rcvd: 41

Note the SERVER: field in 3rd-to-last line reassures me I am asking my local server. You can specify a server name or IP address after the question in this way, which happens to show a normal resolution (which is what we don't want in this case):

tai@fay:~ $ dig adserver.com @8.8.8.8      

; <<>> DiG 9.16.44-Raspbian <<>> adserver.com @8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23032
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;adserver.com.                  IN      A

;; ANSWER SECTION:
adserver.com.           5400    IN      A       159.127.40.163

;; Query time: 119 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Dec 31 16:11:57 EST 2023
;; MSG SIZE  rcvd: 57

(This system I'm using here is quite similar to your set-up but 1) it's a dedicated DNS/DHCP machine and 2) I do not have the added complication of modularizing my dnsmasq through NetworkManager.)

DNS can get pretty complicated in situations like this and I'm not surprised neither you nor I can immediately see what the problem is. Try probing your local dnsmasq specifically, and also add some details about where resolution is occurring, if not from your dnsmasq!

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-12-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。