How to Make /etc/resolv.conf Changes Permanent in Modern Linux

If you are a system administrator managing web hosting environments like Plesk, you have likely encountered one of the most notoriously frustrating behaviors in modern Linux distributions. You open your terminal, edit your DNS settings to point to your local nameserver, save the file, and everything works perfectly.

But then, you reboot the server or restart the network service, and suddenly, your DNS resolution breaks. You open the file again, and your manual edits are completely gone, replaced by the system defaults.

If you are constantly forced to keep adding nameserver 127.0.0.1 to your /etc/resolv.conf file, you are not alone. In this comprehensive guide, we will explore exactly why this happens and provide the definitive steps to make /etc/resolv.conf changes permanent on both Debian/Ubuntu and Red Hat/AlmaLinux based systems.

Why Do My DNS Settings Keep Disappearing?

To understand the solution, we first need to understand the root cause of the problem. Historically, Linux administrators configured DNS directly by editing the /etc/resolv.conf file. If you wrote a nameserver IP address in that file, the system respected it unconditionally.

However, modern Linux distributions have fundamentally changed how network configuration is handled. Today, operating systems use dynamic network management tools—such as systemd-resolved (common on Ubuntu and Debian) or NetworkManager (the standard for CentOS, Rocky Linux, and AlmaLinux 9/10).

These modern services are designed to automatically regenerate the /etc/resolv.conf file every time the server boots up, an interface goes online, or a DHCP lease is renewed. In fact, on many modern servers, /etc/resolv.conf is no longer a static text file at all; it is simply a symbolic link pointing to a dynamically generated file managed by the system.

This automatic regeneration process completely wipes out any manual edits you make. That is why you often see a warning at the top of the file stating: “DO NOT EDIT THIS FILE BY HAND — YOUR CHANGES WILL BE OVERWRITTEN.”

The Goal: Setting the Localhost Nameserver (127.0.0.1)

In hosting environments, particularly those running control panels like Plesk, it is highly recommended to use a local DNS resolver. Setting your primary nameserver to 127.0.0.1 (which represents “localhost”) tells your server to query its own internal DNS service (usually BIND) before asking external providers.

Using a local resolver dramatically speeds up DNS lookups and is absolutely critical for mail servers utilizing DNS-based Blackhole Lists (DNSBL) to filter out spam.

To make this change permanent, you must stop fighting the /etc/resolv.conf file and instead configure your server’s underlying network manager directly. The instructions differ depending on the specific operating system your server is running.


Option A: How to Make /etc/resolv.conf Changes Permanent in Ubuntu & Debian

If your server runs on a Debian or Ubuntu-based system, network name resolution is typically handled by systemd-resolved. Instead of bypassing it, we need to instruct this service to prioritize our localhost IP.

Follow these steps to permanently set your DNS:

Step 1: Edit the systemd-resolved Configuration

First, you need to open the global configuration file for the resolved service. Log into your server via SSH as the root user (or a user with sudo privileges) and open the file using your preferred text editor, such as nano:

sudo nano /etc/systemd/resolved.conf

Step 2: Update the DNS Entry

Once the file is open, look for the [Resolve] section. You will likely see a list of commented-out options (lines starting with a # symbol).

Locate the DNS= line. You need to uncomment it by removing the # at the beginning of the line, and then append 127.0.0.1 to it. If the line does not exist, simply create it directly under the [Resolve] header.

Your configuration should look exactly like this:

[Resolve]
DNS=127.0.0.1
#FallbackDNS=
#Domains=
#LLMNR=no

Save the file and exit the text editor (in nano, press CTRL+O, hit Enter to confirm, and then CTRL+X to exit).

Step 3: Restart the Service to Apply Changes

For the system to recognize your new configuration and write it to the dynamic resolv.conf file, you must restart the systemd-resolved service:

sudo systemctl restart systemd-resolved

Verification:
Run the command cat /etc/resolv.conf. You should now see that nameserver 127.0.0.1 appears at the top of the list, generated securely by systemd. It will now survive all future reboots.


Option B: How to Make /etc/resolv.conf Changes Permanent in CentOS, AlmaLinux & Rocky Linux

For Enterprise Linux distributions like CentOS, Rocky Linux, and especially newer versions like AlmaLinux 9 and 10, network interfaces are managed strictly by NetworkManager. Directly editing legacy network scripts is deprecated, so we must use the nmcli (NetworkManager Command Line Interface) tool to ensure our changes are permanent.

Step 1: Find Your Network Connection Name

Before you can change the DNS, you need to identify the exact name of your active network interface. Run the following command in your terminal:

nmcli connection show

This will output a table of your network connections. Look under the NAME column for your primary active connection. It might be named something like eth0, ens192, eno1, or sometimes System eth0. Note this exact name down.

Step 2: Update the DNS Settings via nmcli

Now, you will use the nmcli command to modify the IPv4 DNS settings of that specific connection. This command tells NetworkManager to prioritize the localhost IP.

Run the following command, making sure to replace [ConnectionName] with the exact name you found in the previous step (keep the quotation marks if the name contains spaces):

nmcli connection modify "[ConnectionName]" ipv4.dns "127.0.0.1"

(Example: nmcli connection modify "eth0" ipv4.dns "127.0.0.1")

Step 3: Apply and Push the Changes

Modifying the connection saves the configuration to the disk, but it does not apply it to the active network state immediately. To push the changes through to your /etc/resolv.conf file, restart the NetworkManager service:

sudo systemctl restart NetworkManager

Verification:
Run cat /etc/resolv.conf. You can now confirm that nameserver 127.0.0.1 is permanently present and positioned at the top of the active nameserver list.


CRITICAL: External DNS Resolution Warning

While configuring your server to use 127.0.0.1 as the primary nameserver is best practice for Plesk and cPanel environments, it introduces a significant risk if your local DNS server is not configured correctly.

You must ensure that your local BIND DNS server is properly configured to forward external queries.

When you set your nameserver to localhost, your server asks itself how to find websites. If it asks “Where is google.com?” and your local BIND server does not know the answer—and is not allowed to ask external providers like Google (8.8.8.8) or Cloudflare (1.1.1.1)—the query will fail.

If external forwarding is misconfigured or disabled, your Plesk server will lose the ability to resolve external domains entirely. This leads to catastrophic server issues, including:

  • The inability to download OS updates (yum update or apt-get update will fail).
  • The inability to verify software licenses.
  • Total failure of outbound email delivery, as your mail server will not be able to resolve MX records for external domains like Gmail or Outlook.

How to Test Your Configuration

Before you close your terminal and consider the job done, you must verify that external resolution is still functioning through your local resolver.

Run a simple ping test to an external domain:

ping -c 4 google.com

If you receive replies with an IP address, your forwarders are working perfectly. If you receive a Temporary failure in name resolution error, you must immediately revert your DNS changes or fix your BIND forwarding configuration in your hosting control panel.

Conclusion

Fighting against automated network managers is a battle you will lose every time you reboot your server. By understanding how systemd-resolved and NetworkManager operate, you can properly inject your custom settings into the system configuration.

Whether you are optimizing a mail server on Debian or managing a robust Plesk hosting environment on AlmaLinux, knowing how to make /etc/resolv.conf changes permanent is a fundamental skill for any modern Linux system administrator. Apply these configurations, configure your forwarders correctly, and you will never have to manually edit that file again.

Leave a Comment

Your email address will not be published.