I do not want network manager to add DNS servers received from DHCP to my /etc/resolv.conf.

When configuring from GUI/Connections/IPV4 and choose the method Automatic (address only) it still adds DNS servers received via DHCP.

Is it possible to do it per connection (specific ssid ?)

  • If you choose method "Automatic (DHCP) addresses only" then NetworkManager won't, or shouldn't, change resolv.conf when activating that connection. If NetworkManager does in fact add addresses to resolv.conf despite the "addresses only" setting then there is a bug in NetworkManager and you should report that bug using Launchpad. To stop NetworkManager from changing resolv.conf you should select "Automatic (DHCP) addresses only" in all connection configurations that become active on your system. – jdthood May 18 '15 at 10:31
up vote 20 down vote accepted

One way to stop Network Manager from adding dns-servers to /etc/resolv.conf file is to do this:

First open the nm conf file /etc/NetworkManager/NetworkManager.conf:

sudo vim /etc/NetworkManager/NetworkManager.conf

And add this to the [main] section:

dns=none

Save and exit.

  • The Main section is [main], not [Main] – A.B. May 15 '15 at 8:54
  • /etc/resolv.conf is typically a symlink - are you sure making it immutable makes the target of the link also immutable? – muru May 15 '15 at 9:19
  • Yes, /etc/resolv.conf is usually a symlink and the chattr method won't work. So I would suggest removing that part of the answer. – jdthood May 18 '15 at 10:36
  • 3
    This doesn't work for me in debian jessie – Dimitri Kopriwa Jan 5 '16 at 16:06
  • 1
    This does not work on Ubuntu 18.04 – LuizAngioletti Sep 3 at 2:21

My personal favorite is to use line supersede domain-name-servers in /etc/dhcp/dhclient.conf. No matter what dns access point provides , your ubuntu will always use those dns specified in dhclient.conf

Sample from my file

#send host-name "andare.fugue.com";
send host-name = gethostname();
#send dhcp-client-identifier 1:0:a0:24:ab:fb:9c;
#send dhcp-lease-time 3600;
#supersede domain-name "fugue.com home.vix.com";
supersede domain-name-servers 208.67.220.220;
#prepend domain-name-servers 127.0.0.1;
request subnet-mask, broadcast-address, time-offset, routers,
    domain-name, domain-name-servers, domain-search, host-name,
    dhcp6.name-servers, dhcp6.domain-search,
    netbios-name-servers, netbios-scope, interface-mtu,
    rfc3442-classless-static-routes, ntp-servers,
    dhcp6.fqdn, dhcp6.sntp-servers;
#require subnet-mask, domain-name-servers;
  • creating a line as described (using server 8.8.8.8) does not work on Ubuntu 18.04. – LuizAngioletti Sep 3 at 2:26
  • 2
    @LuizAngioletti 18.04 is using different type of network manager application, called netplan. This answer was written for pre-18.04 releases. I may update it later, though. – Sergiy Kolodyazhnyy Sep 3 at 2:41

/etc/resolv.conf is symlinked to /run/resolvconf/resolv.conf. NetworkManager doesn't update /etc/resolv.conf directly (only updates /run/resolvconf/resolv.conf). So:

  • remove symlink (rm /etc/resolv.conf)
  • write you own version of /etc/resolv.conf
  • 1
    I know this is a ubuntu forum but while googling for centos this was still my first google result, so posting comment for future centos googlers. this did not solve my problem on centos. It appears the centos network manager is directly working on /etc/resolv.conf. – Tommy Dec 15 '16 at 18:56
  • This is not true for Debian Stretch most certainly. It removes your file and replaces it with another symlink. Very annoying. – RichieHH Mar 8 '17 at 7:40
  • Here is one for (virtualized) Fedora 25 using nmcli: In /etc/sysconfig/network-scripts/ifcfg-ens3 we see that PEERDNS=yes which means (in this case, wrong) DNS information is obtained by DHCP, and it ends up in /etc/resolv.conf. I just want 127.0.0.1 as nameserver. Running nmcli con mod ens3 ipf4.ignore-auto.dns yes result in PEERDNS=no in the interface config file. However /etc/resolv.conf still has stray namseserverentry, so if have to restart the connection nmcli con down ens3; nmcli con up ens3. See also: certdepot.net/rhel7-configure-ipv4-addresses – David Tonhofer Apr 13 '17 at 16:37
  • On Ubuntu 18.04, restarting 'service network-manager restart' will cause NM to overwrite /etc/resolv.conf even if it is a hard file and not a symlink. – LuizAngioletti Sep 3 at 2:27

What I did on Debian Stretch:

  • rm /etc/resolv.conf ###(the symlink)
  • touch /etc/resolv.conf
  • echo "nameserver x.x.x.x" >> /etc/resolv.conf

Where "x.x.x.x" is your preferred DNS Server IP Address be it from OpenDNS, Google or Cloudflare for example.

Thanks for the idea Taha Jahangir! I can confirm that your suggestion worked for my Debian Stretch distribution

protected by Community Sep 26 at 18:44

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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