How to configure DNS nameservers on Ubuntu 18.04
On most Linux distributions you configure DNS nameservers in /etc/resolv.conf. However in recent versions of Ubuntu that changed with the introduction of Netplan.
Netplan is configured by editing YAML files located in /etc/netplan/. The precise name of this file will depend on your system configuration. I’m using an Ubuntu droplet on Digital Ocean, and the file is called 50-cloud-init.yaml.
Configure DNS nameservers
-
Open the
.yamlconfiguration file in/etc/netplanusing your favourite text editor. -
In the
ethernetssection locate the network adapter you want to change the nameservers on (e.g.eth0) -
Add a
nameserverssection and list the addresses of the DNS nameservers you want to use, as shown in the example below:1 2 3 4 5 6 7 8 9network: version: 2 ethernets: eth0: nameservers: addresses: - 1.1.1.1 - 9.9.9.9 # additional configuration omitted -
Save and close the Netplan configuration.
-
To test your new configuration run:
1sudo netplan tryThis will temporarily apply your new network configuration and automatically revert to the old one after 120 seconds. This lets you check your new configuration hasn’t broken everything. If you’re connected via SSH, and the connection hasn’t broken, then that’s a good sign. To accept the new configuration press
Enter. -
To check that your DNS server configuration has changed run:
1sudo systemd-resolve --statusScroll down until to find your network interface. You should see something similar to:
1 2 3 4 5 6 7 8Link 2 (eth0) Current Scopes: DNS LLMNR setting: yes MulticastDNS setting: no DNSSEC setting: no DNSSEC supported: no DNS Servers: 1.1.1.1 9.9.9.9 -
A more thorough way to check you’re using DNS from the provider you’ve configured is to run a DNS query and get the IP address of the server that was used. An easy way to do this is to run the following DNS query:
1nslookup -type=txt resolver.dnscrypt.infoThe
txtrecord will contain the IP address of the resolver that was used. For me this returned:1resolver.dnscrypt.info text = "Resolver IP: 162.158.110.213"A
who.islookup on this IP address reveals it belongs to Cloudflare, which suggests1.1.1.1is indeed being used to resolve DNS queries.