0

On Google Cloud Platform /etc/resolv.conf file being overridden every time I do sudo systemctl restart NetworkManager.service or I restart the machine.

Is there a "correct" way to avoid it? or shall I write a script on startup which overrides it back?

3 Answers3

2

The resolv.conf options are renewed every 24 hours for global DNS, as per this official documentation. It is also possible to modify the values, by editing the DHCP Policy.

For Debian 10, Here are the steps:

  1. Edit "/etc/dhcp/dhclient.conf"

  2. Uncomment the line "supersede domain name", and modify the values of it: supersede domain-name "asdf.v1.com";

    NOTE: supersede will use only your provided details, prepend will use first your values then the server-provided ones, and append will use first the server-provided values, and then your custom ones.

  3. Save the file

  4. Restart the DHCP client with the command "sudo dhclient -v -r"

Please keep in mind that these steps might not work for other distributions, make sure to backup and review the steps before performing them.

Alex G
  • 320
2

You can tell NetworkManager not to modify some of the /etc/resolv.conf entries by making changes to the file /etc/NetworkManager/NetworkManager.conf.

For instance, GCP uses the metadata sever, IP address 169.254.169.254 as the default name sever. You can override the nameserver entry of resolv.conf by following below steps -

  1. Add dns=none entry to /etc/NetworkManager/NetworkManager.conf file as below -
 [main]
 #plugins=ifcfg-rh
 dns=none
  1. Restart NetworkManager service
sudo systemctl restart NetworkManager.service
  1. Modify /etc/resolv.conf with your custom nameserver
nameserver 127.0.0.1

After making these changes, restarting NetworkManger should not override your custom entry.

Daniel t.
  • 9,619
2

Following HÃ¥kan Lindqvist comment I used symbolic link from paragraph 29.2

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/manually-configuring-the-etc-resolv-conf-file_configuring-and-managing-networking

NetworkManager does not automatically update the DNS configuration if /etc/resolv.conf is a symbolic link. This section describes how to replace /etc/resolv.conf with a symbolic link to an alternative file with the DNS configuration.

  1. Create a file, such as /etc/resolv.conf.manually-configured, and add the DNS configuration for your environment to it. Use the same parameters and syntax as in the original /etc/resolv.conf.

  2. Remove the /etc/resolv.conf file:

    # rm /etc/resolv.conf
    
  3. Create a symbolic link named /etc/resolv.conf that refers to /etc/resolv.conf.manually-configured:

    # ln -s /etc/resolv.conf.manually-configured /etc/resolv.conf
    
Michael Hampton
  • 252,907