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.

jdthood
  • 12,467
Sam
  • 1,981
  • 2
  • 12
  • 3

21 Answers21

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
jdthood
  • 12,467
tgm4883
  • 7,912
  • 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 '12 at 18:46
  • 1
    I've removed the line. The other option would be to use tail instead of head. – tgm4883 Oct 30 '12 at 11:44
  • 1
    base, head or tail (as of 12.04) are all being rewritten just like resolv.conf, so I can confirm that @jdthood comment is true. So, complete answer would say - do not edit any of resolv.conf files, and just run: sudo /etc/init.d/networking restart As a matter of fact, that will write interface changes to resolv.conf. – tishma Nov 01 '12 at 11:10
  • 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 01 '12 at 13:18
  • 1
    @tishma, that isn't what jdthood said. jdthood said that the last search and domain lines are the only ones used. – tgm4883 Nov 01 '12 at 14:38
  • OK. To be fair, I haven't tried editing head, tail or base until now, being discouraged enough when I saw these lines in /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

    It's true that networking restart doesn't rewrite it, but as long as editing interface works, I wouldn't bother manually editing.

    – tishma Nov 02 '12 at 10:08
  • 1
    If you aren't sure what you are looking at, then that part could be confusing (admittedly, you should know what you are doing if you are editing those files). The reason it says that at the top of the head file, is that is the header it puts in the resolv.conf file. – tgm4883 Nov 07 '12 at 01:53
  • 4
    In 14.04 this answer did nothing for me. – Jay Sullivan Jun 30 '14 at 00:55
  • 1
    @notfed I just tested one of my 14.04 servers and it worked. If you are testing with comments, please note that in /etc/resolvconf/resolv.conf.d/head is the only place I could put a comment that would make it into the resolv.conf file. Putting it in base did nothing. – tgm4883 Jun 30 '14 at 17:35
  • 3
    If all else fails.. $ sudo resolveconf -u didn't seem to change things for me, restarted the machine, presto. – MSpreij Apr 09 '15 at 15:25
  • 1
    @JaySullivan - It should put nameserver 192.168.1.XXX in file /etc/resolvconf/resolv.conf.d/head in Ubuntu 14.04 – Ren Dec 31 '15 at 12:26
  • I'm actually speechless, bs, resolver or whatever it is, just appeared on debian after some update, and I couldn't access internet, and was wtf'ing for 20 minutes what's going on. – holms May 09 '20 at 22:56
  • When I run sudo resolvconf -u I get this error: sudo: resolvconf: command not found. – Gabriel Staples Dec 08 '21 at 19:47
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).

Alex
  • 115
Jamin
  • 491
  • 5
    dhclient rules over any resolvconf settings so this should be the best answer. – Alex R Mar 04 '13 at 09: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.) – Jason Gross Dec 09 '13 at 05:21
  • try systemctl restart networking.service – Pavel Sayekat Dec 08 '17 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
astrajingga
  • 403
  • 1
  • 4
  • 10
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
    This is the actual correct answer! Finally! – user77232 Nov 09 '18 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 '20 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.

roadmr
  • 34,222
  • 9
  • 81
  • 93
  • 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 '12 at 18:56
  • Yep. I added "nameserver 1.2.3.4" in /etc/resolvconf/resolv.conf.d/base – Bastion Jun 13 '19 at 03: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.

  • 2
    Works fine on Debian 8. Elegant solution! – Artur Bodera Oct 15 '15 at 07: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 05 '16 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 '17 at 20:57
  • Thank you so much, this worked for my Kali! – Lancer.Yan Jun 03 '22 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.

Oli
  • 293,335
gorlux
  • 141
  • 3
  • 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 08 '16 at 22:44
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

Pablo Bianchi
  • 15,657
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
  • 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 '12 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 '17 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
tanmay.01
  • 383
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
ustick
  • 180
siz
  • 121
  • 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! – Will Matheson Mar 20 '20 at 20:31
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

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
CBI
  • 11
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

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

Pete
  • 101
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.

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
Pablo Bianchi
  • 15,657
  • 4
    This is very much not the right way to solve the problem. – jdthood Oct 27 '12 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 '14 at 06:09
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

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.

Pablo Bianchi
  • 15,657
Irv
  • 11
-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.

David Foerster
  • 36,264
  • 56
  • 94
  • 147
tesar
  • 1
-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.

BuZZ-dEE
  • 14,223