DNS Configuration on Linux: Permanently Fix Settings Being Lost After Reboot

Network tutorial - IT technology blog
Network tutorial - IT technology blog

A Classic Nightmare: You fix the DNS… then it’s gone after a reboot

Imagine you are managing a cluster of 20 Ubuntu servers for a web project. You excitedly open the /etc/resolv.conf file, add the line nameserver 8.8.8.8, save it, and see ping google.com responding in 20ms. However, after just one routine reboot or a network service restart, all your hard work goes down the drain. The resolv.conf file automatically reverts to its original state as if your changes never existed.

Why is Linux so “stubborn”? On modern distributions like Ubuntu 20.04/22.04, Fedora, or CentOS 8, /etc/resolv.conf is no longer a static file. It is managed automatically by systemd-resolved or NetworkManager. If you keep editing it manually, you will be stuck in a frustrating “fix-lose-fix” loop forever.

Standard DNS Configuration (For Ubuntu/Debian)

Instead of editing a temporary file, we need to intervene in the higher-level network management tools. Here are the two most common methods.

1. Using Netplan (For Servers)

Most Ubuntu Servers today use Netplan to manage the network. Look for the configuration file in /etc/netplan/. It is usually named 00-installer-config.yaml or something similar.

sudo nano /etc/netplan/00-installer-config.yaml

Edit the nameservers section. Note: YAML is extremely sensitive to indentation. Use exactly 2 or 4 spaces consistently.

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: true
      nameservers:
        addresses: [8.8.8.8, 1.1.1.1]

Apply the changes immediately with the command:

sudo netplan apply

2. Using nmcli (For Desktop or RHEL/CentOS)

If you are using the command line on machines with NetworkManager installed, nmcli is the fastest tool.

# Find the connection name (usually 'Wired connection 1' or 'ens33')
nmcli connection show

# Configure new DNS
nmcli con mod "Wired connection 1" ipv4.dns "8.8.8.8 1.1.1.1"

# Activate the changes
nmcli con up "Wired connection 1"

The Core: The Relationship Between systemd-resolved and resolv.conf

To become a pro, you need to understand the underlying mechanism instead of just memorizing commands.

What role does systemd-resolved play?

It acts as an internal DNS proxy. Instead of applications reaching out directly to the Internet for IPs, they ask systemd-resolved at the address 127.0.0.53

Share: