How do I set my DNS when resolv.conf is being overwritten?

1,077,127

Solution 1

I found out that you can change the nameservers that dnsmasq uses by adding 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 service dnsmasq restart and sudo service network-manager 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

Solution 2

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

NOTE: Before we get started, sure the following package is installed, apt install resolvconf.

$ 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

Solution 3

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

Solution 4

For static IP situations, the Ubuntu Server Guide says to change the file /etc/network/interfaces, which may look like this:

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

You change the IPs 192.168.3.45 192.168.8.10 for the ones you want, like 8.8.8.8

https://help.ubuntu.com/14.04/serverguide/serverguide.pdf Page 38

Solution 5

  1. Search ' Network Connection'

  2. Open it

                        enter image description here

  3. Then select either WiFi or Ethernet, or whatever you are using, and click on edit. You'll get this:

                  enter image description here

  4. Select ipv4 in tabs

  5. Select addresses only in method

  6. Enter your DNS name below, and save it

  7. You're done

Share:
1,077,127

Related videos on Youtube

Seán Hayes
Author by

Seán Hayes

Full stack developer

Updated on September 18, 2022

Comments

  • Seán Hayes
    Seán Hayes over 1 year

    Most of the info I see online says to edit /etc/resolv.conf, but any changes I make there 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.

    UPDATE:

    I answered this question myself: https://unix.stackexchange.com/a/163506/67024

    I think it's the best solution since:

    1. It works.
    2. It requires the least amount of changes and
    3. It still works in conjunction with dnsmasq's DNS cache, rather than bypassing it.
    • Philippe Gachoud
      Philippe Gachoud almost 6 years
      Better answer your question instead of update your question I think... will be easier to find the right answer you gave to your problem
    • Skippy le Grand Gourou
      Skippy le Grand Gourou about 5 years
      It seems that most answers are Ubuntu-oriented, and overly complicated. A universal solution for NetworkManager users is to simply add dns=none in /etc/NetworkManager/NetworkManager.conf (see details in my answer below).
    • foman
      foman almost 5 years
      I think this answer clarifies why the resolve.conf is overwritten, then you know how to configure it.
  • xuhdev
    xuhdev almost 10 years
    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.
  • slm
    slm almost 10 years
    @xuhdev - I've changed the A to use base but you could've used head as well. See my updates for more info.
  • Zook
    Zook almost 10 years
    Leave a comment when you downvote, please. This is the method given in the manual, page 38.
  • Seán Hayes
    Seán Hayes over 9 years
    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.
  • danniee
    danniee over 9 years
    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!
  • Clint Eastwood
    Clint Eastwood over 9 years
    There is a reason why this is marked as the best answer...because it is indeed! thanks very much! I would add that, after all the steps you mentioned, a network restart might be necessary for everything to work smoothly (it was for me.... sudo service network-manager restart)
  • Luke
    Luke about 9 years
    I love you! this UI setting saved my ass from sudo and vim mess :'(
  • HorusKol
    HorusKol almost 9 years
    Ubuntu 14.04 - when I put the nameservers into base and run resolvconf -u, the nameservers were not put into resolv.conf - when I put the nameservers into head, they were
  • HorusKol
    HorusKol almost 9 years
    Using Mint (on Ubuntu 14.04) - but seen this with KDE, too - for some reason, setting DNS servers in the GUI Network Manager doesn't affect the DNS settings used in a terminal
  • Cees Timmerman
    Cees Timmerman almost 9 years
    The unmentioned manual shows all IPs on one line. This answer seems to suggest adding a line. And why is the last number only one X wide? I think it mostly was the extremely informal and uncertain short chat-style writing that garnered the downvotes, @Zook.
  • Nate Lockwood
    Nate Lockwood over 8 years
    On Ubuntu 14.04 Server about half the time a cold boot would result no internet connectivity using a URL but an IP-Address would work. I spent a lot of time fruitlessly trying to fix it, gave up for months, then found this solution. I, too, think it is the best answer.
  • bitsoflogic
    bitsoflogic over 8 years
    Ubuntu 14.04 - Also had to comment out configuration set in /run/resolvconf/interface/NetworkManager
  • Joel Berger
    Joel Berger over 8 years
    This certainly looks right but how do I now regenerate resolv.conf?!
  • Jeff Jirsa
    Jeff Jirsa almost 8 years
    Anytime your solution involves chattr, it's not really a solution.
  • Eric
    Eric over 7 years
    this is what I do on systems where I need to temporarily change the DNS for some reason and don't want to modify the configuration. As a permanent solution I wouldn't recommend it.
  • Younis Bensalah
    Younis Bensalah over 7 years
    "quick and dirty workaround"
  • Michael
    Michael over 7 years
    This is the answer that solved my issue.
  • Nick Triantafillou
    Nick Triantafillou about 7 years
    Perfect. Just adding that you should sudo service networking restart to enable the changes.
  • Admin
    Admin about 7 years
    This isn't dirty. Programs that destroy local configuration because they think they know better are dirty.
  • Dzamo Norton
    Dzamo Norton almost 7 years
    @JoelBerger ifdown eth0; ifup eth0.
  • frazras
    frazras over 6 years
    type nslookup google.com and the first IP in the list should be your new nameserver, if not, you did it wrong
  • cjaphe
    cjaphe about 6 years
    Ubuntu 16.04: Worked if appended to /etc/resolvconf/resolv.conf.d/head only, not with base. Confirmed with nslookup google.com.
  • PlasmaBinturong
    PlasmaBinturong about 6 years
    What if we don't have that dhcp3 folder? I have Xubuntu 17.10, has it moved to /etc/dhcp simply?
  • PlasmaBinturong
    PlasmaBinturong about 6 years
    It's intriguing that dnsmasq has to be installed. This indeed fixed my DNS in a normal situation, but it totally broke my VPN configuration (VPN connection now fails...)
  • Fiddy Bux
    Fiddy Bux over 5 years
    It's worth noting nm-tool has been replaced with nmcli
  • stiv
    stiv over 4 years
    there is no such file on Centos
  • stiv
    stiv over 4 years
    no /etc/network/interfaces on Centos
  • woodz
    woodz almost 4 years
    ubuntu 16: changes from the procedure with dnsmasq are not being propagated into /etc/resolv.conf. The consequence is, hat nslookup still uses its original defined localhost 127.0.0.1. Although I can confirm your syslogs mentioned.
  • Yan King Yin
    Yan King Yin almost 4 years
    I hardly know what's going on... but I go to "Edit Connections" and "IPv4 Settings" and add those 2 DNS servers and it works!!
  • java-addict301
    java-addict301 almost 4 years
    this is the correct answer
  • Xofo
    Xofo almost 3 years
    This is a very good answer.
  • ToiletGuy
    ToiletGuy over 2 years
    quick question, how many name server can we put on ?
  • Tal Weiss
    Tal Weiss over 2 years
    How do I find out which program is changing my resolv.conf file???
  • Younis Bensalah
    Younis Bensalah over 2 years
    There is usually a comment in the resolv.conf file such as "# generated by". The usual suspects are NetworkManager and systemd-resolved.