The Domain Name System (DNS) translates text-based domain names to numeric IP addresses. By default, most networks are configured to work with DNS servers supplied by the internet service provider. However, users are free to change the DNS nameservers.
This tutorial will show you how to change DNS nameservers on your Ubuntu machine using the command line.
Go to the Netplan directory
cd /etc/netplan
List the directory contents with ls to see the name of the yaml file containing network configuration. Checking the /etc/netplan folder contents. Open the file in a text editor. Your file may have a different name.
sudo nano 01-network-manager.yaml
Ethernet connections are listed in the ethernet section of the file. If there are any wireless connections, you can find them in the wifis section. Netplan stores the current DNS configuration parameters in nameservers subsections of each section.
Replace the addresses located in the file with the DNS addresses you want to use. You can enter more than two addresses. Save the changes and exit.
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
Note: The file on your system may lack the entire ethernets or wifis section. In that case, add the lines that are missing, making sure you respect the indentation provided in the example.
Apply the changes you made in the config file:
sudo netplan apply
If the operation is successful, there is no output. To check if the system successfully applied the changes, type
systemd-resolve --status | grep 'DNS Servers' -A2
The output lists your current DNS servers
Confirming the successful configuration of new DNS nameservers using the resolvectl command.
Why Change DNS Nameserver on Ubuntu?
In most cases, your default DNS settings offer optimal performance. However, there are scenarios in which you should consider switching to a third-party DNS provider.