This tutorial will be showing you how to set up a local DNS resolver on CentOS 8/RHEL 8, with the widely-used BIND9 DNS software. There are many synonyms for DNS resolver, some of which are listed below. They all refer to the same thing.
- full resolver (in contrast to stub resolver)
- DNS recursor
- recursive DNS server
- recursive resolver
Also, be aware that A DNS server can also called a name server. Examples of DNS resolver are 8.8.8.8 (Google public DNS server) and 1.1.1.1 (Cloudflare public DNS server). The OS on your PC also has a resolver, although it’s called stub resolver due to its limited capability. A stub resolver is a small DNS client on the end user’s computer that receives DNS requests from applications such as Firefox and forwards requests to a recursive resolver. Almost every resolver can cache DNS response to improve performance, so they are also called caching DNS server.
Why Run Your Own DNS Resolver
Normally, your computer or router uses your ISP’s DNS resolver to query domain names in order to get an IP address. Running your own local DNS resolver can speed up DNS lookups, because
- The local DNS resolver only listens to your DNS requests and does not answer other people’s DNS requests, so you have a much higher chance of getting DNS responese directly from the cache on the resolver.
- The network latency between your computer and DNS resolver is eliminated (almost zero), so DNS queries can be sent to root DNS servers more quickly.
If you run a mail server and use DNS blacklists (DNSBL) to block spam, then you are advised to run a local DNS resolver to speed up DNS lookups. If you run your own VPN server on a VPS (Virtual Private Server), it’s also a good practice to install a DNS resolver on the same VPS.
You may also want to run your own DNS resolver if you don’t like your Internet browsing history being stored on a third-party server.
If you own a website and want your own DNS server to handle name resolution for your domain name instead of using your domain registrar’s DNS server, then you will need to set up an authoritative DNS server, which is different than a DNS resolver. BIND can act as an authoritative DNS server and a DNS resolver at the same time, but it’s a good practice to separate the two roles on different boxes. This tutorial shows how to set up a local DNS resolver and because it will be used on local host/local network, no encryption (DNS over TLS or DNS over HTTPS) is needed. Setting up a DoT or DoH server will be discussed in a future article.
Please note that you need to have root privilege when installing software on CentOS/RHEL. You can add sudo at the beginning of a command, or use su -
command to switch to root user.
Install BIND9 on CentOS 8/RHEL 8
BIND (Berkeley Internet Name Domain) is an open-source DNS server software widely used on Unix/Linux due to it’s stability and high quality. It’s originally developed by UC Berkeley, and later in 1994 its development was moved to Internet Systems Consortium, Inc (ISC).
Run the following command to install BIND 9 on CentOS 8/RHEL 8 from the default repository. BIND 9 is the current version and BIND 10 is a dead project.
sudo dnf update sudo dnf install bind
Check version information.
named -v
Sample output:
BIND 9.11.4-P2-RedHat-9.11.4-17.P2.el8_0.1 (Extended Support Version)
To check the version number and build options, run
named -V
Now we can start BIND 9 with:
sudo systemctl start named
And enable auto start at boot time:
sudo systemctl enable named
You can check its status with:
systemctl status named
Hint: If the above command doesn’t quit immediately, press Q.
The BIND server will run as the named
user, which is created during installation, and listens on TCP and UDP port 53, as can be seen by running the following command:
sudo dnf install net-tools sudo netstat -lnptu | grep named
Usually, DNS queries are sent to the UDP port 53. The TCP port 53 is for responses size larger than 512 bytes.
The BIND daemon is called named. (A daemon is a piece of software that runs in the background.) The named
binary is installed by the bind
package and there’s another important binary: rndc
, the remote name daemon controller. The rndc
binary is used to reload/stop and control other aspects of the BIND daemon. Communication is done over TCP port 953.
For example, we can check the status of the BIND name server.
sudo rndc status
Configurations for a Local DNS Resolver
The named daemon on CentOS 8/RHEL 8 uses the root hints file at /var/named/named.ca
. The root hints file is used by DNS resolvers to query root DNS servers. There are 13 groups of root DNS servers, from a.root-servers.net
to m.root-servers.net
.
Out of the box, the BIND9 server on CentOS/RHEL provides recursive service for localhost only. Outside queries will be denied. Edit the BIND main configuration file /etc/named.conf
.
sudo nano /etc/named.conf
In the options
clause, you can find the following two lines.
listen-on port 53 { 127.0.0.1; }; listen-on-v6 port 53 { ::1; };
This makes named
listen on localhost only. If you want to allow clients in the same network to query domain names, then comment out these two lines. (add double slashes at the beginning of each line)
// listen-on port 53 { 127.0.0.1; }; // listen-on-v6 port 53 { ::1; };
Find the following line.
allow-query { localhost; };
Add the network range from which clients can query domain names like below.
allow-query { localhost; 192.168.0.0/24; 10.10.10.0/24; };
The following line enables recursion service, which is fine.
recursion yes;
I also recommend adding the following directives in the options
clause.
// hide version number from clients for security reasons. version "not currently available"; // enable the query log querylog yes;
Save and close the file. Then test the config file syntax.
sudo named-checkconf
If the test is successful (indicated by a silent output), then restart named
.
sudo systemctl restart named
If you have a firewall running on the BIND server, then you need to open port 53 to allow LAN clients to send DNS queries.
sudo firewall-cmd --zone=public --permanent --add-rich-rule='rule family="ipv4" source address="192.168.0.0/24" port protocol="udp" port="53" accept'
Reload firewall for the change to take effect.
sudo systemctl reload firewalld
This will open UDP port 53 to the private network 192.168.0.0/24. Then from another computer in the same LAN, we can run the following command to query the A record of google.com. Replace 192.168.0.101 with the IP address of your BIND resolver.
dig A google.com @192.168.0.101
Note: On CentOS/RHEL, you need to install the bind-utils
package in order to use the dig
command.
sudo dnf install bind-utils
Now on the BIND resolver, check the query log with the following command.
sudo journalctl -eu named
This will show the latest log message of the named service unit. I can find the following line in the log, which indicates that a DNS query for google.com’s A record has been received from port 57806 of IP address 192.168.0.103.
named[1162]: client @0x7f4d2406f0f0 192.168.0.103#57806 (google.com): query: google.com IN A E(0)K (192.168.0.101)
Setting the Default DNS Resolver on CentOS 8/RHEL 8 Server
On the BIND server, we need to set 127.0.0.1 as the default DNS resolver. You can check the current DNS resolver on CentOS 8/RHEL 8 with the following command.
cat /etc/resolv.conf
Sample output:
# Generated by NetworkManager nameserver 192.168.0.1 nameserver 8.8.8.8
If you run the following command on the BIND server,
dig A facebook.com
This DNS query can’t be found in BIND log. Instead, you need to explicitly tell dig to use BIND.
dig A facebook.com @127.0.0.1
To set BIND as the default resolver, first you need to find the name of your main network interface with the following command.
ip addr
Mine is ens3
. Next, run the following command to edit the network interface configuration file. Replace ens3
with your own interface name. If the BIND resolver is running on a laptop connected using Wi-Fi, then you need to edit the ifcfg-wireless-network-name
file.
sudo nano /etc/sysconfig/network-scripts/ifcfg-ens3
Find the DNS1
parameter and change its value to 127.0.0.1
. Note that if values for other parameters in this file are wrapped in double-quotes, then you also need to wrap 127.0.0.1
with double-quotes.
DNS1="127.0.0.1"
If you can’t find the DNS1
parameter, then add the above line at the bottom. Save and close the file. Then restart NetworkManager for the change to take effect.
sudo systemctl restart NetworkManager
You can now check the content of /etc/resolv.conf
again. As you can see, 127.0.0.1 (BIND) is now the default DNS resolver on CentOS 8/RHEL 8.
Setting Default DNS Resolver on Client Computers
Now you can configure other computes on the LAN to use the BIND server as DNS resolver. For Windows and MacOS computers, you can search on Google to find out how to set default DNS resolvers. Here I will show you how to set DNS resolver on Linux desktop computers. The following method works on any Linux distro that uses NetworkManager.
Click on the Network Manager icon on your Linux desktop to find the Edit connections
. (On Some Linux distros, you need to right-click the Network Manager.)
Then select the current connection and click the cog icon to edit this connection.
Select IPv4 settings tab, change method from Automatic(DHCP)
to Automatic(DHCP) addresses only
, which will prevent your system from getting DNS server address from your router. Then specify a DNS server. Here I enter the BIND server’s IP address in my LAN.
Save your changes. Then restart NetworkManager for the changes to take effect.
sudo systemctl restart NetworkManager
Once you are reconnected, click the Network Manager icon again and select connection information
. You can see that your Linux desktop computer is now using your BIND DNS server.
Conclusion
I hope this tutorial helped you set up a local DNS resolver on CentOS 8/RHEL 8 with BIND9. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂
Rate this tutorial
[Total: 0 Average: 0]