How do I configure DNS on Ubuntu Linux distros via the command line – it’s easier than you think

Follow ZDNET: Add us as a favorite resource on Google.
Highlights taken by ZDNET
- I use Netplan to customize DNS on Ubuntu-based distros.
- This CLI method may be complicated, but you will see its power.
- This method can be used on desktops and servers alike.
Every now and then, you may find yourself in a situation where you need to change DNS providers, and you cannot access the desktop GUI. Maybe you’re working with a remote PC, a home server, or maybe you just want to expand your Linux skills.
In any case, you may want to know how to configure DNS with the Netplan tool, which is a command-line program for configuring networks on Ubuntu-based desktops.
Before we get into it, know that configuring DNS the GUI way is very easy, so if you have access to the desktop environment, you can do this without touching the command line. If, on the other hand, you use SSH remotely from a machine (or the machine simply doesn’t have a GUI), be glad that the command line method is almost as easy (as long as you have a little knowledge of the command line).
Also: 7 Linux commands I can’t live without after 20 years at the airport
Fortunately, it’s not as difficult as you think. All you need to do is edit one file. Note that this works, regardless of whether your system is set to a DHCP IP address or a static IP address.
What is Netplan?
Netplan is used for network configuration in Linux systems. With this tool, you create a network interface definition, define what it should do, and then use it. Netplan files are placed in a YAML configuration and can be used on a server, desktop, cloud, or IoT installation. It also manages various backends, such as NetworkManager and systemd-networkd.
Netplan is configured with files inside the /etc/netplan directory, where you may or may not find many YAML files. Most desktops using DHCP will have only one YAML file, but it is not uncommon to have multiple files.
Also: 5 reasons why you should ditch Windows for Linux today
The structure of the configuration file is standard YAML, where you will have an option definition to the left of the colon and then an option to the right, like:
dhcp4: true
Remember that YAML is very picky about indentation, so be sure to follow the indentation structure of the file you’re editing. If you get an error when using the configuration file, it’s most likely caused by improper indentation.
Also: Linux desktop frozen? My go-to 5 tricks to try – before forcing a hard reboot
Another issue to consider is that on a desktop, your DNS is probably controlled by NetworkManager. You can override that functionality (to provide more flexibility) by adding the following to /etc/NetworkManager/NetworkManager.conf
dns=nothing
rc-manager=unmanaged
Save and close the file. Restart NetworkManager with:
sudo systemctl restarts NetworkManager
That being said, let’s get ready.
Preparing the Netplan file
Let’s assume that you will be using Cloudflare’s secure DNS addresses of 1.1.1.1 and 1.0.0.1.
Open your terminal application and change to the netplan directory with:
Show more
cd /etc/netplan
View directory contents with ls commandment. If you see multiple files, most likely, the one you want starts with 00. In my case, 00-netplan.yaml. Before editing the file, make a backup with the command:
Show more
sudo cp 00-netplan.yaml 00-netplan.yaml.bak
Be sure to edit the above command to match the file you need to edit.
Open the first file for editing:
Show more
Be sure to edit the above command to match the file you need to edit.
With the file open, look at the line:
servers:
That is the stage where DNS is configured. If you don’t see that line, you will add it. If you need to add it, put it above the dhcp4: true line. Here’s what the DNS class looks like, using Cloudflare addresses:
servers:
addresses:
– 1.1.1.1
– 1.0.0.1
Make sure in in nameservers is in the same column as d in dhcp4. That should ensure proper undoing. If not, you’ll have to adjust the offset according to your file structure.
After adding the line, save and close the file with the keyboard shortcut Crtl+X.
Show more
Using editing
To apply the new configuration, issue the command:
sudo netplan is running
If you get any errors, go back and modify the namserver section’s undo to make sure it’s done correctly; if not, your new DNS servers should be working now. To verify that DNS is working, issue the command:
ping google.com
If you see results, congratulations, DNS is working fine. To verify whether the new addresses are being used, issue the command:
solvectl status | grep “DNS”
If you see old DNS addresses, reboot your machine, and everything should be fine.
Also: 5 of my favorite Linux distros that are ready to use out of the box – no setup required



