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

Solution Unverified - Updated

Environment

  • Red Hat Enterprise Linux 8 (RHEL8)
  • Cloud environment with cloud-init enabled

Issue

  • DNS options in /etc/resolv.conf are lost during reboot
  • ipv4.dns-options or ipv6.dns-options in NetworkManager connection settings lost during reboot
  • cloud-init overwrites ifcfg-* files or keyfiles causing loss of customizations

Resolution

Disable cloud-init network configuration:

In `/etc/cloud/cloud.cfg` set:
    network:
       config: disabled

Configure the VM to pull the initial network configuration from the metadata server of the cloud (e.g. Azure):

# yum install NetworkManager-cloud-setup
# systemctl edit nm-cloud-setup.service
[Service]
Environment=NM_CLOUD_SETUP_AZURE=yes
################## end of nm-cloud-setup.service "snap-in" / override #########################
[Save the file and quit the editor.]

Environment is one of:

NM_CLOUD_SETUP_AZURE for Microsoft Azure
NM_CLOUD_SETUP_EC2 for Amazon EC2 (AWS)
NM_CLOUD_SETUP_GCP for Google Cloud Platform(GCP)
NM_CLOUD_SETUP_ALIYUN for Alibaba Cloud (Aliyun)

Reload the systemd configuration, enable and start the nm-cloud-setup service and turn on the nm-cloud-setup.timer:

# systemctl daemon-reload
# systemctl enable --now nm-cloud-setup.service
# systemctl enable --now nm-cloud-setup.timer

Ref: RHEL8 nm-cloud-setup and network management documentation

Now settings such as eth0 ipv4.dns-options will persist across reboots.

nmcli con mod "System eth0" +ipv4.dns-options "timeout:1, attempts:1 rotate"

Root Cause

cloud-init overwrote NM connection profiles and ifcfg-* files with default dhcp configurations, then NM overwrote /etc/resolv.conf without custom ipv4.dns-options.

Components
Category
Tags

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.