Set Up Third Party DNS on Linux: Step-by-Step
In this article
Setting up a third-party DNS on Linux changes which servers your system asks to resolve domain names. This can improve privacy, speed, or parental controls. The quickest way is to edit the network manager settings or the resolv.conf file. Below are the exact steps for most modern Linux distributions using NetworkManager, plus a manual method that works everywhere.
What You'll Need
- A Linux system with sudo or root access.
- The IP addresses of the DNS servers you want to use (e.g., Cloudflare 1.1.1.1 and 1.0.0.1, Google 8.8.8.8 and 8.8.4.4, or Quad9 9.9.9.9).
- An active network connection (Wi-Fi or Ethernet).
Step-by-Step
Method 1: Using NetworkManager (Most Desktops)
- Open the terminal (Ctrl+Alt+T).
- List your active connections:
nmcli connection show. Note the name of the connection you are using (e.g., "Wired connection 1" or your Wi-Fi SSID). - Set the DNS servers for that connection. Replace
1.1.1.1and1.0.0.1with your chosen servers, and replace"Wired connection 1"with your actual connection name:sudo nmcli connection modify "Wired connection 1" ipv4.dns "1.1.1.1 1.0.0.1" - Disable automatic DNS from DHCP (otherwise your router may override your setting):
sudo nmcli connection modify "Wired connection 1" ipv4.ignore-auto-dns yes - Apply the changes by reactivating the connection:
sudo nmcli connection down "Wired connection 1" && sudo nmcli connection up "Wired connection 1" - Verify the change:
nmcli device show | grep DNSorresolvectl status. You should see your chosen DNS servers listed.
Method 2: Manual /etc/resolv.conf (Works on Any System)
- Back up your current resolv.conf:
sudo cp /etc/resolv.conf /etc/resolv.conf.backup - Edit the file:
sudo nano /etc/resolv.conf - Replace the contents with your DNS servers. For example, for Cloudflare:
nameserver 1.1.1.1nameserver 1.0.0.1 - Save and exit (in nano: Ctrl+X, then Y, then Enter).
- Test immediately:
ping -c 4 google.comshould resolve and respond. - Note: If you use NetworkManager or systemd-resolved, this file may be overwritten on reboot or when the network reconnects. To make it permanent, you must disable the service that rewrites it (see Troubleshooting).
Method 3: Using systemd-resolved (Ubuntu 18.04+ and many others)
- Edit the systemd-resolved configuration:
sudo nano /etc/systemd/resolved.conf - Uncomment the
DNS=line and add your servers. For example:DNS=1.1.1.1 1.0.0.1 - Uncomment the
FallbackDNS=line if you want a backup. - Save and exit, then restart the service:
sudo systemctl restart systemd-resolved - Check status:
systemd-resolve --status(orresolvectl statuson newer versions). You should see your DNS servers under the active link.
Common Mistakes
- Forgetting to disable automatic DNS from DHCP. If you skip step 4 in Method 1, your router's DNS will override your setting after a reconnect.
- Using the wrong connection name. Always run
nmcli connection showfirst to get the exact name. - Editing /etc/resolv.conf directly on systems using systemd-resolved. The file is often a symlink to a generated file and will be overwritten.
- Typing a space instead of a comma between DNS servers in some tools. NetworkManager uses spaces, but some other tools use commas.
- Not testing after the change. Always run a ping or use
digto confirm resolution works.
Tips
- Use a DNS service that matches your needs: Cloudflare and Google prioritize speed; Quad9 blocks malicious domains; OpenDNS offers parental controls.
- Write down your original DNS servers (usually your router's IP, like 192.168.1.1) so you can revert easily.
- For a single session, you can test a DNS server without changing system settings using:
dig @1.1.1.1 google.com - If you use a VPN, note that the VPN may override your DNS settings. Check the VPN's own configuration.
- To verify your DNS is working, use
nslookup example.comand see which server answers.
Troubleshooting
If the change does not take effect or breaks your internet:
- Check if resolv.conf is a symlink: Run
ls -l /etc/resolv.conf. If it points to something like../run/systemd/resolve/stub-resolv.conf, then systemd-resolved is managing DNS. Use Method 3 instead. - Restart the network manager:
sudo systemctl restart NetworkManager(orsudo systemctl restart systemd-networkdif using that). - Flush the DNS cache:
sudo resolvectl flush-cachesorsudo systemd-resolve --flush-caches. - Test with a different tool:
host google.comornslookup google.comto see if the issue is with your browser's cache. - Check firewall: Some firewalls block DNS traffic to external servers. Temporarily disable your firewall (e.g.,
sudo ufw disable) to test, then re-enable and allow DNS (port 53) if needed. - If you edited /etc/resolv.conf manually and it gets overwritten on reboot: You need to disable the service that rewrites it. For NetworkManager, edit
/etc/NetworkManager/NetworkManager.confand adddns=defaultunder[main], then restart NetworkManager. For systemd-resolved, use Method 3 instead. - If nothing works and you lose internet: Revert to your original DNS by restoring the backup:
sudo cp /etc/resolv.conf.backup /etc/resolv.conf(if you made one) or set your router's IP as the DNS server.
Frequently Asked Questions
Will changing DNS make my internet faster?
Possibly, if your ISP's DNS is slow. But the difference is usually small unless you have a very slow DNS resolver. The main benefits are privacy and security.
Is it safe to use public DNS like 8.8.8.8?
Yes, Google's and Cloudflare's public DNS are widely used and trusted. They do log some data, so read their privacy policies if that matters to you.
Can I use different DNS for different networks?
Yes, with NetworkManager you can set DNS per connection profile. Each Wi-Fi or Ethernet connection can have its own DNS settings.
Do I need to restart my computer after changing DNS?
No, a network restart or service restart is enough. A full reboot is not required.
Why does my DNS change back after reconnecting?
Because your router or DHCP server is providing DNS automatically. You must disable automatic DNS (as in Method 1 step 4) or configure your router to not push DNS.
Can I use IPv6 DNS servers?
Yes, you can add IPv6 addresses like 2606:4700:4700::1111 (Cloudflare) in the same field, separated by spaces. Just make sure your network supports IPv6.