251

Is /etc/resolv.conf useless in Ubuntu 12.04 LTS (Precise Pangolin)?

I see that the DNS server information is stored in NetworkManager now. The nmcli command line tool can list that for you.

If I want to add one more DNS server, will adding it to /etc/resolv.conf by using the resolvconf package help?

1
  • 2
    I think the command mentioned above should be nm-tool, not nmcli. Jul 19, 2013 at 17:40

20 Answers 20

159

If /etc/resolv.conf contains nameserver 127.0.0.1 then adding entries to /etc/resolvconf/resolv.conf.d/tail won't really do anything useful.

If you are using NetworkManager then you should instead statically add nameserver addresses via network indicator: Edit Connections... | Edit... | IPv4 Settings | Additional DNS servers.

If you really want to add more entries to /etc/resolv.conf, create a /etc/resolvconf/resolv.conf.d/tail and add them there.

As with every Ubuntu release, it's recommended to read the Ubuntu Release Notes, available here:

The Desktop and Common Infrastructure sections contain a link to

7
  • 1
    @stgraber, please comment on the alternative approach I found and posted below. How does it compare with adding an /etc/resolvconf/resolv.conf.d/tail file or using Network Manager? Thanks. May 8, 2012 at 23:02
  • 3
    @stgraber What do you recommend for Ubuntu Server? I've noticed from your blog and comments that Network-Manger seems to be the best way to manage this, but doing an install of network-manager on my system would install a whole bunch of things I don't need (i.e. GUI stuff). Jun 22, 2012 at 3:17
  • 1
    What a bout adding entries to /etc/resolvconf/resolv.conf.d/head (as per your blogpost)?
    – sup
    Mar 12, 2013 at 16:15
  • 3
    @AveryChan, use the "third approach" suggested by @randallcook—just add dns-nameservers to your eth0 section in /etc/network/interfaces.
    – mrm
    Aug 26, 2013 at 5:15
  • 1
    /etc/resolvconf/update.d/libc on Ubuntu 14.04.1 server : # Set TRUNCATE_NAMESERVER_LIST_AFTER_LOOPBACK_ADDRESS=no # to allow additional nameserver addresses to be listed in # resolv.conf after an initial loopback address 127.* or ::1. (set it in /etc/default/resolvconf, as line 23 & 24 of /etc/resolvconf/update.d/libc show. Feb 14, 2015 at 4:07
150

I found another approach here that involves adding a line like the one below to /etc/dhcp/dhclient.conf:

prepend domain-name-servers x.x.x.x, y.y.y.y;

Likewise, I found a third approach here that involves adding lines to /etc/network/interfaces:

auto eth0
iface eth0 inet static
    . . .
    dns-nameservers 8.8.8.8 8.8.4.4

Update: Here is the official documentation for the third approach.

9
  • 18
    I voted for this because the third approach is by far the simplest way to do this.
    – Billy Moon
    Jul 17, 2012 at 9:27
  • 2
    @billy I could not get the third method to work at all.. only the first one listed here, editing the dclient.conf, worked for me. Oct 17, 2012 at 21:49
  • 9
    Note: Editing dhclient.conf only has any effect if dhclient is used (and it is used by both ifup and NetworkManager!) Editing /etc/network/interfaces only has any effect if ifup is used. Configuring NetworkManager connections using the NetworkManager connection editor only has any effect if NetworkManager is used.
    – jdthood
    Oct 29, 2012 at 14:26
  • 2
    If I am not mistaken the third approach adds another nameserver AFTER the ones returned by DHCP. If you want it to come first then one of the other options is more appropriate. I used /etc/resolvconf/resolv.conf.d/head (after overcoming the misleading "DO NOT EDIT" comment in there :) )
    – starfry
    Sep 15, 2014 at 10:46
  • 1
    @Mr.Hyde, those aren't command line commands, they are lines to add to /etc/network/interfaces. Jul 7, 2015 at 5:49
117

It sounds like you are talking about the resolvconf package.

Install the resolvconf package.

Run

cd /etc/resolvconf/resolv.conf.d
sudo cp -p head head.orig  #backup copy, always do this
sudo nano head

The top of the file is a scary warning. The file /etc/resolv.conf is autogenerated from the contents of this file; the warning is there so it will get put in /etc/resolv.conf when /etc/resolv.conf is generated. To the end of the file, add

nameserver <ip_of_nameserver>

Press Ctrl x and answer yes to saving the file. To finish up, regenerate /etc/resolv.conf so the changes are applied right now:

sudo resolvconf -u

Then check the contents of /etc/resolv.conf to see the line you added is now there. Further, it will still be there the next time your machine boots or your network service is restarted, whichever comes first.

2
  • 1
    The information about the file warning is just what I needed. I was under the impression that I shouldn't edit the file! Thanks!
    – Xunnamius
    Oct 22, 2017 at 5:51
  • 1
    This is the best answer for me!
    – Siwei
    Nov 1, 2018 at 11:23
33

Below I will show you the best way that I have found since I run Ubuntu Server edition and use ifup rather than NetworkManager.

Actually for me they made this easier :) by putting it all into the /etc/network/interfaces file. The same configurations that you would have written to resolv.conf can now be in the same file as your network adapter configurations as in the example below:

# The loopback network interface
auto lo
iface lo inet loopback

# The Primary Network Interface
auto eth0
iface eth0 inet static
        address 192.168.1.2
        netmask 255.255.255.0
        network 192.168.0.0
        broadcast 192.168.1.255
        gateway 192.168.1.1
        dns-nameserver 75.75.75.75 
        dns-nameserver 75.75.76.76
        dns-search local
        wildcard mask 0.0.0.255
        cidr prefix size /24
        cidr notation 192.168.1.0/24
        first host 192.168.1.255
        last host 192.168.1.254
        mac address J7:836:737:727:gsgd837:g645

I hope this helps out and makes it easier as it does for me, now we can create static IP addresses and add in nameservers and dns domain all in one file :)

3
  • 1
    Thank you. This has become my favorite method as well. An off-topic note: it's not necessary to specify the 'network' and 'broadcast' addresses since they're automatically calculated from the 'address' and the 'netmask'. All you really need are 'address', 'netmask' and 'gateway'. Feb 21, 2014 at 10:16
  • 1
    This is the cleanest answer. Just make sure to spell the dns-* keywords correctly. Misspelled "dns-nameservers" was my actual problem. Apr 13, 2018 at 21:28
  • Weird. The man page for resolvconf states than dns-nameservers is allowed. Yet my machines had that but stopped working. Have now changed to multiple dns-nameserver entries. Feb 18, 2023 at 14:56
23

Mine is running Ubuntu Server 12.04. I have made the following changes and rebooted the server (typically this can be done by only bringing down the network interface, that is, ifdown eth0 or ifup eth0).

In file /etc/resolvconf/resolv.conf.d/base I added the following entries:

nameserver 8.8.8.8
nameserver 8.8.4.4

I hope the above helps!

12

You can use NetworkManager as stated in Frank's answer, but if you would rather manually edit /etc/resolv.conf, you can do so by deleting it (it's actually a symlink) and then creating a new plain file with the content you want. The resolvconf utility only ever writes to the file /run/resolvconf/resolv.conf.

9

Note: This answer was for a rather different, pre-merge version of the Question, with a focus only on pre-pending a desired name-server.

This works pre-12.04:

Edit /etc/dhcp3/dhclient.conf and add :
prepend domain-name-servers 127.0.0.1;

(In fact, this line is already present ; all you need to do is un-comment it.)

6
  • Does this work if you don't have any addresses assigned by DHCP?
    – Azendale
    Jun 30, 2011 at 23:34
  • If you want to learn more about the dhclient configuration you can do a man dhclient.conf to access the dhclient man page. (This is not a response to Azendale's question.) Aug 31, 2011 at 16:01
  • This answer is completely wrong. First of all, the question submitter wants to add the address of an external namserver, not the address 127.0.0.1. The interface configurer for an external interface is not the place to add an address for an internal nameserver. Second, the file is at /etc/dhcp, not at /etc/dhcp3. Third, since Ubuntu 12.04 such things are configured using resolvconf.
    – jdthood
    Oct 3, 2013 at 8:29
  • 1
    First of all -- the original question was dramatically changed by moderator merging, so my answer of course looks strange. The dhcp3 file was the location when I answered this in 2011, and even if it's not ideal, it worked. I've used this solution on multiple machines. Obviously, you would change the loopback address to the one you want to prepend. I assume most people know what a loopback is, even if they haven't read RFC6890.
    – belacqua
    Oct 4, 2013 at 15:11
  • @jdthood Why not add your own answer? Personally, I was not familiar with the bsd-ish resolvconf way of doing things. I preferred the direct /etc/resolv.conf approach, and I'm not sure what the advantage of the new Rube Goldberg system is. (Likewise with /etc/motd).
    – belacqua
    Oct 4, 2013 at 15:46
9

resolvconf is useless. I'm on a Linux server and my IP address is static and my DNS servers are static. I do not need resolvconf or NetworkManager.

Keeping it simple is my policy when working on a server. The less complexity, the easier it'll be to manage/fix when things break.

So I did aptitude purge resolvconf and manually made sure /etc/resolv.conf isn't a symlink and just created a static file. Just in case a program tries to change the file, I did chattr +i (immutable) to /etc/resolv.conf as a precaution.

2
  • 4
    As resolvconf is part of the base system, by deleting it you are asking for trouble in the future, at upgrade time.
    – jdthood
    Oct 29, 2012 at 14:30
  • 5
    I agree with both the comment and the answer: consider how many problems this has caused, and how many support hours have gone into fixing this "problem", and how many IT staff have spent time looking for answers. Simple is best - I don't need my servers to be changing the DNS to something I don't want.
    – Mei
    Jul 30, 2014 at 16:40
7

Most of the above assumes you have a pristine system, but reality is often such that you have been installing different dhcp clients, disabled the network manager in certain previous version of Ubuntu, etc. It might be worthwhile to know the following. I've been using dnsmasq for a while, but deinstalled it. Currently my system had the following contents of /etc/resolv.conf (which is a symbolic link to /run/resolvconf/resolv.conf on my system):

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

It doesn't matter how often I run sudo resolvconf -u or sudo dpkg-reconfigure resolvconf, it didn't update the nameserver to the ones I set in the NetworkManager. The information resolvconf does use from the NetworkManager was present:

cat /run/resolvconf/interface/NetworkManager  
nameserver 8.8.4.4
nameserver 8.8.8.8

However, it turns out to be the case that if there are multiple files in this directory, this file might not be used at all. The culprit was a file not removed with the deinstallation of dnsmasq:

cat /run/resolvconf/interface/lo.dnsmasq                        
nameserver 127.0.0.1

Just removing this file (and running sudo resolvconf -u afterwards) solved my dns troubles:

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 8.8.4.4
nameserver 8.8.8.8 

I hope this helps others with debugging (the complex interactions between all these different network tools) as well. If you use resolvconf make sure to run ls /run/resolvconf/interfaces to see what's there.

1
  • 1
    Thank you, this was very helpful. I tried the higher voted answers first and none seemed do do the trick. I didn't have any additional files in the /run/resolvconf/interface/ dir, but my NetworkManager file needed some love. Your answer helped resolve this issue for me.
    – mason81
    Jul 2, 2015 at 14:40
4

I just deleted a link in /etc/resolv.conf and created a regulary file with the nameservers' adresses. It works, and I don't see any reasons to use that rather strange construction that the Ubuntu developers have created.

2
  • 1
    Yes, I am not a fan of this kind of change which affects servers, adds complexity and opacity. This is core behavior that shouldn't change from release to release.
    – belacqua
    Oct 4, 2013 at 15:32
  • This answer is 5 years old, and IMHO is still the best of all those listed. It is simple, quick and effective. Is there any downside? Mar 9, 2019 at 4:02
3

You can edit the connection "AUTO ETH0" or whatever the name is that you use to connect in NM. You can do this by right-clicking on NM and selecting "Edit Connections...". On the IPv4 tab you can select "Automatic (DHCP) addresses only" as the "Method" and NM will allow you to set the nameserver address(es) manually even addresses are delivered over DHCP.

0
3

Adding my bit to the already long list, here's another way (tested on 12.04):

Edit /run/resolvconf/interface/NetworkManager to fit your needs. Here's an example:

search foobar.com example.com
nameserver 192.168.1.1

Then, as stated for most of the other examples, do a sudo resolvconf -u. Now you will find your resolv.conf looking like this:

nameserver 192.168.1.1
nameserver 127.0.0.1
search foobar.com example.com

I still don't like the way it's implemented now (seems Ubuntu needs a service for every foo it makes), and would prefer a plain resolv.conf for sure. But this approach seems to be the best compromise for me so far. The added "localhost-NS" shouldn't hurt too much.

0
2

I fixed this by changing the order of sources. I moved the dns source before mdns in /etc/nsswitch.conf:

hosts: files dns mdns4_minimal [NOTFOUND=return] mdns4

You can keep your settings and still use the local caching server this way.

1
  • instead of adding entries, fix the system the is broken and giving you the info you actually want. +1.
    – nelaaro
    Feb 5, 2013 at 12:30
2

How about:

sudo dpkg-reconfigure resolvconf

Before that, use Network Manager to change DNS adress and change Method to

Automatic (DHPC) adresses only

Then run the command above and reboot. That did the solution for me.

2
vi /etc/network/interfaces


This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The primary network interface
allow-hotplug eth0


# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
#auto eth0         <<----- change this comment with the #
iface eth0 inet dhcp
2

This is a very popular question, with a collection of standard possible answers, all of which, IMO, are pretty hacky. I for one have always had problems getting Ubuntu to respect the DNS settings I set up in NetworkManager -- in particular setting static DNS servers with "Automatic (DHCP) addresses only" -- and today I finally figured out what was ACTUALLY WRONG.

The problem is in the interaction between resolvconf and NetworkManager. resolvconf has this file called /etc/resolvconf/interface-order. At least on my systems, NetworkManager isn't in this file at all (except that it's covered by the * wild card at the end). So what happens is, dhclient's most recent report to resolvconf takes precedence over anything NetworkManager has to say.

Thus, at least in my case, the actual answer was to add

NetworkManager

at or near the top of /etc/resolvconf/interface-order.

(Yes, I know many people just say "uninstall resolvconf", which seems like a bad idea in and of itself, to me. But more than that, at least wily and xenial consider resolvconf a vital part of the system [i.e., ubuntu-minimal depends on it], so it would be difficult to keep your system in a consistent, updated state without resolvconf.)

Upon request I can provide more detail about how I figured this out. (EDIT: apparently I didn't do so when it was requested, sorry. At this point I don't remember much more detail than what I say next:) In a nutshell, I replaced the resolvconf executable with a shell wrapper around it which dumped its arguments, input, output and stderr to files; and added set -x to resolvconf's update scripts.

(EDIT: I can say that what I mean by the first part is that I used sudo to move the actual resolvconf executable, which could be found using the which command or the type command. Then create a shell script that ultimately just executes the moved resolvconf, but also echos the arguments to some file, and uses shell redirection to send stdin, stdout, and stderr to various other files. I don't recall where "resolvconf's update scripts" are and can't currently easily check. I think many Linux geeks can figure out what I mean; perhaps some good samaritan will provide even more detail in a comment.)

1
  • Please "provide more detail about how you figured this out" May 31, 2016 at 19:49
1
  1. Add temporary DNS. Edit /etc/resolv.conf.

    nameserver 1.1.1.1
    nameserver 1.0.0.1
    
  2. Install or update resolvconf.

    sudo apt-get install -y resolvconf
    
  3. Add your nameserver into /etc/resolvconf/resolv.conf.d/tail

    nameserver 1.1.1.1
    nameserver 1.0.0.1
    
  4. Reboot your machine: reboot

1
  • Who are you? I am used to starting from zero.
    – xiaoliuzi
    Nov 18, 2019 at 11:28
1

Another way is to define docker specific dns servers at:

/etc/docker/daemon.json

If you don't have such a file, just create it:

{
    "dns": ["1.1.1.1", "1.0.0.1"]
}

Source, also see this.

1

To change DNS, follow instructions in: https://developers.google.com/speed/public-dns/docs/using

  1. Go to Network

  2. For the connection you want to configure the DNS, click on the gear.

  3. Click the IPv4 or IPv6 Settings tab.

  4. If the selected method is "Automatic", select "Automatic (DHCP) addresses only" instead. If the method is set to something else, do not change it.

  5. In the DNS servers field, enter the DNS IP addresses, separated by a comma

EDIT: As this seems to not be working well in newer ubuntu, I would:

sudo systemctl disable systemd-resolved.service
sudo gedit /etc/resolvconf/resolv.conf.d/head 
# add
nameserver 8.8.8.8
nameserver 8.8.4.4

sudo resolvconf --enable-updates
sudo resolvconf -u

Source: https://pchelp.ricmedia.com/set-custom-dns-servers-linux-network-manager-resolv-conf/

0

In my case, the real problem was that the DHCP server was providing an IP address but failing to provide a DNS server. Solved by restarting the DHCP server (and then reconnecting from NetworkManager).

I was able to diagnose it by checking the DHCP details available from nmcli:

to show current connections, one line each:
$ nmcli c

to show more detail about connections and DNS:
$ nmcli

to show the most detail about a specific connection:
$ nmcli c s enp0s5

to filter useful lines about DHCP assignment and DNS:
$ nmcli c s enp0s5 | grep -i "dhcp\|domain\|dns\|ip"

When DNS from DHCP is working correctly, the output includes both of these critical lines:

DHCP4.OPTION[9]:          requested_domain_name_servers = 1
....
IP4.DNS[1]:               192.168.1.1   (or some other value)

You must log in to answer this question.

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