Networking

Set custom DNS servers on Ubuntu 18.04 or 20.04

I’ll show you three methods to set custom DNS servers on Ubuntu including instructions to reset your connection back to defaults if you need to. Below each guide is a video version for further explanation.

Please note* this guide is for Ubuntu 18.04 or 20.04. If you need to set custom DNS servers on Ubuntu 16.04 or earlier, see my previous guide.

Set DNS servers with Network Manager

This is the easiest way to set custom DNS servers on Ubuntu and the preferred method because you’re not messing with any system files, which can lead to trouble if you’re not Linux savvy. Please note that each version of the Ubuntu GUI is slightly different so you may need to improvise. The screenshots below are from Ubuntu 20.04 LTS.

1. Click the Network icon top-right of your screen, then Wired Connected > Wired settings (Or Wi-Fi Adapter)

2. Click the Gear icon under the Wired/Wi-Fi heading

3. Click the IPv4 tab and enter your custom DNS server IP addresses, separated by a comma, then toggle the Automatic button and click Apply

4. (Optional) Click the IPv6 tab and enter your custom DNS server IP addresses separated by a comma, then toggle the Automatic button and click Apply
Note: If you don’t have IPv6 IP addresses you can disable IPv6 altogether with no negative effect: IPv6 Method > Disable > Apply

5. Toggle the LAN/Wi-Fi connection off and on

6. Open up a Terminal window and enter the following command:

systemd-resolve --status

…then hit the END key on your keyboard (or just keep hitting Enter till you get to the last line). You should see your Ethernet/W-Fi interface listed with the DNS servers you set earlier, mine is Link 2 (enp0s3)

If you don’t see your DNS servers listed, please make sure you followed all the steps in this guide. If you still have trouble, drop a comment further below.

Set DNS servers with Netplan, Network Manager and static IP

Note* As of 18/05/2020 Network Manager doesn’t respect the Netplan option nameservers: addresses [8.8.8.8,8.8.4.4] option even when you specify dhcp4-overrides: use-dns: false it still uses (and give priority to) the default DHCP DNS servers. This renders any custom DNS servers redundant. The only way around this AFAIK is to specify the Ethernet connection as static. Please comment or email me if you have found otherwise.

1. Open up a Terminal window and enter: ip a to list your current Ethernet/W-Fi connection details. Find your wired/Wi-Fi link name, mine is enp0s3 and just under that, you’ll see the IP address and netmask, mine is inet 192.168.1.114/24. Enter ip route show to reveal your default gateway, mine is default via 192.168.1.1 Keep this window open or write down the IP, netmask and default gateway.

2. Open File Manager and navigate to /etc/netplan, right-click and choose Open in Terminal. Enter the dir command, then select and copy the *.yaml file name to clipboard. Enter sudo nano [filename].yaml to open your Netplan config file (paste the file name from clipboard).

3. Copy and paste the code below and change everything in CAPS to your own, but please make sure you indent using the space bar not TAB otherwise YAML will throw an error.

network:
    version: 2
    renderer: NetworkManager
    ethernets:
       [DEVICE_NAME]:
          dhcp4: false
          addresses: [IP_ADDRESS/NETMASK]
          gateway4: GATEWAY_IP
          nameservers:
             addresses: [NAMESERVER_1, NAMESERVER_2]

My details are as follows:

network:
    version: 2
    renderer: NetworkManager
    ethernets:
       enp0s3:
          dhcp4: false
          addresses: [192.168.1.114/24]
          gateway4: 192.168.1.1
          nameservers:
             addresses: [8.8.8.8, 8.8.4.4]

Save the file ctrl+o Enter and exit ctrl+x

Now apply changes:

sudo netplan apply

If you get a warning you may have used TAB instead of space bar, or you have a syntax error, open the YAML file up and double check everything. If you get nothing but a new line on the command prompt, you’re good to go.

4. Once your YAML code has been applied without errors, enter systemd-resolve --status and confirm you’re using specified DNS servers:

Set custom DNS using Netplan and networkd

Using this method you’ll lose the Network Manager GUI and network icon, so only use if necessary (I recommend methods 1 or 2).

1. Open File Manager and navigate to /etc/netplan, right-click and choose Open in Terminal. Enter the dir command, then select and copy the *.yaml file name to clipboard. Enter sudo nano [filename].yaml to open your Netplan config file (paste the file name from clipboard).

2. Copy and paste the code below replacing CAPS with your values (take note the renderer is changed to networkd), but please make sure you indent using the space bar not TAB otherwise YAML will throw an error.

network:
    version: 2
    renderer: networkd
    ethernets:
       [DEVICE_NAME]:
          dhcp4: true
          nameservers:
             addresses: [NAMESERVER_1, NAMESERVER_2]

Mine looks like this:

network:
    version: 2
    renderer: networkd
    ethernets:
       enp0s3:
          dhcp4: true
          nameservers:
             addresses: [8.8.8.8, 8.8.4.4]

Save the file ctrl+o Enter and exit ctrl+x

Now apply changes:

sudo netplan apply

If you get a warning, it’s likely you’ve used TAB instead of space bar or you have a syntax error, open the YAML file up and double check everything. If you get nothing but a new line on command prompt, you’re good to go.

3. Once your YAML code has been applied without errors, enter systemd-resolve --status to confirm you’re using specified DNS servers:

Reset your Ethernet connection back to defaults

If you’ve run into trouble or just want to reset your connection back to defaults, follow these instructions (for methods 2 and 3 only).

1. Open File Manager and navigate to /etc/netplan, right-click and choose Open in Terminal. Enter the dir command, then select and copy the *.yaml file name to clipboard. Enter sudo nano [filename].yaml to open your Netplan config file (paste the file name from clipboard).

2. Remove everything, then copy/paste the following code, or enter it manually:

network:
    version: 2
    renderer: NetworkManager

…now save ctrl+o Enter and exit ctrl+x

3. Apply the changes:

sudo netplan apply

If you get any errors when applying the new config, you’ve likely used TAB instead of space bar or have a syntax error, so double check everything. If you get nothing but a new line on the command prompt, you’re good to go.

Sources and further reading

Share
Richie

I was a musician till the age of 30 with a keen interest in technology. I did a short web design course which sparked my interest in programming and in 2009 I launched Ricmedia Guitar, then Ricmedia PC Help. I now publish tech articles and tutorials on the main ricmedia.com domain.

View Comments

Recent Posts

Build a Mini-ITX NAS RAID Server with Enterprise SAS Hard Drives

Today I'll show you how to build a rock-solid home/office NAS server using a Mini-ITX… Read More

1 year ago

Reset Apple Safari to default settings

Since Safari version 9.0 (macOS El Capitan), Apple have removed the menu option to Reset… Read More

1 year ago

Enable God Mode on Windows 10

How would you like to see all the hidden expert settings available on your Windows… Read More

1 year ago

Perform disk error check (chkdsk) on Windows 10

Today, I'll show you how to run Windows Check Disk (chkdsk) to find and fix… Read More

1 year ago

Set permanent DNS nameservers on Ubuntu/Debian with resolv.conf

Setting custom DNS servers on Linux can increase performance, security and even thwart some websites… Read More

1 year ago

How to install Debian Linux – Complete Guide

Today, I'll show you how to download and install Debian Linux, simply follow each step… Read More

1 year ago