How to make persistent changes to the /etc/resolv.conf

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux 5, 6, 7, 8, 9, 10
  • initscripts
  • NetworkManager
  • DNS

Issue

  • DNS servers in /etc/resolv.conf change after a reboot or network service restart
  • resolv.conf gets overwritten
  • Why do entries in /etc/resolv.conf get cleared after a system reboot?
  • How to make permanent changes to the /etc/resolv.conf
  • Reboot removes or changes entries in /etc/resolv.conf

Resolution

In general, NetworkManager is managing /etc/resolv.conf. On RHEL7, issues with initscripts prevented this. If you are on RHEL7, please ensure

  • To update initscripts to 9.49.41-1.el7 (released with RHBA-2018:0983) or later to ensure you are not affected. These fixes are part of RHEL7.5GA and later.
  • To update NetworkManager to 1.8.0-9.el7 (released with RHSA-2017:2299) or later to ensure you are not affected. These fixes are part of RHEL7.4GA and later.

Depending on the desired outcome, following is possible:

  • Goal: use DNS servers obtained from the DHCP server:
    • RHEL will by default (so unmodified RHEL deployments) overwrite /etc/resolv.confas soon as any network interfaces use DHCP. No further action required.
  • Goal: use DNS servers obtained from the DHCP server, and additional DNS servers:
    • For this, "BOOTPROTO=dhcp" should be used in the ifcfg-interface file, and additionally DNS1=<dns-server-ip> should be configured in the interfaces ifcfg-interface file. With this, both the DHCP-DNS servers, and the DNS1-server will be written to /etc/resolv.conf. The DOMAIN option can be used to configure search domains, for example:

      ```
      DOMAIN="domain1 domain2 domain3"
      ```
      
  • Goal: do not overwrite DNS servers in /etc/resolv.conf:
    • If resolv.conf should not be overwritten with the DNS servers obtained via DHCP, then the DHCP interfaces should have PEERDNS=no set in their ifcfg file, for example:

      ```
      $ cat /etc/sysconfig/network-scripts/ifcfg-eth1
      TYPE=Ethernet
      DEVICE=eth1
      BOOTPROTO=dhcp
      PEERDNS=no
      ```
      
    • On RHEL 9 and later, /etc/sysconfig/network-scripts/ifcfg-* file has been deprecated. Instead, you can use /etc/NetworkManager/system-connections/<Connection_Name>.nmconnection file, for example:

      ```
      [ipv4]
      dns=192.168.122.1;
      dns-search=example.com;
      ignore-auto-dns=true
      method=auto
      ```
      

      Or from the command line, you can run:

      ```
      # nmcli con modify <Connection_Name> ipv4.ignore-auto-dns true ipv4.dns-search "example.com" ipv4.dns "192.168.122.1"
      ```
      

      Note the combination of method=auto and ignore-auto-dns=true ignores automatically configured name servers and search domains, and only name servers and search domains specified in the "dns" and "dns-search" properties are used. Thus, this will not prevent overwriting of resolv.conf file itself. You still need to specify dns servers and search domains via nmcli command line or in <Connection_Name>.nmconnection file, not directly editing resolv.conf file.

    • As an alternative, if NetworkManager is used, it can be configured to not update /etc/resolv.conf in /etc/NetworkManager/NetworkManager.conf or /etc/NetworkManager/conf.d/90-dns-none.conf:

      ```
      [main]
      dns=none
      ```
      

      After the change, NetworkManager has to be restarted using systemctl restart NetworkManager.
      This is particularly important to avoid the issue described in Solution 6975705.

    • As a further alternative (or workaround), you could also modify /etc/resolv.conf as desired and then make the file immutable to changes (details are in the chattr(1) manpage). After making the file immutable "+i", further modifications will be impossible before reversing that operation with "-i". Please be aware that this is a common source for problems, for example when multiple persons administrate a system, a first person makes the file immutable and other persons are not aware of the chattr command.

      ```
      # chattr +i /etc/resolv.conf
      ```
      

In environments using cloud-init where dhcp needs to update resolv.conf but custom DNS options are required

follow How to make /etc/resolv.conf changes persistent in a cloud environment

Root Cause

RHEL will by default (so without further configuration/modification) overwrite /etc/resolv.confas soon as any network interfaces use DHCP. Depending on the exact version if initscripts/NetworkManager, "RESOLV_MODS=no" or "PEERDNS=no" in the relevant /etc/sysconfig/network-scripts/ifcfg-* files can prevent this.

Diagnostic Steps

  • Messages from the /var/log/messages:
Oct 14 12:40:52 hostname NET[22961]: /etc/sysconfig/network-scripts/ifdown-post : updated /etc/resolv.conf
Oct 14 12:40:57 hostname NET[23256]: /etc/sysconfig/network-scripts/ifup-post : updated /etc/resolv.conf
Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.