Ubuntu: How do I add a DNS server via resolv.conf?



Question:

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?


Solution:1

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


Solution:2

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.


Solution:3

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.


Solution:4

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-nameservers 75.75.75.75 75.75.76.76          dns-search local  

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 :)


Solution:5

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!


Solution:6

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.


Solution:7

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


Solution:8

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.


Solution:9

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.


Solution:10

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.


Solution:11

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.


Solution:12

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.


Solution:13

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.


Solution:14

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.


Solution:15

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  


Solution:16

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


Solution:17

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": ["10.0.0.2", "8.8.8.8"]  }  

Source: https://robinwinslow.uk/2016/06/23/fix-docker-networking-dns/

Also, see: https://docs.docker.com/engine/reference/commandline/dockerd/#/daemon-configuration-file


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »