198

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.XXX

In 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.1

Is 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.

1
  • 3
    resolvconf is a bad software, just delete it, and manage the /etc/resolv.conf with the old way.
    – bronze man
    Aug 28, 2019 at 1:49

21 Answers 21

145

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.10

But 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.XXX

Then tell resolvconf to regenerate resolv.conf.

sudo resolvconf -u
13
  • 13
    Although this answer has votes, and the first part is more or less correct, the second part of the answer is incorrect. (1) Do not put a "search" line in /etc/resolvconf/resolv.conf.d/head. If you put a "search" line there, this line will be ignored if resolvconf includes a "search" line in the dynamic part of the resolv.conf file. The glibc resolver ignores all but the last "search" or "domain" line. See resolv.conf(5). (2) If the resolvconf configuration is changed you should not restart the resolvconf job but just run an update, "resolvconf -u".
    – jdthood
    Oct 27, 2012 at 18:46
  • 1
    I've removed the line. The other option would be to use tail instead of head.
    – tgm4883
    Oct 30, 2012 at 11:44
  • 6
    @tishma: Hi. First, to prevent any misunderstanding: nothing writes to the base, head or tail files. Nothing writes to any files in /etc/resolvconf/resolv.conf.d/ at run time. These files are read by resolvconf which assembles their content into the file that it writes --- /run/resolvconf/resolv.conf --- to which the symlink /etc/resolv.conf points. Second, concerning what to do after dns-* options in /etc/network/interfaces are changed. Do not run "/etc/init.d/networking restart"; that is now deprecated. Instead ifdown the interface in question and ifup it again.
    – jdthood
    Nov 1, 2012 at 13:18
  • 4
    In 14.04 this answer did nothing for me. Jun 30, 2014 at 0:55
  • 3
    If all else fails.. $ sudo resolveconf -u didn't seem to change things for me, restarted the machine, presto.
    – MSpreij
    Apr 9, 2015 at 15:25
39

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 dnsserverip

Then 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).

3
  • 5
    dhclient rules over any resolvconf settings so this should be the best answer.
    – Alex R
    Mar 4, 2013 at 9:35
  • 6
    /etc/init.d/networking restart did not work on my machine, but sudo ifdown -a and then sudo ifup -a did. (Also, it took me a bit to realize I had to replace dnsserverip with something like 8.8.8.8; I feel a bit silly.) Dec 9, 2013 at 5:21
  • try systemctl restart networking.service Dec 8, 2017 at 14:42
23

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 youraltdns

As an example for Google DNS, you may want to use this:

dns-nameservers 8.8.8.8 8.8.4.4

Second

Edit your DHCP configuration file, located at:

/etc/dhcp/dhclient.conf

Mark 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 restart

In 16.04:

sudo ifdown -a
sudo ifup -a
3
  • 3
    /etc/init.d/networking restart did not work on my machine, but sudo ifdown -a and then sudo ifup -a did. Dec 9, 2013 at 5:22
  • This is simple and it works even though it is a bit hacky! Problem with things like ubuntu is having 1000 ways to do one thing! Jul 18, 2016 at 12:52
  • And then you can check that /etc/resolv.conf contains those 2 new DNS entries on the first useful lines. Dec 20, 2016 at 16:31
17

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
  • 2
    This is the actual correct answer! Finally!
    – user77232
    Nov 9, 2018 at 19:12
  • Thanks for this answer. TIP: if you don't want these to be the primary (say you are on AWS) then append these to /etc/resolvconf/resolv.conf.d/base instead of /etc/resolvconf/resolv.conf.d/head.
    – Mike Q
    Jan 14, 2020 at 17:57
9

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.

2
  • 4
    You can add lines to /etc/resolvconf/resolv.conf.d/base, but because every nameserver is accessible via an interface and is accessible only when that interface is up, it is best to associate the nameserver information with that interface. If the interface is configured with ifup, this means: put the info on "dns-search" and "dns-nameservers" lines in /etc/network/interfaces stanzas. If the interface is configured via DHCP then this means: configure the DHCP server to supply search names and nameserver addresses to clients. Etc. Use the "base" file only as a temporary hack or as a last resort.
    – jdthood
    Oct 27, 2012 at 18:56
  • Yep. I added "nameserver 1.2.3.4" in /etc/resolvconf/resolv.conf.d/base
    – Bastion
    Jun 13, 2019 at 3:50
7

For me, the above answers were inadequate for the following reasons:

  • I'm not using resolvconf, just plain /etc/resolv.conf.
  • Using chattr +i to lock down resolv.conf seems too hacky. I need Puppet to be free to make changes when necessary.
  • AFAIK, editing /etc/network/interfaces doesn't prevent resolv.conf from 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 set options timeout:1 and options attempts:1 in my resolv.conf file.

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/nodnsupdate

Now 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.

4
  • 2
    Works fine on Debian 8. Elegant solution! Oct 15, 2015 at 7:19
  • 1
    just for completeness: manpage dhclient-script holds the information about the DHCP client network configuration script mentioned an the answer above.
    – hecke
    Feb 5, 2016 at 19:36
  • This didn't work on 16.04, I added things to /etc/network/interfaces.d too, with no effect, added an empty override of make_resolv_conf recommended here, no effect.... but did not modify /etc/dhcp/dhclient.conf -- do I really need to modify a static config file to fix this?
    – silverjam
    Jan 30, 2017 at 20:57
  • Thank you so much, this worked for my Kali!
    – Lancer.Yan
    Jun 3, 2022 at 15:58
3

This 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/interfaces and restarted networking. They didn't show up in /etc/resolv.conf.

  • I put them in /etc/resolv.conf explicitly, 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.

1
  • This is probably caused by dnsmasq. You can simply remove it using apt-get remove dnsmasq or update config in /etc/dnsmasq.conf.
    – Tombart
    Jan 8, 2016 at 22:44
2

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
  • 3
    Adding lines to /etc/resolvconf/resolv.conf.d/head is a poor solution, even worse than adding lines to /etc/resolvconf/resolv.conf.d/base. The correct solution for interfaces configured using ifup is to add "dns-search" and "dns-nameservers" lines to stanzas in /etc/network/interfaces. See also my comments on the other answers.
    – jdthood
    Oct 27, 2012 at 19:01
  • 3
    It's the only thing that actually worked for me... and it's seems hard to figure why none of the "correct" solutions are working.
    – silverjam
    Jan 30, 2017 at 20:54
2

add on the last line eg:

nameserver 8.8.8.8

Open up a terminal and type

sudo chattr +i /etc/resolv.conf

the +i takes care that the file wont be reseted on a boot.

To undo the above

sudo chattr -i /etc/resolv.conf

For more

man chattr
2

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/head

and run following command

sudo resolvconf -u
1
  • Putting search ___ and nameserver ___ in that file was the thing that finally worked for me to get my openVPN connection to an internal network to use that network's DNS! Mar 20, 2020 at 20:31
2

Using resolvconf and disabling systemd-resolved.service (this works for me ubuntu 19.04):

  1. Install resolvconf

    sudo apt install resolvconf
    
  2. Adding nameservers

    Open /etc/resolvconf/resolv.conf.d/base with your text editor, I use vim.

    sudo vim /etc/resolvconf/resolv.conf.d/base
    

    then, add the nameservers inside the opened file, ex.:

    nameserver 1.1.1.1
    nameserver 1.0.0.1
    
  3. Updating resolv.conf

    sudo resolvconf -u
    
  4. Disable systemd-resolved.service and reboot. nameserver 127.0.0.53 is not written to /etc/resolv.conf.

    sudo systemctl disable systemd-resolved.service
    sudo reboot
    

Done!

Source

1
  • Could you specify for which Ubuntu version you did this? The original question refers to ubuntu 12.04, since long deprecated. Jun 27, 2019 at 13:15
1

The 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 line search 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`
  1. Use your favorite available editor to add a Domain line 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
  • 3
    Not to rain on your parade, but this is Ask Ubuntu, and so Fedora is off-topic.
    – Flimm
    Dec 3, 2014 at 0:05
1

For Ubuntu Server 18 Netplan is the new utility for configuring networking.

# cd /etc/netplan

Then 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 apply

See https://netplan.io/

https://www.techrepublic.com/article/how-to-set-dns-nameservers-in-ubuntu-server-18-04/

1
0

If 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.

0

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.conf

We should have right to choose to not use bad software like resolvconf.

By the way, search field in the /etc/resolv.conf is useless.

0

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.conf

To get rid of the file. Then do a restart and everything will work.

1
  • resolvconf is a directory, not a file (i.e. /etc/resolvconf)
    – mahemoff
    Apr 26, 2020 at 23:33
0

Mi solution on 12.04:

I noticed that if you add the dns-nameserver in interfaces that do not take the name resolution servers

resolvconf man page

To make resolv.conf not change when we edit manually do this in the terminal:

sudo resolvconf –disable-updates

after:

sudo resolvconf -a eth0 # or your network interface

then manually edit /run/resolvconf/resolv.conf adding a maximum of two DNS servers.

Then restart the service:

sudo /etc/init.d/networking restart
2
  • 4
    This is very much not the right way to solve the problem.
    – jdthood
    Oct 27, 2012 at 18:57
  • 1
    @jdthood as someone browsing this can you elaborate WHY this is not the right way? It seems logical to me, but I do not know anything about this.
    – ErikPerik
    Mar 25, 2014 at 6:09
0

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

0

none of the above worked for me on Ubuntu 20.4 I 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.

-1

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.

-2

That configuration is declared in /etc/default/bind9

RESOLVCONF=no|yes

no = 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .