How to Set DNS Servers on Ubuntu Linux
In this article
Setting custom DNS servers on Ubuntu-based distributions is a straightforward process that improves privacy, bypasses ISP restrictions, and can speed up name resolution. The modern standard method involves using Netplan, the configuration utility for NetworkManager and systemd-networkd. You will need terminal access with sudo privileges and the IP addresses of your chosen DNS providers (e.g., Google 8.8.8.8 or Cloudflare 1.1.1.1).
What You'll Need
- A terminal window (Ctrl+Alt+T)
- Sudo user privileges
- The IP addresses of your preferred DNS providers
- Knowledge of your active network interface name (usually eth0, enp3s0, or wlp2s0)
Step-by-Step
- Identify Your Network Interface: Open the terminal and run
ip route | grep default. Look at the output; the word following "dev" is your interface name (e.g.,eth0). Note this down. - Locate Netplan Configurations: List the YAML files in the Netplan directory by running
ls /etc/netplan/. You will typically see one file named something like00-installer-config.yamlor50-cloud-init.yaml. - Edit the Configuration File: Open the identified file with a text editor. For example:
sudo nano /etc/netplan/00-installer-config.yaml. Be careful with indentation; YAML is sensitive to spaces. - Add DNS Settings: Inside the
network:section, under theethernets:subsection for your interface, add thenameservers:key. It should look like this:network: version: 2 ethernets: eth0: dhcp4: no nameservers: addresses: [8.8.8.8, 1.1.1.1]If you use DHCP, keepdhcp4: yesbut still add thenameserversblock below it. Note that menu labels can differ slightly by version, device, or region, so ensure you match the existing structure of your file. - Apply the Changes: Save the file (Ctrl+O, Enter, then Ctrl+X in Nano). Apply the new configuration by running
sudo netplan apply. - Verify the Change: Check if the settings applied without error. You can verify the active DNS by running
systemd-resolve --statusand looking for the "DNS Servers" line under your interface.
Common Mistakes
The most frequent error is incorrect YAML indentation. Netplan requires strict spacing; mixing tabs and spaces or misaligning the addresses list will cause the netplan apply command to fail. Another mistake is forgetting to specify both primary and secondary DNS servers, which can leave you vulnerable if the first server goes offline. Finally, users often forget to replace the placeholder interface name (like eth0) with their actual interface name identified in Step 1.
Tips
Always back up your original Netplan configuration file before editing it. Run sudo cp /etc/netplan/your-file.yaml /etc/netplan/your-file.yaml.bak so you can restore it if you lock yourself out of the network. Use public DNS providers known for reliability and privacy, such as Cloudflare (1.1.1.1) or Quad9 (9.9.9.9), rather than generic ISP defaults which may log your activity. If you are on a laptop moving between networks, consider using a tool like resolvconf or NetworkManager's GUI settings instead of static Netplan configs, as Netplan is primarily designed for static or cloud-init managed setups.
Troubleshooting
If sudo netplan apply returns an error, check the syntax of your YAML file. Indentation errors are the usual culprit. If the network fails to connect after applying changes, revert to your backup file immediately. If DNS resolution works but internet access does not, check your gateway IP address in the Netplan config. Ensure the gateway4 (or routes in newer versions) is correctly set. Restart the networking service with sudo systemctl restart networking or reboot the system. If you cannot access the terminal due to network loss, boot into recovery mode or use a live USB to edit the config file from another OS.
Frequently Asked Questions
Does changing DNS change my IP address?
No. DNS translates domain names to IP addresses. Changing DNS servers does not alter your public IP address assigned by your ISP, though it may hide your browsing history from your ISP.
Can I use Netplan for Wi-Fi connections?
Yes, but the syntax differs slightly. Under the wifis: key instead of ethernets:, you must include the SSID and password within the configuration block.
Why did my internet stop working after changing DNS?
This usually indicates a typo in the YAML file or an incorrect gateway address. Revert to the previous configuration file to restore connectivity.
Is it safe to use Google DNS?
Yes, Google DNS (8.8.8.8) is widely used and generally reliable. However, it logs query data, so for stricter privacy, consider providers like Quad9 or Cloudflare.
How do I revert to automatic DNS?
Remove the nameservers block from your Netplan YAML file and run sudo netplan apply. The system will fall back to DHCP-assigned DNS servers.
Does this work on all Ubuntu versions?
Netplan is standard in Ubuntu 17.10 and later. Older versions used ifupdown or NetworkManager directly via /etc/resolv.conf, which is now a symlink and not recommended for manual editing.