1. Overview

DNS (Domain Name System) helps link domain names to IP addresses for general networking. Setting up the right DNS addresses is usually necessary for fast and secure web browsing. Sometimes, our Internet service provider (ISP) may not provide us with the best or safest DNS servers. To remedy this, we can use third-party DNS providers.

The default local DNS server in Ubuntu 22.04 is systemd-resolved. This service can resolve domain names on the local area network.

Also, systemd-resolved creates and manages the DNS configuration file /etc/resolv.conf automatically based on different sources. As a result, if we make any manual changes to resolv.conf, they’ll be lost on reboot.

In this tutorial, we’ll see ways to permanently set up DNS configuration in Ubuntu 22.04:

  • using command-line tools
  • by changing settings via GUI
  • by installing the Resolvconf package

We’ll also look at the priority for each method.

2. Setting DNS Nameserver

Linux systems can have different DNS-related programs such as Network Manager, Resolvconf, systemd-resolved, and others. Sometimes, these programs don’t work well together, which can mess up DNS resolution.

Many DNS-configuring programs, with the exception of Network Manager, are unaware of one another’s existence. This could also cause DNS resolution issues on our system.

For instance, the default program might undo any changes we make to the resolv.conf file after a short while. Also, it can reverse them when the system starts up.

To resolve and prevent many DNS issues, we should stop all DNS-managing programs except the one we want to use.

3. Configuring DNS From the Command Line

One way to set DNS in Ubuntu 22.04 is by adding nameservers to the resolv.conf file using the command line.

3.1. Populate /etc/resolv.conf

Let’s use the echo command with redirection to add our custom DNS nameserver address:

$ sudo echo "nameserver 8.8.8.8" >> /etc/resolv.conf

However, when the system restarts, systemd-resolved overwrites any changes in resolv.conf. Therefore, manual modification won’t make changes permanent. To ensure they are, we must stop other programs from managing DNS.

3.2. Prevent Automatic DNS Management

So, let’s stop Network Manager from managing DNS lookups. For this, we change the main configuration file of Network Manager.

First, we open the file at /etc/NetworkManager/NetworkManager.conf with the vim command. Here, we change the DNS field to none:

$ sudo vim /etc/NetworkManager/NetworkManager.conf
[main]
dns=none
...

Finally, we restart the Network Manager:

$ sudo systemctl restart NetworkManager.service

Next, we disable the systemd-resolved service:

$ sudo systemctl disable --now systemd-resolved.service

Similarly, we’ll need to disable any other service automatically changing the resolv.conf file. As a result, our updates to the resolv.conf file should now be permanent.

4. Setting DNS Preferences via Graphical Interface

We can also write to the resolv.conf file using system settings. For this, we set DNS servers through the graphical interface.

However, in this case, we’ll need to keep systemd-resolved running:

$ sudo systemctl enable systemd-resolved
$ sudo systemctl start systemd-resolved

After that, we can perform the steps to set up our preferred DNS servers:

  1. Go to the Network or Wi-Fi menu in Settings
  2. Pick the network connection and click the gear icon
  3. Turn off the Automatic option for DNS in the IPv4 section
  4. Enter the DNS nameserver to use

We can also add multiple DNS servers. For this, we enumerate the DNS addresses with commas between them.

Now, to make these changes work, we need to restart the networking. We can do this using the command line or system settings:

$ sudo systemctl restart NetworkManager

To confirm the changes, we can look at the contents of the resolv.conf file:

$ cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 8.8.8.8

At this point, the resolv.conf file contains the DNS entry we chose in the system settings. If no other DNS service modifies it, this entry should remain.

5. Using Resolvconf

Let’s now use the Resolvconf package for our DNS configuration in resolv.conf.

5.1. Installation

First, we install the resolvconf package :

$ sudo apt install resolvconf

Next, we enable and start the resolvconf service:

$ sudo systemctl enable resolvconf.service
$ sudo systemctl start resolvconf.service

Then, we can check the status of the service:

$ sudo systemctl status resolvconf.service

After the installation is complete, we can configure Resolvconf.

5.2. Configuration

Now, we can add our nameservers to /etc/resolvconf/resolv.conf.d/head:

$ cat /etc/resolvconf/resolv.conf.d/head
nameserver 8.8.8.8 

Then, we can use the -u or update switch of resolvconf to write the changes to resolv.conf:

$ sudo resolvconf -u

To verify our modifications, we again check resolv.conf:

$ cat /etc/resolv.conf
...
nameserver 8.8.4.4
nameserver ...

Thus, the Resolvconf package is now managing DNS resolution.

Also, the Resolvconf and GUI approaches can work side by side.

6. Problems With Netplan

Netplan is a network configuration tool. It uses a YAML configuration file to read and define network configurations like IP addresses, DNS servers, and others.

Then, it renders the YAML configuration file into a system-specific network configuration format. With Network Manager installed, Netplan uses network-manager as a renderer and controls network interfaces and connections.

Netplan should be able to configure all aspects of the network configuration, including the DNS settings. However, currently, Netplan has some issues setting up DNS nameservers.

Even if we set the nameserver in the Netplan config file, it isn’t read by the resov.conf file. systemd-resolved still writes to the resolv.conf file and controls DNS resolution.

In short, Netplan doesn’t currently write to the resolv.conf file.

7. Conclusion

In this article, we’ve looked at different ways to set up DNS in Ubuntu 22.04.

First, we saw how to edit the resolv.conf file. Here, the changes persisted even when the system restarts. Next, we used the system GUI settings to set the DNS nameservers.

Finally, we employed the Resolvconf package to write to the resolv.conf file. In conclusion, Resolvconf is a good way to read or write the resolv.conf file, since it’s easy to use and doesn’t require stopping the systemd-resolved service.

Comments are closed on this article!