DNS query caching is essential for speeding up internet browsing by storing responses to previously made queries. In this article, we'll focus on setting up such caching on CentOS 7 using the dnsmasq tool. Dnsmasq is a lightweight, easily configurable DNS forwarder and DHCP server.
Prerequisites
Before starting the installation and configuration of dnsmasq, make sure you have:
- Access to a CentOS 7 server with superuser (root) privileges.
- Internet connectivity to download necessary packages.
Installing dnsmasq
-
Updating the System and Installing dnsmasq The first step is to update the system and install dnsmasq. Open a terminal and run the following commands:
sudo yum update -y sudo yum install dnsmasq -y
- Enabling and Starting the dnsmasq Service After installing dnsmasq, you need to enable and start the service. This ensures that dnsmasq will be automatically launched at system startup:
sudo systemctl enable dnsmasq sudo systemctl start dnsmasq
Configuring dnsmasq
The configuration file for dnsmasq is located at /etc/dnsmasq.conf
. Before making any changes, it's recommended to make a backup of this file:
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.backup
- Editing the Configuration File Open
/etc/dnsmasq.conf
in any text editor. For example:sudo nano /etc/dnsmasq.conf
- Basic Configuration For basic DNS caching, find and modify the following lines in the configuration file (or add them if they're missing):
# Enable DNS caching cache-size=1000 # Log queries log-queries # Specify upstream DNS servers server=8.8.8.8 server=8.8.4.4
Here,
cache-size
defines the memory size to store DNS queries (in this case, 1000 queries). Theserver
lines specify upstream DNS servers that dnsmasq will use for resolving queries not in the cache. -
Restarting dnsmasq After making changes in the configuration file, it's necessary to restart the dnsmasq service for the new configuration to take effect:
Testing the Configuration
After configuring dnsmasq, you can test its functionality using tools like dig
or nslookup
. Upon the first query for a specific domain, the response time might be longer as the query is sent to the upstream DNS server. With subsequent queries, the response time should be shorter, indicating that the response was retrieved from the local cache.
Example test with dig
:
dig example.com
Watch the QUERY TIME
section in the output, which shows how long it took to process the query.
Setting up DNS query caching on CentOS 7 with dnsmasq is a relatively straightforward process that can significantly improve internet browsing speed and reduce latency. Thanks to dnsmasq's lightweight nature and flexibility, it's an ideal choice for small to medium-sized networks.