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

netplanhero

Jack Wallen/ZDNET

Follow ZDNET: Add us as a preferred source on Google.


ZDNET’s key takeaways

  • I use Netplan to customize DNS on Ubuntu-based distros.
  • This CLI method may be daunting, but you’ll see its power.
  • This method can be used on desktops and servers alike.

Every once in a while, you might find yourself in a situation where you need to change DNS providers, and you don’t have access to a desktop GUI. You might be dealing with a PC remotely, an at-home server, or maybe you just want to up your Linux skills.

Either way, you might want to know how to configure DNS with the Netplan tool, which is the command-line system for configuring networking 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 a desktop environment, you could do this without ever touching the command line. If, on the other hand, you’ve used SSH to remote into a machine (or the machine simply has no GUI), be glad the command line method is almost as easy (as long as you have at least a modicum of experience with the command line).

Also: 7 Linux commands I can’t live without after 20 years in the terminal

Fortunately, it’s not as hard as you think. All you need to do is edit a single file. Do note that this does work, whether you have your system set up for a DHCP IP address or a static IP address.

What is Netplan?

Netplan is used for network configurations on Linux systems. With this tool, you create a description of a network interface, define what it should do, and then apply it. Netplan files are laid out in YAML configurations and can be used on server, desktop, cloud, or even IoT installations. It is also capable of controlling various backends, such as NetworkManager and systemd-networkd.

Netplan is configured via files within the /etc/netplan directory, where you may or may not find multiple YAML files. Most desktops using DHCP will only have one YAML file, but it’s not out of the ordinary to have multiple files.

Also: 5 reasons you should ditch Windows for Linux today

The layout of the config file is typical YAML, where you’ll have a description of an option on the left side of a colon and then the option on the right, such as:

dhcp4: true

Keep in mind that YAML is very picky about indentation, so make sure you follow the indentation layout of the file you are editing. If you get an error when applying a configuration file, most likely it’s caused by improper indentation.

Also: Linux desktop frozen? My 5 go-to tricks to try – before forcing a hard reboot

Another issue to think about is that on the desktop, your DNS is probably controlled by NetworkManager. You can override that functionality (to give you more flexibility) by adding the following to /etc/NetworkManager/NetworkManager.conf

dns=none
rc-manager=unmanaged

Save and close the file. Restart NetworkManager with:

sudo systemctl restart NetworkManager

With that said, let’s configure.

Configuring the Netplan file

Let’s assume you’ll be using Cloudflare’s secure DNS addresses of 1.1.1.1 and 1.0.0.1. 

Open your terminal app and change into the netplan directory with:


Show more

cd /etc/netplan

View what’s in the directory with the ls command. If you see multiple files, more than likely, the one you want begins with 00. In my case, it’s 00-netplan.yaml. Before you edit the file, make a backup with the command:


Show more

sudo cp 00-netplan.yaml 00-netplan.yaml.bak

Make sure to edit the above command to conform to the file you need to edit.

Open the original file for editing with:


Show more

Make sure to edit the above command to conform to the file you need to edit.

With the file open, look for the line:

nameservers:

That’s the section where DNS is configured. If you don’t see that line, you’ll add it. If you need to add it, place it above the dhcp4: true line. Here’s how the DNS section looks, using the Cloudflare addresses:

nameservers:
    addresses:
    – 1.1.1.1
    – 1.0.0.1

Make sure the n in nameservers is in the same column as the d in dhcp4. That should ensure proper indentation. If not, you’ll have to adjust the indentation according to your file layout.

After you’ve added the line, save and close the file with the Crtl+X keyboard shortcut. 


Show more

Applying the edits

To apply the new edits, issue the command:

sudo netplan apply

If you receive any errors, go back and alter the indentation of the namserver section to make sure it is indented properly; otherwise, your new DNS servers should now be in effect. To make sure DNS is functioning, issue the command:

ping google.com

If you see results, congratulations, DNS is working properly. To verify if the new addresses are being used, issue the command:

resolvectl status | grep “DNS”

If you see the old DNS addresses, restart your machine, and all should be good.

Also: 5 of my favorite Linux distros ready to use out of the box – no setup required

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *