How do I include lines in resolv.conf that won't get lost on reboot?
Olivia Zamora
I have finally migrated to 12.04 from 7.10. I have one last part to complete but I am stumped. I am using Puppet on each server, and in the past I have included a nameserver address and a search domain name for the puppetmaster in resolv.conf.
search puppetmaster.com
nameserver 192.168.1.XXXIn 12.04 resolv.conf gets overwritten when rebooted. I cannot use a static IP for these, so using the /etc/network/interfaces to help me out is a nill point.
# 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.1Is there a way to get resolvconf to handle this either in the head, tail or base? If there is, are there any examples I can use to tweak on my server.
Any help is much appreciated.
121 Answers
It's probably better to have your DNS server be able to resolve 'puppet' to the right address, and either to have your DHCP server hand out the DNS nameserver address and search list or else (if you have static IP addresses) to have something like the following in /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.10But if you do want to do it via the resolvconf configuration files you will want to edit /etc/resolvconf/resolv.conf.d/base. In that file, put in your info as you would in resolv.conf.
nameserver 192.168.1.XXXThen tell resolvconf to regenerate resolv.conf.
sudo resolvconf -u 13 I think the answer is check your /etc/dhcp/dhclient.conf, i.e. don't request dns-nameservers from your dhcp client.
Then update your /etc/network/interfaces
auto eth0
iface eth0 inet dhcp
dns-search google.com
dns-nameservers dnsserveripThen your resolv.conf will get auto configured the way you want it.
Add to the dns-search and then run a /etc/init.d/networking restart (even though this script's deprecated it still works).
This is likely caused by DHCP configuration when you first installed Ubuntu. Try this 3-step process to handle this auto configuration issue.
First
Edit your interface configuration, which is located in: /etc/network/interfaces
Add this line below iface lo inet loopback:
dns-nameservers yourdns youraltdnsAs an example for Google DNS, you may want to use this:
dns-nameservers 8.8.8.8 8.8.4.4Second
Edit your DHCP configuration file, located at:
/etc/dhcp/dhclient.confMark the syntax as a comment using # on every line
or simply remove every request name-server.
In 16.04, you may not be required to make any changes here.
Third
Restart your networking by using this command :
/etc/init.d/networking restartIn 16.04:
sudo ifdown -a
sudo ifup -a 3 As many other answers state this has to do with resolvconf being installed in your system.
So the best way to keep something in resolv.conf that won't get lost on reboot is to include it in resolvconf configuration files that are in:
/etc/resolvconf/resolv.conf.d/In there go for the head file. Whatever you put there will be written at the top of /etc/resolv.conf
So everything will go to something like this:
# echo nameserver 8.8.8.8 >> /etc/resolvconf/resolv.conf.d/head
# resolvconf --enable-updates
# resolvconf -u 2 Please look at resolvconf's man page. You can force inclusion of certain DNS settings by creating e.g. /etc/resolvconf/resolv.conf.d/base:
/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.There are other special files (head and tail), these may help you achieve what you want.
2For me, the above answers were inadequate for the following reasons:
- I'm not using
resolvconf, just plain/etc/resolv.conf. - Using
chattr +ito lock downresolv.confseems too hacky. I need Puppet to be free to make changes when necessary. - AFAIK, editing
/etc/network/interfacesdoesn't preventresolv.conffrom being overwritten; it simply specifies the name servers that should be written. For me, specifying the name servers wasn't the point. I'm trying to setoptions timeout:1andoptions attempts:1in myresolv.conffile.
The best solution I found overrides the default behavior of dhclient using its documented hooks.
Create a new file at /etc/dhcp/dhclient-enter-hooks.d/nodnsupdate with the following contents:
#!/bin/sh
make_resolv_conf() { :
}Then make the file executable:
chmod +x /etc/dhcp/dhclient-enter-hooks.d/nodnsupdateNow when dhclient runs -- either on reboot or when you manually run sudo ifdown -a ; sudo ifup -a -- it loads this script nodnsupdate. This script overrides an internal function called make_resolv_conf() that would normally overwrite resolv.conf and instead does nothing.
This worked for me on Ubuntu 12.04.
4This may just be some weird quirk in my machine, but someone else might have the same corner case.
I tried numerous ways to get my ISP nameservers included in /etc/resolv.conf with no success:
I included them in
/etc/network/interfacesand restarted networking. They didn't show up in/etc/resolv.conf.I put them in
/etc/resolv.confexplicitly, but of course they got overwritten. They did show up in/run/resolvconf/interface/eth0.inet, but never made it to/etc/resolv.conf.I tried configuring resolvconf for dynamic updates. No change.
Finally I read somewhere that if the local machine (127.0.0.1) shows up in /etc/resolv.conf any other nameservers are not included.
In desperation I edited /run/resolvconf/interface/lo.named, deleted the only line in it (nameserver 127.0.0.1) and restarted: ifdown eth0 && ifup eth0.
/etc/resolv.conf then included my ISP nameservers for the first time! I ran service network-manager restart to see if it was stable and /etc/resolv.conf still includes my ISP nameservers. Rebooted just to make sure and it's still there but /run/resolvconf/interface/lo.named got reset to: nameserver 127.0.0.1.
Curiously restarting networking still works: /etc/resolv.conf still contains my ISP nameservers. I can't explain this (can someone?) but this might help someone stuck in the same spot.
add your nameserver to file /etc/resolvconf/resolv.conf.d/head. The file contains message that you had received:
that file should looks like this after adding 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 2 add on the last line eg:
nameserver 8.8.8.8Open up a terminal and type
sudo chattr +i /etc/resolv.confthe +i takes care that the file wont be reseted on a boot.
To undo the above
sudo chattr -i /etc/resolv.confFor more
man chattr Add entries in /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/headand run following command
sudo resolvconf -u 1 Using resolvconf and disabling systemd-resolved.service (this works for me ubuntu 19.04):
Install
resolvconfsudo apt install resolvconfAdding nameservers
Open
/etc/resolvconf/resolv.conf.d/basewith your text editor, I usevim.sudo vim /etc/resolvconf/resolv.conf.d/basethen, add the nameservers inside the opened file, ex.:
nameserver 1.1.1.1 nameserver 1.0.0.1Updating
resolv.confsudo resolvconf -uDisable
systemd-resolved.serviceand reboot.nameserver 127.0.0.53is not written to/etc/resolv.conf.sudo systemctl disable systemd-resolved.service sudo reboot
Done!
1The other solutions did not work for me on my Fedora 20 system. My particular problem was that the "search" line in /etc/resolv.conf was being overwritten. Here is what fixed it. (This assumes that NetworkManager is producing the linesearch rn.yourcompany.com and you want to have it be search rn.yourcompany.com yourcompany.com intnet.yourcompany.com:
1.Use the "ifconfig" command to find out what interface is of interest:
$ ifconfig : :
em2: <this was the one which was connected>2.Become root and change to the system configuration network devices directory:
$ sudo su -[sudo]
password for youruser:
# cd /etc/sysconfig/networking/devices`- Use your favorite available editor to add a
Domainline with the additional domains to search:
DOMAIN="yourcompany.com intnet.yourcompany.com"
Save, logout, and log back in. NetworkManager should now have the line in \etc\resolve.conf:
search rn.yourcompany.com yourcompany.com intnet.yourcompany.com 1 For Ubuntu Server 18 Netplan is the new utility for configuring networking.
# cd /etc/netplanThen edit nameservers addresses entry in yaml file (use correct indentation). For example, if you use Google's DNS servers :
nameservers: addresses: [8.8.8.8,8.8.4.4]To restart the service
# netplan applySee
1If you are using DHCP, edit /etc/dhcp/dhclient.conf to add additional DNS servers:
prepend domain-name-servers 12.34.56.78, 12.34.56.79;The DHCP client overwrites the dns-nameservers in etc/network/interfaces and I think in /etc/resolvconf/resolv.conf.d/base too.
This worked for me in Ubuntu server 14.04.3.
See the Debian NetworkConfiguration Wiki for details.
Just backup your resolve.conf and delete the resolvconf pacage and edit the /etc/resolv.conf file to whatever you want.
apt-get remove -y resolvconf
echo 'nameserver 8.8.8.8' > /etc/resolv.confWe should have right to choose to not use bad software like resolvconf.
By the way, search field in the /etc/resolv.conf is useless.
I found this the simplest fix. If you have resolv.conf and resolvconf files they will step on one another. You need to remove the resolv.conf file that get and overwrite every time you do a reboot.
Put the nameserver 1.1.1.1 1.0.0.1 at the bottom of the resolvconf file and then run
sudo rm /etc/resolv.confTo get rid of the file. Then do a restart and everything will work.
1Mi solution on 12.04:
I noticed that if you add the dns-nameserver in interfaces that do not take the name resolution servers
To make resolv.conf not change when we edit manually do this in the terminal:
sudo resolvconf –disable-updatesafter:
sudo resolvconf -a eth0 # or your network interfacethen manually edit /run/resolvconf/resolv.conf adding a maximum of two DNS servers.
Then restart the service:
sudo /etc/init.d/networking restart 2 Another way is to configure resolvconf(8) to use the local search domain and nameservers before querying the DHCP-supplied search domains and nameservers. That is accomplished by creating /etc/resolvconf.conf (sudo nano /etc/resolvconf.conf):
search_domains="nono.com"
name_servers="1.1.1.1 1.0.0.1"Thank you Brian Cunnie at pivotal blog
none of the above worked for me on Ubuntu 20.4I ended up adding the following entry to my root's crontab
@reboot sleep 20 && /root/restoreDNS>/etc/restoreDNS.out 1>2&
basically after 20 seconds of booting up; it executes a shell script that copies my version onto /etc/resolv.conf and it was good for me.
Just put a
dns-search google.com && dns-nameservers (sample: 8.8.8.8)command on your /etc/network/interfaces configuration. then restart your network.
it should work.
That configuration is declared in /etc/default/bind9
RESOLVCONF=no|yesno = don't apply the condition in the init.d bind9
yes, or other value = override resolv.conf
This problem accures when you install bind9 and don't care about check all confs.