258

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?

Braiam
  • 69,112
PnotNP
  • 3,209

20 Answers20

163

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

guntbert
  • 13,475
stgraber
  • 2,904
151

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.

B. Shea
  • 1,252
Randall Cook
  • 4,155
  • 4
  • 19
  • 21
118

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.

Azendale
  • 12,021
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 :)

Joseph Mituzas
  • 331
  • 2
  • 2
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!

Don Chai
  • 231
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.

Jase
  • 731
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.

Mark
  • 91
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.)

belacqua
  • 23,540
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.

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.

Ivan
  • 116
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.

jdthood
  • 12,625
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.

belacqua
  • 23,540
Izzy
  • 3,620
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.

jdthood
  • 12,625
Dan B
  • 21
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.

Adige72
  • 75
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
enedil
  • 1,002
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
  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

Pablo Bianchi
  • 17,371
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.

Pablo Bianchi
  • 17,371
AndreyKo
  • 166
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/

Ferroao
  • 959
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)
krubo
  • 951