1. Overview

Sometimes, we need to use a different DNS server for a specific domain for reasons such as bypassing the DNS filter that blocks certain domain names.

In this tutorial, we’ll discuss whether it’s possible to use domain-specific DNS using the standard DNS resolution provided by Linux distributions. Additionally, we’ll discuss a few other alternative ways to make requests to domains using different DNS.

Finally, we’ll see how to change the DNS per application, which can be a better solution in some circumstances.

2. Domain-Specific DNS on Linux

The standard DNS resolution on Linux doesn’t support domain-specific DNS resolution. By standard DNS resolution, we mean the standard resolve.conf. However, some distributions might involve a different mechanism, which might support this feature.

For instance, some distributions use systemd-resolved as their preferred way to resolve domains. In the next sections, we’ll discuss other workarounds that can make domain-specific DNS resolutions.

2.1. BIND

BIND is a standard set of tools that provides feature-complete DNS services on Linux.

We can install the BIND under the package name bind using a package manager:

$ sudo apt install bind9 -y

Once installed, we can go ahead and create an entry for our domain configuration file in /etc/named.conf:

include "/etc/named/mydomain.com.conf";

Then, in our mydomain.com.conf file, we’ll specify forwarders to use:

zone "mydomain.com" {
    type forward;
    forwarders { 8.8.4.4; };
};

Now, we’ll need to restart the named service for the configuration to take effect:

$ sudo systemctl restart named

Our local DNS is now ready to use. At this point, we’ll have to update our network settings to use 127.0.0.1 as the DNS server.

2.2. dnsmasq

dnsmasq is a lightweight DNS manager for Linux. Like BIND, it’s not installed on most Linux distributions by default, so we’ll install it using a package manager:

$ sudo apt install dnsmasq -y

Now, let’s adjust the /etc/dnsmasq.conf file and have our domain use a specific DNS:

...
...
...
server=/mydomain.com/8.8.4.4
server=/baeldung.com/8.8.8.8
server=1.1.1.1

The final server entry is the default DNS server. Next, we’ll need to restart the dnsmasq service:

$ sudo systemctl restart dnsmasq

Now, the final step is to replace the nameserver in /etc/resolve.conf file with the dnsmasq nameserver:

nameserver 127.0.0.1
...

2.3. systemd-resolved

systemd-resolved is a systemd service that provides network name resolution to local applications.

Most major distributions now use systemd by default. So, chances are it’s already installed on our machine. We can check its status through the following command:

$ systemctl status systemd-resolved
* systemd-resolved.service - Network Name Resolution
     Loaded: loaded (/usr/lib/systemd/system/systemd-resolved.service; disabled; preset:>
     Active: inactive (dead)
...

We can add entries for domain-specific DNS in the systemd-resolved configuration file, which is located in /etc/systemd/resolved.conf:

...
[Resolve]
DNS=8.8.8.8 8.8.4.4 1.1.1.1
Domains=~baeldung.com

The tilde prefix before the domain signifies that the domain isn’t a search path but rather a direct domain query. Once our configuration is complete, we can enable and start the systemd-resolved service:

$ systemctl enable systemd-resolved --now
Created symlink /etc/systemd/system/dbus-org.freedesktop.resolve1.service -> /usr/lib/systemd/system/systemd-resolved.service.
Created symlink /etc/systemd/system/sysinit.target.wants/systemd-resolved.service -> /usr/lib/systemd/system/systemd-resolved.service.

3. Changing DNS per Application

Sometimes, we need to direct all the DNS queries of certain applications to a specific DNS. In this scenario, we’ll need to wrap our application in another application, which is capable of this feature.

One good tool for such a task is firejail, which we can download and install from its official website. Once installed, all we need to do is specify the DNS and the application to launch:

$ firejail --dns=8.8.4.4 telegram-desktop

4. Conclusion

In this article, we discussed the different ways to use specific DNS for certain domains. Besides that, we also learned how we can use a specific DNS for certain applications.