dotfiles/dns.md
2026-02-05 15:52:40 +01:00

2.0 KiB

How to Manually Manage Your DNS Configuration (/etc/resolv.conf)

1. Introduction

On a standard Linux system, the /etc/resolv.conf file is managed automatically by network services. However, if your system is overriding your preferred DNS settings, you may need to take manual control.

This guide explains how to "unlock" the file to make changes and "lock" it again to prevent the system from overwriting your manual configuration. The chattr command is used to change file attributes, specifically the immutable flag.


2. The Workflow: Unlock, Edit, Lock

The process always follows these three steps:

  1. Unlock (Make Mutable): Remove the immutable flag so you can write to the file.
  2. Edit: Make your desired changes to the DNS servers.
  3. Lock (Make Immutable): Set the immutable flag again to protect the file from being overwritten.

3. The Commands

Step 1: Unlock the File (Make it Editable)

To make changes to /etc/resolv.conf, you must first remove the immutable flag (-i).

sudo chattr -i /etc/resolv.conf

Step 2: Edit the File

Once the file is unlocked, you can edit it with any command-line text editor. Using nano is a common and straightforward choice.

sudo nano /etc/resolv.conf

Inside the editor, add or change the nameserver lines to your desired DNS providers. For example:

# Primary DNS (Cloudflare)
nameserver 1.1.1.1
# Secondary DNS (Google)
nameserver 8.8.8.8
# Tertiary DNS (Quad9)
nameserver 9.9.9.9

Save the file and exit the editor. (In nano, you do this by pressing Ctrl+X, then Y to confirm, and Enter to save).

Step 3: Lock the File (Make it Read-Only)

This is the most important step. To prevent the system from overwriting your changes, you must make the file immutable again by adding the immutable flag (+i).

sudo chattr +i /etc/resolv.conf

Once this command is run, your DNS settings are saved and protected. The changes take effect immediately.