您尚未登录。

#1 2014-07-03 02:28:12

gouera
会员
注册时间: 2014-05-18
帖子: 12

dnsmasq不能缓存了

这个是我的配置文件
[wsl@wsl ~]$ grep -Ev "(^#|^$)" /etc/dnsmasq.conf
resolv-file=/etc/resolv.dnsmasq.conf
listen-address=127.0.0.1,192.168.1.169

一开始本机用的是dhcpcd开获取ip地址,是可以缓存的。
今晚换成了静态ip地址,就不可以了。。。真搞不懂这个软件是怎么回事.

这是连续2个查询的结果,可以看见查询时间都比较长的。

[wsl@wsl ~]$ dig baidu.com

; <<>> DiG 9.9.2-P2 <<>> baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17572
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;baidu.com.			IN	A

;; ANSWER SECTION:
baidu.com.		600	IN	A	220.181.111.86

;; Query time: 61 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Jul  3 02:27:16 2014
;; MSG SIZE  rcvd: 43

[wsl@wsl ~]$ dig baidu.com

; <<>> DiG 9.9.2-P2 <<>> baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4747
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;baidu.com.			IN	A

;; ANSWER SECTION:
baidu.com.		600	IN	A	123.125.114.144

;; Query time: 56 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Thu Jul  3 02:27:17 2014
;; MSG SIZE  rcvd: 43

[wsl@wsl ~]$

最近编辑记录 依云 (2014-07-06 10:50:49)

离线

#2 2014-07-03 02:50:38

gouera
会员
注册时间: 2014-05-18
帖子: 12

Re: dnsmasq不能缓存了

刚才看了下官方wiki,我是用Networkmanager的,要在Networkmanager的配置文件里面添加一条dns=dnsmasq  和创建一个cache的文件,我都照做了,但是还是不能缓存。

离线

#3 2014-07-03 09:05:29

cuihao
所在地: USTC, Hefei
注册时间: 2011-08-19
帖子: 1,222
个人网站

Re: dnsmasq不能缓存了

搞错了,你可以看到 dnsmasq 的 wiki 写的是:

NetworkManager has the ability to start dnsmasq from its configuration file. Add the option dns=dnsmasq to NetworkManager.conf in the [main] section then disable the dnsmasq.service from being loaded by systemd:

NetworkManager 的 dns=dnsmasq(默认应该就是这样),是 nm 自己调用 dnsmasq,走它的配置(不会加载 /etc/dnsmasq.conf)。DNS 会设置为你在 nm 里配置(或自动获取)的服务器。
如果你是按 dnsmasq 的配置方法,自己安装启动的 dnsmasq,会和 nm 的冲突(于是导致某个用不了)。

你有两种自己配置 dnsmasq 的方法:
1、让 nm 启动 dnsmasq(保留 nm 配置的 dns=dnsmasq),在 /etc/NetworkManager/dnsmasq.d 写额外的 dnsmasq 配置,nm 调用 dnsmasq 时会加载这些配置。
2、自己启动 dnsmasq(去掉 nm 配置的 dns=dnsmasq),在 nm 中将网络连接的 DNS 手动设置为 127.0.0.1。

最近编辑记录 cuihao (2014-07-03 09:06:24)


Site: CVHC.CC   Twitter: @cuihaoleo   Org: LUG@USTC
AD:  ~欢迎参与志愿计算~

离线

#4 2014-07-04 10:22:47

gouera
会员
注册时间: 2014-05-18
帖子: 12

Re: dnsmasq不能缓存了

cuihao 说:

搞错了,你可以看到 dnsmasq 的 wiki 写的是:

NetworkManager has the ability to start dnsmasq from its configuration file. Add the option dns=dnsmasq to NetworkManager.conf in the [main] section then disable the dnsmasq.service from being loaded by systemd:

NetworkManager 的 dns=dnsmasq(默认应该就是这样),是 nm 自己调用 dnsmasq,走它的配置(不会加载 /etc/dnsmasq.conf)。DNS 会设置为你在 nm 里配置(或自动获取)的服务器。
如果你是按 dnsmasq 的配置方法,自己安装启动的 dnsmasq,会和 nm 的冲突(于是导致某个用不了)。

你有两种自己配置 dnsmasq 的方法:
1、让 nm 启动 dnsmasq(保留 nm 配置的 dns=dnsmasq),在 /etc/NetworkManager/dnsmasq.d 写额外的 dnsmasq 配置,nm 调用 dnsmasq 时会加载这些配置。
2、自己启动 dnsmasq(去掉 nm 配置的 dns=dnsmasq),在 nm 中将网络连接的 DNS 手动设置为 127.0.0.1。

也不行啊,我修改了nm的配置文件,将dns=dnsmasq删除了,在nm的ui界面填的127.0.0.1的dns
这样虽然可以用,但是也还是不能缓存。
这个是nm的配置文件:
[root@wsl ~]# cat /etc/NetworkManager/NetworkManager.conf
[main]
plugins=keyfile
no-auto-default=50:E5:49:53:A9:AA,
[root@wsl ~]#

dns文件:
[root@wsl ~]# cat /etc/resolv.conf
# Generated by resolvconf
nameserver 127.0.0.1
[root@wsl ~]#

dnsmasq配置文件:
[root@wsl ~]# grep -Ev "(^$|^#)" /etc/dnsmasq.conf
resolv-file=/etc/resolv.dnsmasq.conf
listen-address=127.0.0.1,192.168.1.169
[root@wsl ~]#

dnsmasq上游地址:
[root@wsl ~]# cat /etc/resolv.dnsmasq.conf
nameserver 42.120.21.30
[root@wsl ~]#


查询结果:
[root@wsl ~]# dig qq.com

; <<>> DiG 9.9.2-P2 <<>> qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27799
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;qq.com.                IN    A

;; ANSWER SECTION:
qq.com.            600    IN    A    14.17.32.211

;; Query time: 59 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Fri Jul  4 18:21:26 2014
;; MSG SIZE  rcvd: 40

[root@wsl ~]# dig qq.com

; <<>> DiG 9.9.2-P2 <<>> qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12322
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;qq.com.                IN    A

;; ANSWER SECTION:
qq.com.            600    IN    A    182.140.167.44

;; Query time: 54 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Fri Jul  4 18:21:29 2014
;; MSG SIZE  rcvd: 40

[root@wsl ~]#

离线

#5 2014-07-04 11:07:57

cuihao
所在地: USTC, Hefei
注册时间: 2011-08-19
帖子: 1,222
个人网站

Re: dnsmasq不能缓存了

那就大概是 dnsmasq 哪里配置错误了吧。
看看日志有没有什么有用的信息?反正我也没啥头绪了。

或者试试让 nm 启动 dnsmasq。

话说你要 listen 192.168.1.169,是要对外提供 dns 服务么?


Site: CVHC.CC   Twitter: @cuihaoleo   Org: LUG@USTC
AD:  ~欢迎参与志愿计算~

离线

#6 2014-07-06 00:46:30

gouera
会员
注册时间: 2014-05-18
帖子: 12

Re: dnsmasq不能缓存了

cuihao 说:

那就大概是 dnsmasq 哪里配置错误了吧。
看看日志有没有什么有用的信息?反正我也没啥头绪了。

或者试试让 nm 启动 dnsmasq。

话说你要 listen 192.168.1.169,是要对外提供 dns 服务么?


刚才修改了dns,发现又可以缓存了。。。
原来是因为我用了openerdns。

刚才在社区看到个帖子,说openerdns容易被攻击..想想换一个试试,修改成114.114.114.114,dig一下发现可以缓存了。。。然后我又换回openerdns,发现不能缓存。在换回114... 发现又可以。
想不通为什么。
难道openerdns阻止了dnsmasq的缓存动作?

离线

#7 2014-07-06 10:49:54

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,491
个人网站

Re: dnsmasq不能缓存了

gouera 说:
cuihao 说:

那就大概是 dnsmasq 哪里配置错误了吧。
看看日志有没有什么有用的信息?反正我也没啥头绪了。

或者试试让 nm 启动 dnsmasq。

话说你要 listen 192.168.1.169,是要对外提供 dns 服务么?


刚才修改了dns,发现又可以缓存了。。。
原来是因为我用了openerdns。

刚才在社区看到个帖子,说openerdns容易被攻击..想想换一个试试,修改成114.114.114.114,dig一下发现可以缓存了。。。然后我又换回openerdns,发现不能缓存。在换回114... 发现又可以。
想不通为什么。
难道openerdns阻止了dnsmasq的缓存动作?

openerdns 的实现有问题。TTL 统一设置的 600。可能还有其它问题。

我这里 dnsmasq 也没办法缓存它。

离线

#8 2014-07-06 11:30:44

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,491
个人网站

Re: dnsmasq不能缓存了

查清楚了,是因为回复没有 RA(Recursion Allowed)标志的原因。

源码位于:dnsmasq-2.71/src/rfc1035.c:1155

  /* Don't put stuff from a truncated packet into the cache.
     Don't cache replies from non-recursive nameservers, since we may get a 
     reply containing a CNAME but not its target, even though the target 
     does exist. */
  if (!(header->hb3 & HB3_TC) && 
      !(header->hb4 & HB4_CD) &&
      (header->hb4 & HB4_RA) &&
      !no_cache_dnssec)
    cache_end_insert();

检查代码(经过整理):

>>> import dnslib, socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> q = dnslib.DNSRecord.question('baidu.com', 'A')

>>> s.sendto(q.pack(), ('openerdns', 53))
>>> ans = s.recvfrom(2048)
>>> a_bad = dnslib.DNSRecord.parse(ans[0])

>>> s.sendto(q.pack(), ('8.8.8.8', 53))
>>> ans = s.recvfrom(2048)
>>> a_good = dnslib.DNSRecord.parse(ans[0])

>>> a_bad
<DNS Header: id=0xb819 type=RESPONSE opcode=QUERY flags=RD rcode='NOERROR' q=1 a=1 ns=0 ar=0>
<DNS Question: 'baidu.com.' qtype=A qclass=IN>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=600 rdata='220.181.111.85'>
>>> a_good
<DNS Header: id=0xb819 type=RESPONSE opcode=QUERY flags=RD,RA rcode='NOERROR' q=1 a=3 ns=0 ar=0>
<DNS Question: 'baidu.com.' qtype=A qclass=IN>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=135 rdata='123.125.114.144'>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=135 rdata='220.181.111.85'>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=135 rdata='220.181.111.86'>

>>> s.sendto(q.pack(), ('', 53))                                                                                                                       
>>> ans = s.recvfrom(2048)                                                                                                                             
>>> a_local = dnslib.DNSRecord.parse(ans[0])                                                                                                           
>>> a_local
<DNS Header: id=0xb819 type=RESPONSE opcode=QUERY flags=RD,RA rcode='NOERROR' q=1 a=3 ns=0 ar=0>
<DNS Question: 'baidu.com.' qtype=A qclass=IN>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=258 rdata='220.181.111.85'>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=258 rdata='220.181.111.86'>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=258 rdata='123.125.114.144'>

离线

#9 2014-07-07 03:37:29

gouera
会员
注册时间: 2014-05-18
帖子: 12

Re: dnsmasq不能缓存了

百合仙子 说:

查清楚了,是因为回复没有 RA(Recursion Allowed)标志的原因。

源码位于:dnsmasq-2.71/src/rfc1035.c:1155

  /* Don't put stuff from a truncated packet into the cache.
     Don't cache replies from non-recursive nameservers, since we may get a 
     reply containing a CNAME but not its target, even though the target 
     does exist. */
  if (!(header->hb3 & HB3_TC) && 
      !(header->hb4 & HB4_CD) &&
      (header->hb4 & HB4_RA) &&
      !no_cache_dnssec)
    cache_end_insert();

检查代码(经过整理):

>>> import dnslib, socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> q = dnslib.DNSRecord.question('baidu.com', 'A')

>>> s.sendto(q.pack(), ('openerdns', 53))
>>> ans = s.recvfrom(2048)
>>> a_bad = dnslib.DNSRecord.parse(ans[0])

>>> s.sendto(q.pack(), ('8.8.8.8', 53))
>>> ans = s.recvfrom(2048)
>>> a_good = dnslib.DNSRecord.parse(ans[0])

>>> a_bad
<DNS Header: id=0xb819 type=RESPONSE opcode=QUERY flags=RD rcode='NOERROR' q=1 a=1 ns=0 ar=0>
<DNS Question: 'baidu.com.' qtype=A qclass=IN>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=600 rdata='220.181.111.85'>
>>> a_good
<DNS Header: id=0xb819 type=RESPONSE opcode=QUERY flags=RD,RA rcode='NOERROR' q=1 a=3 ns=0 ar=0>
<DNS Question: 'baidu.com.' qtype=A qclass=IN>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=135 rdata='123.125.114.144'>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=135 rdata='220.181.111.85'>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=135 rdata='220.181.111.86'>

>>> s.sendto(q.pack(), ('', 53))                                                                                                                       
>>> ans = s.recvfrom(2048)                                                                                                                             
>>> a_local = dnslib.DNSRecord.parse(ans[0])                                                                                                           
>>> a_local
<DNS Header: id=0xb819 type=RESPONSE opcode=QUERY flags=RD,RA rcode='NOERROR' q=1 a=3 ns=0 ar=0>
<DNS Question: 'baidu.com.' qtype=A qclass=IN>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=258 rdata='220.181.111.85'>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=258 rdata='220.181.111.86'>
<DNS RR: 'baidu.com.' rtype=A rclass=IN ttl=258 rdata='123.125.114.144'>

非常感谢,还专门去看源码了。。

离线

页脚