如何在resolv.conf中包含不会在重启时丢失的行?
我终于从7.10迁移到了12.04。 我有最后一部分要完成,但我很难过。 我在每台服务器上使用Puppet,过去我在resolv.conf中为puppetmaster包含了一个名称服务器地址和一个搜索域名。
search puppetmaster.com nameserver 192.168.1.XXX
在12.04中,resolv.conf在重新启动时被覆盖。 我不能为这些使用静态IP,因此使用/ etc / network / interfaces来帮助我是一个小问题。
# 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.0.1
有没有办法让resolvconf在head,tail或base中处理这个问题? 如果有,是否有任何示例我可以用来调整我的服务器。
任何帮助深表感谢。
最好让你的DNS服务器能够将’puppet’解析到正确的地址,并让你的DHCP服务器分发DNS名称服务器地址和搜索列表,否则(如果你有静态IP地址)有类似的东西/ etc / network / interfaces中的以下内容。
iface eth0 inet static address 192.168.3.3 netmask 255.255.255.0 gateway 192.168.3.1 dns-search example.com dns-nameservers 192.168.3.45 192.168.8.10
但是,如果您确实希望通过resolvconf配置文件来执行此操作,则需要编辑/etc/resolvconf/resolv.conf.d/base
。 在该文件中,像在resolv.conf
一样输入您的信息。
nameserver 192.168.1.XXX
然后告诉resolvconf重新生成resolv.conf
。
sudo resolvconf -u
我想答案是检查你的/etc/dhcp/dhclient.conf
,即不要从你的dhcp客户端请求dns-nameservers
。
然后更新/etc/network/interfaces
auto eth0 iface eth0 inet dhcp dns-search google.com dns-nameservers dnsserverip
然后你的resolv.conf
将按照你想要的方式自动配置。
添加到dns-search
,然后运行/etc/init.d/networking restart
(即使这个脚本已弃用,它仍可正常工作)。
这可能是您第一次安装Ubuntu时由DHCP配置引起的。 尝试这个3步过程来处理此自动配置问题。
第一
编辑您的接口配置,该配置位于: /etc/network/interfaces
在iface lo inet loopback
下面添加以下行:
dns-nameservers yourdns youraltdns
作为Google DNS的示例,您可能希望使用以下内容:
dns-nameservers 8.8.8.8 8.8.4.4
第二
编辑DHCP配置文件,位于:
/etc/dhcp/dhclient.conf
使用#
在每一行标记语法作为注释,或者只删除每个请求名称 – 服务器。 在16.04,您可能不需要在此处进行任何更改。
第三
使用以下命令重新启动网络:
/etc/init.d/networking restart
在16.04:
sudo ifdown -a sudo ifup -a
请查看resolvconf
的手册页。 您可以通过创建例如/etc/resolvconf/resolv.conf.d/base
强制包含某些DNS设置:
/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.
还有其他特殊文件(头部和尾部),这些可以帮助您实现您想要的。
对我来说,上述答案不充分,原因如下:
- 我没有使用
resolvconf
,只是简单的/etc/resolv.conf
。 - 使用
chattr +i
锁定resolv.conf
似乎太hacky了。 我需要Puppet在必要时自由地进行更改。 - AFAIK,编辑
/etc/network/interfaces
不会阻止resolv.conf
被覆盖; 它只是指定应该写的名称服务器。 对我来说,指定名称服务器不是重点。 我正在尝试设置options timeout:1
和options attempts:1
在我的resolv.conf
文件中为1。
我发现的最佳解决方案使用其记录的钩子覆盖了dhclient
的默认行为。
在/etc/dhcp/dhclient-enter-hooks.d/nodnsupdate
创建一个新文件,其中包含以下内容:
#!/bin/sh make_resolv_conf() { : }
然后使文件可执行:
chmod +x /etc/dhcp/dhclient-enter-hooks.d/nodnsupdate
现在当dhclient运行时 – 无论是重启还是手动运行sudo ifdown -a ; sudo ifup -a
sudo ifdown -a ; sudo ifup -a
– 它加载此脚本nodnsupdate
。 这个脚本会覆盖一个名为make_resolv_conf()
的内部函数,它通常会覆盖resolv.conf
而不会执行任何操作。
这在Ubuntu 12.04上对我有用。
正如许多其他答案所述,这与在系统中安装resolvconf
。
因此,在resolv.conf
中保留一些不会在重启时丢失的最佳方法是将其包含在以下的resolvconf配置文件中:
/etc/resolvconf/resolv.conf.d/
在那里去找head
文件。 无论你放什么,都会写在/etc/resolv.conf
的顶部
所以一切都将是这样的:
# echo nameserver 8.8.8.8 >> /etc/resolvconf/resolv.conf.d/head # resolvconf --enable-updates # resolvconf -u
将您的名称服务器添加到文件/etc/resolvconf/resolv.conf.d/head。 该文件包含您收到的消息:
添加8.8.8.8后,该文件应如下所示
root@hvnatvcc: ~ # 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 nameserver 8.8.8.8
这可能只是我的机器中的一些奇怪的怪癖,但其他人可能有相同的角落情况。
我尝试了很多方法来获取/etc/resolv.conf中包含的ISP名称服务器但没有成功:
-
我将它们包含在
/etc/network/interfaces
并重新启动网络。 它们没有显示在/etc/resolv.conf
。 -
我明确地将它们放在
/etc/resolv.conf
,但当然它们被覆盖了。 它们确实显示在/run/resolvconf/interface/eth0.inet
,但从未进入/etc/resolv.conf
。 -
我尝试为动态更新配置resolvconf。 没变。
最后我在某处读到如果本地机器(127.0.0.1)出现在/etc/resolv.conf
中,则不包括任何其他名称服务器。
无奈/run/resolvconf/interface/lo.named
我编辑了/run/resolvconf/interface/lo.named
,删除了它中唯一的一行( nameserver 127.0.0.1
)并重新启动: ifdown eth0 && ifup eth0
。
/etc/resolv.conf然后第一次包括我的ISP名称服务器! 我运行service network-manager restart
以查看它是否稳定,并且/etc/resolv.conf
仍包含我的ISP名称服务器。 重新启动只是为了确保它仍然存在, 但 /run/resolvconf/interface/lo.named
重置为: nameserver 127.0.0.1
。
好奇地重启网络仍然有效: /etc/resolv.conf
仍然包含我的ISP名称服务器。 我无法解释这个问题(有人可以吗?)但这可能有助于某些人陷入同一地点。
在我的Fedora 20系统上,其他解决方案对我不起作用。 我的特殊问题是/etc/resolv.conf中的“搜索”行被覆盖了。 这是修复它的原因。 (这假设NetworkManager正在生成行search rn.yourcompany.com
并且您希望将其search rn.yourcompany.com yourcompany.com intnet.yourcompany.com
:
1.使用“ifconfig”命令找出感兴趣的接口:
$ ifconfig : : em2:
2.成为root并切换到系统配置网络设备目录:
$ sudo su -[sudo] password for youruser: # cd /etc/sysconfig/networking/devices`
- 使用您最喜欢的可用编辑器添加带有要搜索的其他域的
Domain
行:
DOMAIN="yourcompany.com intnet.yourcompany.com"
保存,注销并重新登录.NetworkManager现在应该在\etc\resolve.conf
有一行:
search rn.yourcompany.com yourcompany.com intnet.yourcompany.com
在最后一行添加例如:
nameserver 8.8.8.8
打开终端并输入
sudo chattr +i /etc/resolv.conf
+ i注意文件不会在启动时重置。
撤消上述内容
sudo chattr -i /etc/resolv.conf
更多
man chattr
英文翻译:
我解决了 “只有12.4”
我注意到,如果在不接受名称解析服务器的接口中添加dns-nameserver,感谢此处找到的帮助解决了问题。
http://manpages.ubuntu.com/manpages/lucid/man8/resolvconf.8.html
为了使我们在手动编辑时不改变resolv.conf,请在终端中执行此操作:
sudo resolvconf –disable-updates
后:
sudo resolvconf -a eth0 # or your network Interfas
然后手动编辑/run/resolvconf/resolv.conf
最多添加两个DNS服务器。 谢谢PS别忘了重启:
sudo /etc/init.d/networking restart
原文:
solo ubuntu 12.4
Mi解决方案。
他visto que si agregas los dns-nameserver en interfaces esta no toma los server de resolucion de nombres
Gracias a la ayuda encontrada en esta pagina he resuelto el problema。
http://manpages.ubuntu.com/manpages/lucid/man8/resolvconf.8.html
Para hacer que resolv.conf no cambie cuando la editamos manualmente hacemos esto en la terminal:
sudo resolvconf –disable-updates
despues:
sudo resolvconf -a eth0 # o tu interfas de red
luego editamos manualmente /run/resolvconf/resolv.conf
agregando un maximo de 2 DNS服务器。 saludos PD no olvidar reiniciar:
sudo /etc/init.d/networking restart
如果您使用的是DHCP,请编辑/etc/dhcp/dhclient.conf
以添加其他DNS服务器:
prepend domain-name-servers 12.34.56.78, 12.34.56.79;
DHCP客户端覆盖etc/network/interfaces
的dns-nameservers
,我想在/etc/resolvconf/resolv.conf.d/base
也是如此。
这在Ubuntu服务器14.04.3中适用于我。
有关详细信息 ,请参阅Debian NetworkConfiguration Wiki 。
我发现这是最简单的修复。 如果您有resolv.conf和resolvconf文件,它们将相互踩踏。 您需要删除每次重新启动时都会覆盖的resolv.conf文件。 将名称服务器8.8.8.8 8.8.4.4放在resolvconf文件的底部,然后运行命令sudo rm /etc/resolv.conf
来删除该文件。 然后重新启动,一切都会工作。
在/etc/resolvconf/resolv.conf.d/head
添加条目
echo 'search puppetmaster.com' | sudo tee -a /etc/resolvconf/resolv.conf.d/head echo 'nameserver 192.168.1.XXX' | sudo tee -a /etc/resolvconf/resolv.conf.d/head
并运行以下命令
sudo resolvconf -u
刚放一个
dns-search google.com && dns-nameservers (sample: 8.8.8.8)
/etc/network/interfaces
配置中的命令。 然后重启你的网络。
它应该工作。
该配置在/etc/default/bind9
RESOLVCONF=no|yes
no =不在init.d
bind9
应用条件
是 ,或其他值=覆盖resolv.conf
安装bind9
并且不关心检查所有confs时会出现此问题。