Change default nameserver in Linux

David Ngugi - Feb 27 - - Dev Community

My internet provider provides a wifi router that is pre-configured with DHCP that also provides the nameserver as their internal DNS server. There is a ton of info about why you wouldn't want this and they revolve about 2 main issues; security and performance.

Fedora 40

On Fedora, the wifi is managed by Network manager. I chose to use cloud-dns with encryption so that no-one in the middle can see what I am looking up on the internet. I chose cloud-dns because they tend to be more performant than Google nameservers.

Additionally, I also want to persistent these changes to all wifi connections regardless of which wifi network I use which is handy for laptops on the move.

Create a global NetworkManager configuration file

sudo nano /etc/NetworkManager/conf.d/dns-override.conf
Enter fullscreen mode Exit fullscreen mode

Add the following content

[main]
dns=none
Enter fullscreen mode Exit fullscreen mode

This tells NetworkManager not to use DHCP-assigned DNS.

Override DNS for all Wi-Fi connections

sudo nano /etc/NetworkManager/conf.d/dns-servers.conf
Enter fullscreen mode Exit fullscreen mode

Add:

[global-dns]
servers=1.1.1.2,1.0.0.2
Enter fullscreen mode Exit fullscreen mode

Enable DNS Over TLS
Open sudo nano /etc/systemd/resolved.conf and copy the following in

[Resolve]
DNS=1.1.1.2 1.0.0.2
DNSOverTLS=yes
Enter fullscreen mode Exit fullscreen mode

Restart systemd-resolved and NetworkManager

sudo systemctl restart systemd-resolved
sudo systemctl restart NetworkManager
Enter fullscreen mode Exit fullscreen mode

Debian based Linux

Disable DHCP-provided DNS from Wi-Fi Interface
Edit the sudo nano /etc/systemd/network/20-wireless.network
configuration for your interface (if applicable):

[Network]
DNS=1.1.1.2 1.0.0.2
DNSOverTLS=yes
Domains=~.
Enter fullscreen mode Exit fullscreen mode

Restart networking services

sudo systemctl restart systemd-networkd
Enter fullscreen mode Exit fullscreen mode
. .