13

CentOS will wipe out any manual changes made to /etc/resolv.conf periodically. The defaults in Linux are poor in terms of failing over in a reasonable time (query name servers in same order every time, 5 second timeout, 2 retries).

Hence, the first DNS in your resolv.conf is essentially critical path. If it fails you can be looking at 10 seconds before you fail over.

These defaults are tweakable (see resolv.conf man page), but how can any changes be made permanent in CentOS and persist through reboots etc.?

Adam C
  • 5,262

7 Answers7

16

The answer can be found in the /sbin/dhclient-script:

if [ -n "${RES_OPTIONS}" ]; then
    echo "options ${RES_OPTIONS}" >> ${rscf}
fi

But, it's not terribly obvious where you can set RES_OPTIONS to make the script pick it up - some things like the search domain can be set in the ifcfg-ethX file, but resolver options are set elsewhere. The file you want is in fact /etc/sysconfig/network. To set the relevant options, add something like this line to that file:

RES_OPTIONS="rotate timeout:1 retries:1"

That will set the timeout to 1 second, use a single retry and tell the client to rotate its resolvers per request instead of sequentially going through the list each time.

If you would like to have the changes take effect immediately, then issue a service network restart command and check out your new /etc/resolv.conf in all its glory. Here's what mine looked like when testing this out:

# cat /etc/resolv.conf 
; generated by /sbin/dhclient-script
search example.com
options rotate timeout:1 retries:1
nameserver 10.1.1.2
nameserver 10.1.1.1
Adam C
  • 5,262
6

The accepted answer is when using legacy networking scripts. If you use NetworkManager you might not even have /etc/sysconfig/network, and if you do it will still not be used for connections managed by NetworkManager.

If you use NetworkManager:

To add options, ex adding rotate to bond0:

nmcli con mod bond0 +ipv4.dns-options rotate

To remove that option:

nmcli con mod bond0 -ipv4.dns-options rotate

The + is good to change options too; NetworkManager is smart enough to detect existing options and update them. For example, changing the timeout value:

root@debian:~# nmcli con show bond0 |grep ipv4.dns-options
ipv4.dns-options:                       "rotate,timeout:5"
root@debian:~# nmcli con mod bond0 +ipv4.dns-options timeout:3
root@debian:~# nmcli con show bond0 |grep ipv4.dns-options
ipv4.dns-options:                       "rotate,timeout:3"

This means the value is ignored for remove and not even needed. To remove timeout:

nmcli con mod bond0 -ipv4.dns-options timeout

It will work with a timeout value too but that value will be ignored, so removing timeout:5 will also remove any other timeout value.

NB: While looking into this I came across a related bug that was fixed in network-manager v1.14.6, v1.15.2-dev and v1.16. If you encounter any issue check your network-manager version first.

1

As this answer appeared in my searches for how to do it on my machine (MX Linux, Debian derivative) and it did not had the answer for that distribution, I want to add how to do it for that distribution:

Edit this file:

 /etc/resolvconf/resolv.conf.d/head
0

If you need to do this with flat files rather than nmcli commands (such as with a configuration management tool) Red Hat provides another solution.

Create a script /etc/NetworkManager/dispatcher.d/15-resolv, which copies a custom /etc/resolv.conf into place.

#!/bin/bash
#
# Description : script to override default resolv.conf file
# with customized file.
cp -f /etc/resolv.conf.custom /etc/resolv.conf

After restarting NetworkManager, this script will be executed, replacing the file with your own.

https://access.redhat.com/solutions/61921

Aaron Copley
  • 12,954
0

You can tell NetworkManager not to manage the /etc/resolv.conf file all together.

  1. Create the /etc/NetworkManager/conf.d/90-dns-none.conf file with the following content:

    [main]
    dns=none
    
  2. Reload the NetworkManager service:

    systemctl reload NetworkManager
    

The documentation this came from has a second solution which involves replacing /etc/resolv.conf with a symlink to your file.

Give this a read: 22. Manually configuring the /etc/resolv.conf file

Aaron Copley
  • 12,954
0

just add

resolv_conf_options=rotate\ timeout:1\ retries:1

in your resolvconf.conf

-4

I faced the same problem last time with my new subscribed linux VPS. How i solved it was to use the chattr +i command to make the file immutable. Just go to the /etc folder and run this after you had made necessary changes to the resolv.conf file :

chattr +i resolv.conf

If you wanna revert the setting, just do this:

chattr -i resolv.conf

Full guideline for your reference: http://boxtutor.com/fix-etcresolv-conf-is-not-saving-after-server-reboot/