
How to Permanently Set DNS Servers in Linux (Without Your Changes Being Overwritten)
If you’ve ever worked with a Linux system, you’ve likely encountered this common frustration: you manually edit the /etc/resolv.conf
file to set your preferred DNS servers, only to find your changes have vanished after a reboot or a network reconnect. This isn’t a bug; it’s a feature of modern Linux distributions that use network management services to dynamically control your system’s configuration.
Directly editing /etc/resolv.conf
is a temporary fix at best. To make your DNS settings permanent, you need to configure the service that’s actually in charge. This guide will show you the correct methods for achieving a stable, permanent DNS setup on your Linux machine.
Understanding the Problem: Why /etc/resolv.conf
Resets
On most modern Linux systems, the /etc/resolv.conf
file is not a static configuration file. Instead, it is dynamically generated and managed by a higher-level service. The two most common services responsible for this are:
systemd-resolved
: A system service that handles network name resolution. It’s standard on many modern distros like Ubuntu, Debian, and their derivatives.NetworkManager
: A comprehensive network management tool used by default in Fedora, CentOS, RHEL, and others.
These tools automatically configure DNS based on your network connection’s settings, often received from a DHCP server. When you manually edit the file, the managing service simply overwrites your changes the next time it refreshes the network state. You can often see a warning at the top of the file itself, stating something like, # This file is managed by man:systemd-resolved(8). Do not edit.
Step 1: Identify What’s Managing Your DNS
Before making any changes, you need to know which service is in control. A simple command can often reveal this. Open your terminal and run:
ls -l /etc/resolv.conf
If the output shows that /etc/resolv.conf
is a symbolic link (symlink) to a file like /run/systemd/resolve/stub-resolv.conf
, then your system is using systemd-resolved
. If it points to a NetworkManager
file or is a regular file on a system known to use NetworkManager, you’ll use that method instead.
Method 1: Configuring systemd-resolved
(The Modern Standard)
If your system uses systemd-resolved
, this is the cleanest and recommended approach. You will edit the service’s main configuration file, not /etc/resolv.conf
.
Edit the
resolved.conf
file with a text editor likenano
orvim
. You’ll need administrative privileges.sudo nano /etc/systemd/resolved.conf
Set your DNS servers. Inside the file, you will see a
[Resolve]
section. Find the line that starts with#DNS=
. Remove the#
to uncomment it and add the IP addresses of your desired DNS servers, separated by spaces. You can also set a fallback DNS.For example, to use Google’s DNS and Cloudflare’s as a fallback:
[Resolve] DNS=8.8.8.8 8.8.4.4 FallbackDNS=1.1.1.1 1.0.0.1
Pro Tip: Using trusted, public DNS resolvers like Google (8.8.8.8), Cloudflare (1.1.1.1), or Quad9 (9.9.9.9) can often provide faster and more secure browsing than your default ISP-provided DNS.
Save the file and exit the editor (in
nano
, pressCtrl+X
, thenY
, thenEnter
).Restart the
systemd-resolved
service to apply your new configuration.sudo systemctl restart systemd-resolved
Verify your changes. You can check the service’s status to see which DNS servers are now active.
resolvectl status
Your system will now permanently use the DNS servers you specified, even after a reboot.
Method 2: Configuring NetworkManager
If your system is managed by NetworkManager
, you can set the DNS servers through its command-line tool (nmcli
) or a graphical interface.
Using the Command Line (nmcli
)
Find your active connection name.
nmcli connection show
Look for your primary connection, often named something like
eno1
orWired connection 1
.Modify the connection to use manual DNS. Replace
"YourConnectionName"
with the name from the previous step. This command sets the DNS servers and, crucially, tells NetworkManager to ignore DNS settings provided by DHCP.sudo nmcli con mod "YourConnectionName" ipv4.dns "8.8.8.8 8.8.4.4" sudo nmcli con mod "YourConnectionName" ipv4.ignore-auto-dns yes
Restart the connection to apply the settings.
sudo nmcli con down "YourConnectionName" && sudo nmcli con up "YourConnectionName"
Your DNS settings are now saved permanently within that specific network profile.
Using the Graphical Interface (GUI)
If you prefer a graphical approach:
- Open your system’s Settings panel and navigate to the Network or Wi-Fi section.
- Find your active connection and click the gear icon to edit its settings.
- Go to the IPv4 tab.
- Switch the DNS setting from Automatic (DHCP) to Manual.
- Enter your desired DNS server IPs in the field provided, separated by commas (e.g.,
8.8.8.8, 1.1.1.1
). - Click Apply to save the changes.
The Last Resort: Making resolv.conf
Immutable
There is a “brute force” method that involves making the /etc/resolv.conf
file immutable, meaning it cannot be changed or deleted by any user or process, including the system itself.
Warning: This method is not recommended. It can interfere with the proper functioning of your network services and may cause unexpected issues, especially with VPNs or networks that require specific DNS servers. Only use this if all other methods have failed and you understand the risks.
- First, manually edit
/etc/resolv.conf
with your desired DNS servers.
bash
sudo nano /etc/resolv.conf
Add your nameservers:
nameserver 8.8.8.8
nameserver 1.1.1.1
- Use the
chattr
command to set the immutable flag.
bash
sudo chattr +i /etc/resolv.conf
The file can no longer be overwritten. If you ever need to change it again, you must first remove the immutable flag withsudo chattr -i /etc/resolv.conf
.
Conclusion
For a stable and predictable Linux system, it’s essential to work with its management tools, not against them. By configuring systemd-resolved
or NetworkManager
directly, you ensure that your preferred DNS settings persist across reboots and network changes. This approach is not only more robust but also the correct way to manage network configuration on a modern Linux distribution.
Source: https://kifarunix.com/make-permanent-dns-changes-in-resolv-conf-on-linux/