我在网上看到的大多数信息都说要编辑/etc/resolv.conf
,但我在那里做的任何改动都被覆盖了。
$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND --
# YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1
看来127.0.1.1是dnsmasq
的一个本地实例。dnsmasq
文档说要编辑/etc/resolv.conf
。我试着在/etc/resolv.conf.d/base
中放入自定义的nameservers,但运行sudo resolvconf -u
后,这些变化没有在/etc/resolv.conf
中显示出来。
顺便说一下,我不想在每个连接的基础上改变DNS,我想设置默认的DNS设置,在没有其他指定的情况下用于所有连接。
更新:
我自己回答了这个问题。 https://unix.stackexchange.com/a/163506/67024
我认为这是最好的解决方案,因为。
1.它是有效的。 2.2.它需要最少的改动,而且 3.它仍然与dnsmasq'的DNS缓存一起工作,而不是绕过它。
我相信如果你想覆盖DNS命名服务器,你只需在你的 "base "文件的 "resolv.conf.d "下添加与此类似的一行。
$ sudo vim /etc/resolvconf/resolv.conf.d/base
然后把你的名字服务器列表像这样放进去。
nameserver 8.8.8.8
nameserver 8.8.4.4
最后更新resolvconf
。
$ sudo resolvconf -u
如果你看一下resolvconf
的手册,它描述了/etc/resolvconf/resolv.conf.d/
下的各种文件。
/etc/resolvconf/resolv.conf.d/base
File containing basic resolver information. The lines in this
file are included in the resolver configuration file even when no
interfaces are configured.
/etc/resolvconf/resolv.conf.d/head
File to be prepended to the dynamically generated resolver
configuration file. Normally this is just a comment line.
/etc/resolvconf/resolv.conf.d/tail
File to be appended to the dynamically generated resolver
configuration file. To append nothing, make this an empty
file. This file is a good place to put a resolver options line
if one is needed, e.g.,
options inet6
尽管在head
文件的顶部有一个警告。
$ cat /etc/resolvconf/resolv.conf.d/head
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
这个警告是为了在构建这些文件时,警告将最终进入这些文件所产生的resolv.conf
文件。因此,你可以很容易地在base
文件中加入上述的nameserver
行,也可以在head
文件中加入。
我也对这个问题感兴趣,我尝试了@sim提出的解决方案。
为了测试它,我把
nameserver 8.8.8.8
在/etc/resolvconf/resolv.conf.d/base
和
nameserver 8.8.4.4
在 /etc/resolvconf/resolv.conf.d/head
中。
然后我重新启动了网络,用
sudo service network-manager restart
结果是,/etc/resolv.conf
看起来像
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.4.4
nameserver 127.0.1.1
而nm-tool
指出dnsserver是
DNS: 208.67.222.222
DNS: 208.67.220.220
这是我的路由器所提供的。 另一方面,挖出的地址告诉我们
;; Query time: 28 msec
;; SERVER: 8.8.4.4#53(8.8.4.4)
如果我是对的,我从这一切得出的结论是
1.只有"head"部分被resolvonf读取:"base"部分被 以某种方式被dnsmasq控制 2. dnsserver实际上被强制到 8.8.4.4,而不管dhcp提供的服务器是什么,但是你失去了dnsmasq提供的缓存,因为请求总是被发送到8.8.4.4。 3.dnsmasq仍然只使用由dhcp提供的dnsserver。
总而言之,它是可行的,但我不认为它是所要求的预期结果。 我认为一个更接近的解决方案是下面这个。编辑
sudo vim /etc/dhcp/dhclient.conf
然后添加
supersede domain-name-servers 8.8.8.8;
结果如下:resolv.conf只包含127.0.0.1,这意味着调用了dnsmasq缓存,nm-tool说
DNS: 8.8.8.8
这意味着,如果搜索的名字不在缓存中,那么它将在8.8.8.8被询问,而不是在由dhcp提供的服务器上。
另一个(也许更好)的选择是使用"prepend"而不是"supersede":这样的话,如果名字没有被8.8.8.8解决,那么请求就会落在另一个服务器上。事实上,nm-tool说
DNS: 8.8.8.8
DNS: 208.67.222.222
DNS: 208.67.220.220