Home / Prevent /etc/resolv.conf being overwritten on CentOS

Prevent /etc/resolv.conf being overwritten on CentOS

This post is more of a reference for myself rather than anything else to solve an issue I was having with DNS lookups on a CentOS machine. It’s how to prevent the /etc/resolv.conf file being overwritten when the system restarts if you have a custom configuration you don’t want changed.

Prevent /etc/resolv.conf being overwritten on CentOS

On a Linux system the /etc/resolv.conf specifies the nameservers to use when looking up DNS records, and may be overwitten when the system reboots and gets DNS and IP address information information from the network settings, DHCP servers etc.

The particular system I was maintaining has no GUI so everthing needs to be done via an SSH session on the command line.

The solution I implemented was to create a file at /etc/dhclient-enter-hooks with the contents:


I haven’t yet been able to reboot the machine in question but will update this post reporting back once I have been able to do so with whether or not it worked.

Update later on in the day

I was able to reboot the server late in the evening and my file wasn’t left completely intact but the end result was what I wanted. It looked something like this before the reboot:

Search [something here, can't remember exactly what]

Line 2 contains the local caching server I had already installed and was the line I had added; the other lines were already present and had presumably been added by the process that started the network. After I rebooted the only line remaining was the line.

So for some reason the file was still re-generated (the timestamp is the same as the time the server rebooted, and not when I modified it earlier in the evening) but retained my custom settings and not the other settings. I shall certainly have to keep my eye on this file and its settings and have another look at how the nameserver settings are set.