65

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

Pabi
  • 7,401
  • 3
  • 40
  • 49
  • 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
  • 3
    You can either use GUI in the IPv4 settings on the connection and choose "adddresses only" in the mode, or, do what it does yourself by editing the corresponding connection file in /etc/NetworkManager/system-connections/ and adding ignore-auto-dns=true line to the [ipv4] section. – Sasha Pachev Jul 25 '19 at 02:12

4 Answers4

75

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
rc-manager=unmanaged

Save and exit.

Tubrag
  • 3
krt
  • 1,956
  • 15
  • 12
  • /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 09: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
  • from man page (NetworkManager.conf, 15.04 ): The default if the key is not specified. NetworkManager will update resolv.conf to reflect the nameservers provided by currently active connections. Good, but I would just comment that line out instead of setting to none – Sergiy Kolodyazhnyy May 19 '15 at 06:45
  • 8
    This doesn't work for me in debian jessie – Dimitri Kopriwa Jan 05 '16 at 16:06
  • What version of NM does this work on? Might want to note it, because it doesn't work on older stuff. – Michael Kohne Mar 02 '16 at 23:07
  • 11
    This does not work on Ubuntu 18.04 – LuizAngioletti Sep 03 '18 at 02:21
  • 9
    I had to sudo service network-manager restart before the change to the .conf file took effect. – Chris Moore Nov 06 '18 at 14:30
  • 1
    Worked for me on Ubuntu 18.10, I had to run restart network manager as Chris above stated. – Rwky Jan 31 '19 at 22:31
  • Thank you so much! worked like a charm on 18.04.. Finally!!! – Pa_ Feb 11 '19 at 20:47
  • I've done this but now resolvconf is updating resolv.conf... I edited /etc/resolvconf/resolv.conf.d/head and my edits also appeared on /etc/resolv.conf How do I stop that? @krt Thanks very much. – Shayan Sep 02 '19 at 08:53
  • Stop+Disable systemd-resolved, remove the symlink, create a real /etc/resolv.conf file and set dns=none as mentioned, and reboot. Work ok on Fedora 33. – Arvy Mar 01 '21 at 23:17
  • Did you mean unmanaged? Because none just rewrite resolv.conf into an empty file. – DMaster Mar 12 '21 at 09:38
  • sudo service network-manager restart is a must, i even restart but still not working – Ryan Arief Oct 17 '21 at 12:23
  • I have a related question: https://askubuntu.com/questions/1453982/ubuntu-22-04-1-nameservers-keep-getting-overwritten – Ryan Feb 08 '23 at 03:14
  • For me, sudo systemctl restart NetworkManager.service was the command for restarting. Unfortunately, the problem at https://askubuntu.com/questions/1453982/ubuntu-22-04-1-nameservers-keep-getting-overwritten still happened even after adding the above lines to the [main] section. – Ryan Feb 08 '23 at 03:22
7

/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
Taha Jahangir
  • 296
  • 3
  • 5
  • 4
    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
  • 1
    This is not true for Debian Stretch most certainly. It removes your file and replaces it with another symlink. Very annoying. – RichieHH Mar 08 '17 at 07:40
  • 1
    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: https://www.certdepot.net/rhel7-configure-ipv4-addresses/ – David Tonhofer Apr 13 '17 at 16:37
  • 1
    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 03 '18 at 02:27
  • The symlink and (/run/resolvconf/resolv.conf) does not existing for Arch linux – xx1xx Aug 11 '20 at 04:07
5

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;
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
2

Just in case I have done a little script to do that automatically (here with google DNS) for every ethernet/wireless connections:

nmcli -g name,type connection  show  --active | awk -F: '/ethernet|wireless/ { print $1 }' | while read connection
do
  nmcli con mod "$connection" ipv6.ignore-auto-dns yes
  nmcli con mod "$connection" ipv4.ignore-auto-dns yes
  nmcli con mod "$connection" ipv4.dns "8.8.8.8 8.8.4.4"
  nmcli con down "$connection" && nmcli con up "$connection"
done
张馆长
  • 131