Change DNS Servers (IPv4, IPv6) on Linux

mail
Cyrus Kao
Last modified

When I set up a new machine, the first thing to do is probably change my DNS (Domain Name System) server to Google's 8.8.8.8 since the DNS servers of my ISP (Internet Service Provider) is unbearably slow. Nowadays, there are even more options like Cloudflare's 1.1.1.1, and IPv6 adoption is working its way up too.

In my opinion, changing DNS servers on Linux is even easier than Windows. As you only need to do it once and it applies to all the network interfaces. In this guide we'll be covering configure DNS servers on all Linux distros and prevent DNS settings from changing by other services.

Backup DNS Configuration

Backup the current DNS configuration to /etc/resolv.conf.backup in case something went wrong:

sudo cp /etc/resolv.conf /etc/resolv.conf.backup
Bash

Change DNS Servers

Remove the immutable flag on DNS configuration /etc/resolv.conf so we can edit it:

sudo chattr -i /etc/resolv.conf
Bash

Open /etc/resolv.conf with the text editor of your choice:

  • vim (command-line editor for advanced user)

    sudo vim /etc/resolv.conf
    Bash
  • nano (easy to use command-line editor)

    sudo nano /etc/resolv.conf
    Bash
  • gedit (editor with graphical interface, default text editor of Ubuntu and distros with GNOME)

    sudo gedit /etc/resolv.conf
    Bash

    or

    gedit admin:/etc/resolv.conf
    Bash
    /etc/resolv.conf
    /etc/resolv.conf opened with gedit

Then add or edit your desired DNS servers (be aware, maximum of 3 nameserver are allowed):

nameserver 2606:4700:4700::1111
nameserver 2606:4700:4700::1001
nameserver 1.1.1.1
Plain text

DNS servers are used in the order in /etc/resolv.conf. Make sure to put IPv6 servers in the front if it's preferred.

Commonly Used DNS Servers

Here are some commonly used DNS servers for your reference:

  • Cloudflare DNS

    • IPv4
      • 1.1.1.1
      • 1.0.0.1
    • IPv6
      • 2606:4700:4700::1111
      • 2606:4700:4700::1001
  • Google DNS

    • IPv4
      • 8.8.8.8
      • 8.8.4.4
    • IPv6
      • 2001:4860:4860::8888
      • 2001:4860:4860::8844

Prevent DNS Settings From Being Changed

/etc/resolv.conf is often maintained by service like NetworkManager, so when reboot it will all reset to default. To make the changes permanent, we can make the DNS configuration file immutable:

sudo chattr +i /etc/resolv.conf
Bash

To undo this:

sudo chattr -i /etc/resolv.conf
Bash

Comments

0500