HomeLinuxHow to change DNS server permanently on Ubuntu 20.04?

How to change DNS server permanently on Ubuntu 20.04?

change dns server permanently

In Ubuntu 20.04, I encountered a common issue with DNS server resolution management. The system component responsible for this, systemd-resolved, dynamically updates the /etc/resolv.conf file based on its own configuration, often leading to changes appearing temporary and reverting to systemd-resolved settings. In this article, I will share how I resolved this issue and also explore the other different methods that can use to effectively manage DNS resolution on Ubuntu 20.04 system.

In this article we will discuss about :

a. Editing resolved.conf

b. Creating a Custom resolv.conf

c. Locking the /etc/resolv.conf File

d. Disabling systemd-resolved

e. Using “resolvconf” utility

Understanding systemd-resolved

As I started to delve into the issue further, I quickly realized that systemd-resolved is the core component responsible for managing DNS resolution on my Ubuntu 20.04 system. Notably it dynamically updates the /etc/resolv.conf file. Consequently the changes I made to appear temporary and subject to reversion.

The Common Issues I Encountered with DNS server

The issue I encountered was that changes I made to the /etc/resolv.conf file often seemed to be short-lived. If DNS changes revert to their previous settings shortly after being modified, it can disrupt services that rely on the updated DNS configuration.

Different methods to change DNS server permanently 

1. Editing resolved.conf

Editing /etc/systemd/resolved.conf is a way to configure DNS settings managed by systemd-resolved, which is a system service on Linux that provides network name resolution to local applications. By editing this configuration file, you can customize how DNS resolution works on your system. 

Using any text editor, edit the configuration file resolved.conf and add the required DNS server.

root@ervintest:~# nano /etc/systemd/resolved.conf
resolved.conf

2.Creating a Custom resolv.conf

To maintain control over your DNS configurations and prevent systemd-resolved from overwriting them, you can create your own custom resolv.conf file. This custom file will contain the DNS settings you want to use. Instead of directly editing or replacing the /etc/resolv.conf file (which systemd-resolved might overwrite), you create a symbolic link from /etc/resolv.conf to your custom resolv.conf file. This link effectively redirects any DNS-related queries from the system to your custom file. 

When applications or processes on your system request DNS resolution, they typically read the /etc/resolv.conf file. By creating a symbolic link, you’re ensuring that they read your custom resolv.conf file instead. This approach allows you to maintain your custom DNS configurations without worrying about them being overwritten by system updates or changes made by systemd-resolved.

root@ervintest:~# ln -sf /etc/resolv.custom.conf /etc/resolv.conf
root@ervintest:~# nano /etc/resolv.custom.conf

3. Locking the /etc/resolv.conf File

Using the chattr command, you can lock the /etc/resolv.conf file to prevent further modifications:

root@ervintest:~# chattr +i /etc/resolv.conf

4. Disabling systemd-resolved

When manual DNS management is preferred, systemd-resolved can be disabled using the following commands.

root@ervintest:~# systemctl stop systemd-resolved
root@ervintest:~# systemctl disable systemd-resolved

5. Using “resolvconf” utility

To manage DNS configurations and prevent them from being disordered by various programs, additionally you can use the “resolvconf” utility. Resolvconf is a tool commonly used on Debian and Ubuntu-based systems to manage the /etc/resolv.conf file and moreover ensure that DNS settings are consistent and controlled. 

root@ervintest:~# apt-get install resolvconf

You can configure DNS settings by editing the /etc/resolvconf/resolv.conf.d/base file or creating separate files in the /etc/resolvconf/resolv.conf.d/ directory. 

 root@ervintest:~# nano /etc/resolvconf/resolv.conf.d/base

In the file, specify the DNS server IP addresses you want to use, one per line. For example, to use Google’s public DNS servers:

nameserver 8.8.8.8
nameserver 8.8.4.4

After configuring the DNS settings, you need to update Resolvconf to apply the changes. Run the following command:

root@ervintest:~# resolvconf -u

By using any of these methods we can achieve stable and customized DNS configurations tailored for our specific needs. To upgrade Upgrade Ubuntu to the Latest Version, visit this.

Scroll to Top