Setup local DNS caching with DNSmasq on CentOS 8


In today’s interconnected world, reliable and efficient network infrastructure is crucial for smooth online experiences. Whether you’re a seasoned system administrator or an enthusiast looking to optimize your network, setting up a local DNS caching server can significantly enhance your network’s performance and reduce latency.

DNS, short for Domain Name System, plays an important role in translating domain names into IP addresses. When you access a website or any online service, your device needs to query a DNS server to get the corresponding IP address. By default, these DNS queries are sent to remote DNS servers, which can cause delays and increase network traffic.

To overcome these challenges, we will implement a local DNS caching server using DNSmasq on CentOS 8. DNSmasq is a lightweight and versatile DNS forwarding and DHCP server that can be easily configured to provide local DNS caching capabilities. This setup enables your CentOS 8 machine to cache DNS responses locally, reducing the reliance on external DNS servers and accelerating the overall network performance.

Throughout this blog post, we will guide you step-by-step through the process of installing and configuring DNSmasq on CentOS 8. We’ll cover the necessary prerequisites, and explain the key concepts behind DNS caching, By the end, you’ll have a fully operational local DNS caching server that optimizes DNS resolution and improves your network’s responsiveness.

Whether you’re running a home network, a small business infrastructure, or a larger enterprise setup, implementing local DNS caching with DNSmasq on CentOS 8 can have significant advantages. It not only reduces the load on external DNS servers but also enhances the reliability and security of DNS resolution within your network.


Before proceeding with the installation and configuration of DNSmasq, make sure you have the following:

  • A CentOS 8 machine with root or sudo privileges.
  • A stable internet connection.
  • Basic knowledge of the Linux command line.

Install DNSmasq

The first step is to install DNSmasq on your CentOS 8 machine. Open a terminal or SSH into your CentOS server and run the following command:

dnf install dnsmasq

You can also use yum to install DNSmasq:

yum install dnsmasq

Configuring DNSmasq

Once DNSmasq is installed, it’s time to configure its settings. The main configuration file for DNSmasq is located at /etc/dnsmasq.conf. Open the file using a text editor:

vim /etc/dnsmasq.conf

In the configuration file, you’ll find different options to customize DNSmasq. Some important settings to consider are:


Specify the IP address on which DNSmasq should listen for DNS queries. set it to if you are using DNSmasq as a local DNS caching service.


Set the path to the file containing upstream DNS servers. you can create any file where for example we are creating a file named “resolv.dnsmasq” in “/etc” with the following content:

vim /etc/resolv.dnsmasq

These configurations will enable DNSmasq to query the DNS records from the and and cache locally.


Define the maximum number of DNS records to cache. The cache-size value represents the maximum number of DNS records that can be stored in the cache. It is defined in terms of the number of DNS resource records (RRs) rather than the amount of memory consumed. Each cached DNS record takes up a certain amount of memory, and as the cache size increases, so does the memory usage of the DNSmasq process.

The appropriate value for cache-size depends on factors such as the available memory on your CentOS 8 machine and the expected DNS query load. It’s important to strike a balance between maximizing cache utilization and avoiding excessive memory consumption.



Uncomment this line to prevent DNSmasq from using the “/etc/resolv.conf” file.


Uncomment this line to enable asynchronous DNS resolution.

When DNSmasq receives a DNS query, it typically sends the query to the configured upstream DNS servers and waits for a response. During this waiting period, DNSmasq uses a polling mechanism to periodically check for the arrival of the DNS response. This polling approach introduces some delay and can impact the responsiveness of DNS resolution, especially in high-traffic scenarios.

By enabling no-poll, DNSmasq switches to an asynchronous mode of operation. Instead of continuously polling for the response, it allows the DNS resolution process to be event-driven. When a DNS query is sent, DNSmasq immediately moves on to process other tasks, and when the DNS response arrives, it is handled asynchronously. This approach improves the responsiveness of DNS resolution by reducing the delay caused by polling.

Start and Enable DNSmasq

Now that you have configured DNSmasq, you need to start and enable the service before making the final changes to the Linux network settings. execute the following command to start the DNSmasq service and make it run at the startup:

systemctl start dnsmasq
systemctl enable dnsmasq

Set default DNS to DNSmasq

If you have uncommented the “no-resolv” option in the DNSmasq config you don’t need to edit the “/etc/resolv.conf”.

As the last step, you need to make one change in the “/etc/resolv.conf” file. you need to comment on all lines that refer to “nameserver” and write a new one with as the value, see the following example:


Also, it’s recommended to apply this change in your network configuration in the “network-scripts” file:

vim /etc/sysconfig/network-scripts/YOUR_NETWORK_INTERFACE_NAME

Set the DNS1 value to and set the DNS2 to another DNS server as the backup.

After that you need to restart your network interface for changes to take effect:

nmcli device reapply YOUR_NETWORK_INTERFACE_NAME

If you want to find your Network Interface name you can use “ifconfig” command.
To install “ifconfig”:

yum install net-tools


The installation and configuration process detailed in this article provides step-by-step instructions, enabling system administrators and network enthusiasts to seamlessly deploy a fully operational local DNS caching server on their CentOS 8 machine. By following these guidelines, users can effectively optimize DNS resolution, streamline network performance, and elevate the overall online experience.

Leave a Reply

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