Share

Introduction

resolv.conf is an important configuration file in Linux that contains information about the DNS (Domain Name System) servers that your system uses to resolve domain names into IP addresses. However, sometimes the resolv.conf file can be overwritten, which can lead to problems with your DNS settings. In this article, we’ll see how to stop resolv.conf from being overwritten in Linux.

Method 1: Use chattr Command to Stop resolv.conf from being Overwritten in Linux

The first method that stops overwriting of resolv.conf in Linux is to use the chattr command.

This command changes the file attributes of a file, including making it read-only. Here are the steps to follow:

Open the terminal and type the following command:

sudo chattr +i /etc/resolv.conf

This command makes the resolv.conf file read-only and prevents modification of resolv.conf.

To remove the read-only attribute, type the following command:

sudo chattr -i /etc/resolv.conf  

Method 2: Use systemd-resolved to Stop resolv.conf from being Overwritten in Linux

The second method is to use systemd-resolved, a system service that manages network name resolution.

This service sets up DNS servers for our system. Consequently, it prevents overwriting of resolv.conf. Here are the steps to follow:

Install the systemd-resolved package by typing the following command:

sudo apt-get install systemd-resolved   

Once installed, open the /etc/systemd/resolved.conf file and add the DNS servers that you want to use. For example:

[Resolve]   DNS=8.8.8.8 8.8.4.4   

Save the file and restart the systemd-resolved service by typing the following command:

sudo systemctl restart systemd-resolved.service   

This will update the DNS settings for your system and prevents overwriting of resolv.conf.

Method 3: Use a script to update resolv.conf

Another solution is to use a script that automatically updates the file with a specific DNS server of our choice.

Here’s an example script that you can modify to your needs:

#!/bin/bash   
echo "nameserver 8.8.4.4" >> /etc/resolv.conf   

Open a text editor and paste the above script.

Modify the DNS servers to match your desired settings.

Save the file as something like update_dns.sh.

Make the script executable by typing the following command:

chmod +x /path/to/update_dns.sh 

Finally, run the script as root or with sudo whenever you want to update your DNS settings.

Method 4: Configure network interfaces to use specific DNS servers

If you’re using Network Manager to manage your network interfaces, you can configure them to use specific DNS servers instead of relying on resolv.conf. Here are the steps to follow:

Open the /etc/NetworkManager/NetworkManager.conf file and add the following line under the [main] section:

dns=none   

This line disables Network Manager from managing the resolv.conf file.

Next, open the configuration file for the interface you want to configure. For example, if you want to configure the eth0 interface, open the file /etc/NetworkManager/system-connections/Wired\ Connection\ 1.

In the configuration file, add the following lines under the [ipv4] section:

method=auto   dns=8.8.8.8;8.8.4.4;   

Save the file and restart the Network Manager service by typing the following command:

sudo systemctl restart NetworkManager.service   

This will configure the eth0 interface to use our new DNS servers. Further it prevents automatic update of resolv.conf.

Conclusion

In conclusion, in this article, we have seen how to stop resolv.conf from being overwritten in Linux. It is an important configuration file in Linux. Since it automatically overwrites it can cause problems with your DNS settings. However, with the methods in this article, you can assure that it’s not updated without our consent. Also, we can ensure our DNS settings stay intact.

Whether you choose to make the file read-only, use systemd-resolved, use a script to update resolv.conf, or configure network interfaces to use specific DNS servers, you can rest assured that your DNS settings will remain unchanged.


Share