Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Most of the info I see online says to edit /etc/resolv.conf, but any changes I make just get overridden.

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

It seems that 127.0.1.1 is a local instance of dnsmasq. The dnsmasq docs say to edit /etc/resolv.conf. I tried putting custom nameservers in /etc/resolv.conf.d/base, but the changes didn't show up in /etc/resolv.conf after running sudo resolvconf -u.

FYI, I don't want to change DNS on a per-connection basis, I want to set default DNS settings to use for all connections when not otherwise specified.

share|improve this question

8 Answers 8

I believe if you want to override the DNS nameserver you merely add a line similar to this in your base file under resolv.conf.d.

Example

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

Then put your nameserver list in like so:

nameserver 8.8.8.8
nameserver 8.8.4.4

Finally update resolvconf:

$ sudo resolvconf -u

If you take a look at the man page for resolvconf it describes the various files under /etc/resolvconf/resolv.conf.d/.

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

   /etc/resolvconf/resolv.conf.d/head
          File to be prepended to the dynamically generated resolver 
          configuration file.  Normally this is just a comment line.

   /etc/resolvconf/resolv.conf.d/tail
          File to be appended to the dynamically generated resolver 
          configuration file.  To append nothing, make this  an  empty  
          file.   This file is a good place to put a resolver options line 
          if one is needed, e.g.,

              options inet6

Even though there's a warning at the top of the head file:

$ 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

this warning is is there so that when these files are constructed, the warning will ultimately work its way into the resulting resolv.conf file that these files will be used to make. So you could just as easily have added the nameserver lines that are described above for the base file, to the head file too.

References

share|improve this answer
    
I believe you should add this line to the base file as the head file basically contains the header comments to tell you not to modify the file. –  xuhdev May 29 at 6:18
    
@xuhdev - I've changed the A to use base but you could've used head as well. See my updates for more info. –  slm May 29 at 6:34

I am also interested in this question and I tried the solution proposed @sim.

To test it, I put

nameserver 8.8.8.8

in /etc/resolvconf/resolv.conf.d/base and

nameserver 8.8.4.4

in /etc/resolvconf/resolv.conf.d/head

Then I restarted the network with

sudo service network-manager restart

The result is that /etc/resolv.conf looks like

# 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 127.0.1.1

and nm-tool states that the dnsserver are

DNS:             208.67.222.222
DNS:             208.67.220.220

which are the ones provided by my router. On the other hand digging an address tells that

;; Query time: 28 msec
;; SERVER: 8.8.4.4#53(8.8.4.4)

If I am right, I conclude from all this that

  1. only the "head" part is read by resolvonf: the "base" part is somehow controlled by dnsmasq
  2. the dnsserver is actually forced to 8.8.4.4 regardless of the server provided by dhcp, BUT you loose the caching provided by dnsmasq, since the request is always sent to 8.8.4.4
  3. dnsmasq is still using ONLY the dnsserver provided by dhcp.

All in all, it works but I don't think it is the intended result asked for. A more close solution I think is the following. Edit

sudo vim /etc/dhcp/dhclient.conf

then add

supersede domain-name-servers 8.8.8.8;

The result is the following: resolv.conf contains only 127.0.0.1, which means that dnsmasq cache is invoked and nm-tool says

DNS:             8.8.8.8

which means that if the name searched for is not in the cache, then it is asked for at 8.8.8.8 and not at the server provided by dhcp.

Another (perhaps better) option is to use "prepend" instead of "supersede": in this way, if the name is not resolved by 8.8.8.8, then the request falls back on the other server. In fact, nm-tool says

DNS:             8.8.8.8    
DNS:             208.67.222.222
DNS:             208.67.220.220
share|improve this answer
    
A much better answer than hacking into the NS configs. Especially the option to prepend a server in front of the dhcp provided ones. Seems like the perfect balance of solving the problem, without creating new ones! –  Steve Midgley Nov 22 at 21:08

Try adding dns-nameservers XXX.XXX.XXX.X into your /etc/networking/interfaces file.

share|improve this answer
    
Leave a comment when you downvote, please. This is the method given in the manual, page 38. –  Zook Jul 24 at 16:07

A quick and dirty workaround that wasn't mentioned yet is setting the immutable flag on the resolv.conf file right after editing it.

$ sudo nano /etc/resolv.conf

Add this and save:

nameserver 8.8.8.8

Then:

$ sudo chattr +i /etc/resolv.conf

That should do the trick. I do this on my system too.

share|improve this answer
up vote 0 down vote accepted

I found out that you can change the nameservers that dnsmasq uses by addiing the following lines to /etc/dnsmasq.conf:

server=8.8.8.8
server=8.8.4.4

I didn't have a /etc/dnsmasq.conf file though, since it's installed by the dnsmasq package, but Ubuntu only comes with dnsmasq-base. I ran sudo apt-get install dnsmasq, then edited /etc/dnsmasq.conf, then sudo /etc/init.d/dnsmasq restart.

I ran sudo tail -n 200 /var/log/syslog to check my syslog and verify that dnsmasq was using the nameservers I specified:

Oct 21 23:00:54 mylaptop dnsmasq[8611]: using nameserver 8.8.8.8#53
Oct 21 23:00:54 mylaptop dnsmasq[8611]: using nameserver 8.8.4.4#53
share|improve this answer

Some users here pointed out that dns is somehow controlled by dnsmasq. That is indeed true. I've faced a somewhat small issue, where no matter how I changed head or body in /etc/resolvconf/resolv.conf.d , my computer couldn't actually access interned by domain name - only worked with ip addresses. What I did is to edit /etc/NetworkManager/NetworkManager.conf file. Originally, it said dns=dnsmasq but i changed it dns=208.67.222.222. Although this way, nm-tool doesn't mention 208.67.222.222, I still was able to use domain names , not just ip addresses.

Here's how my NetworkManager.conf file looks like now:

[main]
plugins=ifupdown,keyfile,ofono
#dns=dnsmasq
dns=208.67.222.222

[ifupdown]
managed=false

For more details on my problem and this solution, refer to my post on askubuntu.com

Update:

Having returned home from uni today, I've discovered that I couldn't connect to my home wifi. I've read-up a little on man NetworkManager.conf and it turns out that dns= in [main] is actually a line for plug-ins, so line dns=dnsmasq is actually adding dnsmasq plugin to the network manager, apparently. So my solution still worked, just not as expected. Here's excerpt from the man page:

dns=plugin1,plugin2, ... List DNS plugin names separated by ','. 

DNS plugins are used to provide local caching nameserver functionality (which speeds up DNS queries) and to push DNS data to applications that use it.

So by setting dns=208.67.222.222 I may have, basically, prevented network manager from using that plug in, which would otherwise use local dns server (which apparently doesn't work).

share|improve this answer

search ' Network Connection' open it

enter image description here

Then select either wifi or Ethernet whatever you are using and click on edit you will get this:

enter image description here

Select ipv4 in tabs

Select addresses only in method

and enter your dns name below, Save it

and you are done.

share|improve this answer
    
I'd have to do this for each network connection though. In the past you could change the default for all connections, which is what I was looking to do here. –  Seán Hayes Nov 9 at 18:17

sudo echo -e "nameserver 8.8.8.8\n" | sudo resolvconf -a eth0

share|improve this answer
2  
Hi and welcome to the site We like answers to explain what they do and how they work here. Please don't post one-line code only answers. Also, there's no point in using sudo for the echo, you only need it for the resolvconf. Similarly, there is absolutely no point in using -e and \n. Simple echo adds a newline anyway, what you're doing will print an empty line. If that's what you wanted, then please explain why. –  terdon Aug 11 at 11:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.