Change DNS Servers (IPv4, IPv6) on Linux
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 in Windows. As you only need to do it once and it applies to all the network interfaces. In this guide we'll be covering how to 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 goes wrong:
$ sudo cp /etc/resolv.conf /etc/resolv.conf.backup
Change DNS Servers
Remove the immutable flag on DNS configuration /etc/resolv.conf
so we can edit it:
$ sudo chattr -i /etc/resolv.conf
Open /etc/resolv.conf
with the text editor of your choice:
-
vim
(command-line editor for advanced user)$ sudo vim /etc/resolv.conf
-
nano
(easy to use command-line editor)$ sudo nano /etc/resolv.conf
-
gedit
(editor with graphical interface, default text editor of Ubuntu and distros with GNOME)$ sudo gedit /etc/resolv.conf
or
$ gedit admin:/etc/resolv.conf
Then add or edit your desired DNS servers (be aware, maximum of 3 nameserver
are allowed):
/etc/resolv.confnameserver 2606:4700:4700::1111
nameserver 2606:4700:4700::1001
nameserver 1.1.1.1
Plain text
DNS servers are used in the original order in
/etc/resolv.conf
. Make sure to put the IPv6 servers in the front if it's preferred.
Commonly Used DNS Servers
Here are some commonly used DNS servers for your reference:
-
- IPv4
1.1.1.1
1.0.0.1
- IPv6
2606:4700:4700::1111
2606:4700:4700::1001
- IPv4
-
- IPv4
8.8.8.8
8.8.4.4
- IPv6
2001:4860:4860::8888
2001:4860:4860::8844
- IPv4
Prevent DNS Settings From Being Changed
/etc/resolv.conf
is often maintained by services like NetworkManager
, so when rebooted 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
To undo this:
$ sudo chattr -i /etc/resolv.conf