CentOS 7 NetworkManager Keeps Overwriting /etc/resolv.conf

Mattias Geniar, Tuesday, September 1, 2015

In CentOS or Red Hat Enterprise Linux (RHEL) 7, you can find your /etc/resolv.conf file, which holds all nameserver configurations for your server, to be overwritten by the NetworkManager.

If you check the content of /etc/resolv.conf, it may look like this.

$ cat /etc/resolv.conf
# Generated by NetworkManager
search mydomain.tld
nameserver 8.8.8.8

The NetworkManager will assume it has the rights to control /etc/resolv.conf, if it finds a DNS related configuration in your interface configuration file.

$ grep DNS /etc/sysconfig/network-scripts/ifcfg-*
DNS1="8.8.8.8"
IPV6_PEERDNS="yes"

To prevent Network Manager to overwrite your resolv.conf changes, remove the DNS1, DNS2, ... lines from /etc/sysconfig/network-scripts/ifcfg-*.

Now, you can manually change the /etc/resolv.conf file again, and you should be good to go. NetworkManager will no longer overwrite your DNS nameserver configurations.



Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek & public speaker. Currently working on DNS Spy & Oh Dear!. Follow me on Twitter as @mattiasgeniar.

Share this post

Did you like this post? Will you help me share it on social media? Thanks!

Comments

Yves Vogl Saturday, August 6, 2016 at 16:40 - Reply

Unfortunately that’s not true. Even with PEERDNS=”no”, IPV6_PEERDNS=”no” and NM_CONTROLLED=”no” the Network Manager will keep overriding your /etc/resolv.conf.

Here’s my network configuration to reproduce:

# Device
DEVICE="enp4s0"
TYPE="Ethernet"
ONBOOT="yes"
NM_CONTROLLED="no"
BOOTPROTO="none"
PEERDNS="no"

# IPv4
IPADDR="x.x.x.x"
NETMASK="x.x.x.x"
SCOPE="peer x.x.x.x"

# IPv6
IPV6INIT="yes"
IPV6ADDR="x.x.x.x/128"
IPV6_DEFAULTGW="fe80::1"
IPV6_DEFAULTDEV="enp4s0"
IPV6_PEERDNS="no"

I’ve tried this option on my own after reading the official documentation (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/3/html/Reference_Guide/s1-networkscripts-interfaces.html) but found out that you need to either ensure that Network Manager is disabled completely:

# systemctl disable NetworkManager.service
# systemctl stop NetworkManager.service

or tell NetworkManager to not modify the DNS settings:

 /etc/NetworkManager/NetworkManager.conf
 [main]
 dns=none

Cheers,
Yves


Mattias Geniar Sunday, August 7, 2016 at 21:32 - Reply

Hi Yves,

Thanks for the nice feedback, much appreciated!

Mattias


Alex Wednesday, November 16, 2016 at 00:38 - Reply

Ironically, Redhat’s own training manual does not address this problem properly.

I was taking a RHEL 7 Sysadmin course when I ran into this bug. I used nmcli thinking it would save me time in creating a static connection. Well, the connection was able to ping IPs immediately, but was not able to resolve any host addresses. I noticed that /etc/resolv.conf was being overwritten and cleared of it’s settings.

No matter what we tried, there was nothing the instructor and I could do to fix the issue. We finally used the “dns=none” solution posted here to fix the problem.


Alexander Garzon Thursday, December 15, 2016 at 05:15 - Reply

Well, the dns=none trick doesnt works for me. Centos 7

systemctl restart network.service always rewrite my /etc/resolv.conf


Alex Wednesday, January 4, 2017 at 16:04 - Reply

Have you tried running “service NetworkManager restart” as root first? Worked for me on Centos:
[14:03:39] XXXXXXXXXX:~ > cat /etc/centos-release
CentOS Linux release 7.2.1511 (Core)


Yves Vogl Saturday, March 4, 2017 at 12:19 - Reply

Hi, indeed – you need to restart the NetworkManager service after modifying /etc/NetworkManager/NetworkManager.conf


David Foster Friday, December 15, 2017 at 01:15 - Reply

Ran into this problem today on RHEL7.4 (Maipo, both 3.10.0-693.5.2.el7.x86_64 and 3.10.0-693.11.1.el7.x86_64).

I could not get this problem to go away using “dns=none” in /etc/NetworkManager/NetworkManager.conf, even after restarting the service. I had to disable this service completely.

Is this recent behavior?


Ken Yap Friday, February 23, 2018 at 04:18 - Reply

If you follow the original recommendation, be sure to have a static hostname set in /etc/hostname or via hostnamectl otherwise the hostname defaults to localhost.localdomain.

It’s an annoyance that I have to run NetworkManager otherwise screen sharing via vino-server doesn’t work.


Ken Yap Friday, February 23, 2018 at 08:59 - Reply

I can confirm that the correct method is the dns=none in [main] in /etc/NetworkManager/NetworkManager.conf.

If you remove DNS1, etc from /etc/sysconfig/network-scripts/ifcfg-xxx when NetworkManager fires up (I have to run it in my situation), it will put no nameservers in /etc/resolv.conf which will be problematic. A puppet agent cannot recover this situation as it cannot contact the puppetmaster to set /etc/resolv.conf correctly.


Brian Wednesday, March 7, 2018 at 01:08 - Reply

Thank you Ken – yes – that fix finally worked for me on RHEL 7!

Set “dns=none” in the [man] section of /etc/NetworkManager/NetworkManager.conf
Tested: with “$ cat /etc/resolv.conf” both before and after “# service network restart” and got the same output!

Otherwise I could not find out how to reliably set the “search” domains list, as I did not see an option in the /etc/sysconfig/network-scripts/ifcfg-INT files.


Brian Wednesday, March 7, 2018 at 01:41 - Reply

Brian again here… note that I also had “DNS1, DNS2” removed from /etc/sysconfig/nework-scripts/ifcfg-INT.

CAUTION: the “hostnamectl”[1] command will also reset /etc/resolv.conf rather bluntly… replacing the default “search” domain and deleting any “nameserver” entries. The file will also include the “# Generated by NetworkManager” header comment.

[1] e.g. #hostnamectl set-hostname newhost.domain –static; hostnamectl status
Then notice how that will overwrite /etc/resolv.conf as well


tom Tuesday, July 10, 2018 at 16:56 - Reply

nmcli or nmtui commands – will allow you to set parameters right into NetowrkManager. This should program NetworkManager directly and prevent the override of your resolv.conf. Hope it helped


javed Monday, December 3, 2018 at 20:31 - Reply

It worked on RHEL 7.5 on aws EC2
/etc/NetworkManager/NetworkManager.conf
[main]
dns=none


Ben Monday, March 4, 2019 at 17:06 - Reply

I found that if I added “dns=none” to NetworkManager.conf the restarted NetworkManager, it would still overwrite my resolv.conf. However, if I restored my original resolv.conf and then restarted NetworkManager a second time, it would not overwrite. Quite bizarre. I tried several times to ensure that I wasn’t imagining it.


Leave a Reply

Your email address will not be published. Required fields are marked *

Inbound links