...

...

Skip to Content

How to change DNS settings on Linux

Using resolv.conf

This method works with most Linux distributions.

Edit resolv.conf

Terminal
sudo vim /etc/resolv.conf

Add DNS Servers

Replace the existing nameserver lines with:

IPv4

/etc/resolv.conf
nameserver 185.222.222.222 nameserver 45.11.45.11

IPv6

/etc/resolv.conf
nameserver 2a09:: nameserver 2a11::

Prevent Automatic Overwrites (Optional)

Some systems may overwrite /etc/resolv.conf automatically. To prevent this, you can lock the file.

Install e2fsprogs:

Terminal
sudo apt install e2fsprogs -y

Lock the file:

Terminal
sudo chattr +i /etc/resolv.conf

To unlock later, use sudo chattr -i /etc/resolv.conf.

Using Netplan (Ubuntu 18.04+)

Ubuntu 18.04 and later use Netplan for network configuration. The config file is located at /etc/netplan/*.yaml (e.g., 50-cloud-init.yaml or 01-netcfg.yaml).

Edit Netplan Configuration

Open your Netplan config file. A typical configuration looks like:

/etc/netplan/01-netcfg.yaml
network: version: 2 ethernets: eth0: addresses: - 192.0.2.2/24 gateway4: 192.0.2.1/24 match: macaddress: 01:23:45:67:89:AB nameservers: addresses: - 198.51.100.1 - 203.0.113.1 search: [] set-name: eth0

Update DNS Servers

Replace the nameservers section with:

YAML
nameservers: addresses: - 185.222.222.222 - 45.11.45.11

Apply Changes

Terminal
sudo netplan apply

Verify Configuration

Terminal
resolvectl status
Last updated on