DNS

How to Configure dnsmasq on CentOS 7

How to Configure dnsmasq on CentOS 7
dnsmasq is a very lightweight and simple DNS server. dnsmasq can be configured to be a DNS server and a DHCP server. In this article, I am going to show you how to configure dnsmasq DNS server on CentOS 7. I am also going to talk about some of the common DNS server configuration options of dnsmasq. So, let's get started.

Setting Up Static IP Address:

First, you have to set up a static IP address on your CentOS 7 machine. You can do that via Network Manager Text User Interface nmtui.

Run nmtui with the following command:

$ sudo nmtui

Now, select Edit a connection and press .

Now, select Automatic from the IPv4 CONFIGURATION section and press .

Now, select Manual and press .

Now, navigate to and press .

Now, you have to add the IP address, Gateway, DNS server address and search domain.

The address should be the IP address of your CentOS 7 machine. This will be the IP address of your DNS server as well once you configure dnsmasq.

The gateway is the IP address of your router.

Use the IP address that you're setting for your CentOS 7 machine as the primary DNS server. Then, I prefer adding 8.8.8.8 (Google's public DNS server) as the secondary DNS server.

The search domain should be your own domain name.

Once you're done, make sure all of the marked options are checked. You can select the options and press to check them if they are not checked already.

Then, navigate to and press .

Finally, press 2 times and reboot your computer with the following command:

$ sudo reboot

Once your computer boots, the IP address should be set as you can see in the screenshot below.

$ ip a

Installing dnsmasq:

Dnsmasq is available in the official package repository of CentOS 7. So, you can easily install it with the YUM package manager.

First, update the YUM package repository cache with the following command:

$ sudo yum makecache

Now, run the following command to install dnsmasq:

$ sudo yum install dnsmasq

Now, press y and then press to continue.

Press y and then press to accept the GPG key of the CentOS 7 repository as well.

dnsmasq should be installed.

Configuring dnsmasq:

The default dnsmasq configuration file is /etc/dnsmasq.conf. This file contains all the supported options of dnsmasq and well documented. But, it's very long and hard to work with. So, I am going to rename this file to /etc/dnsmasq.conf.bk and create a new configuration file /etc/dnsmasq.conf. That way, whenever I need any help, I can just look at the file /etc/dnsmasq.con.bk and type in the required options in the /etc/dnsmasq.conf file.

To rename the dnsmasq configuration file, run the following command:

$ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bk

Now, create a new configuration file with the following command:

$ sudo vi /etc/dnsmasq.conf

Now, press i to go to the INSERT mode of vi and type in the following lines. Then press . Finally, type in :wq! and press to save the file.

Here, port is used to set the port (53 in this article) that dnsmasq will listen to.

domain is your own domain name. I've used example.com in this article.

Now, restart the dnsmasq service with the following command:

$ sudo systemctl restart dnsmasq

Now, check whether the dnsmasq server is running correctly with the following command:

$ sudo systemctl status dnsmasq

It is working correctly.

Adding New DNS Entries to dnsmasq:

Now, you can add new DNS entries to dnsmasq very easily using the /etc/hosts file.

To do that, open the /etc/hosts file with vi as follows:

$ sudo vi /etc/hosts

Now, type in the following lines at the end of the file and save the file.

192.168.21.20     ns.example.com
 
192.168.21.51     docker1.example.com
192.168.21.52     docker2.example.com
192.168.21.53     docker3.example.com
 
192.168.21.80     esxi1.example.com
192.168.21.81     esxi2.example.com
 
192.168.21.100    www.mywebsite.com

Here, you have a DNS entry in each line. Each line contains an IP address and a hostname separated by tabs or spaces. You type in the IP address first and then the hostname.

Now, restart the dnsmasq service for the changes to take effect.

$ sudo systemctl restart dnsmasq

Testing DNS Server:

Now, you can use dig to test whether the DNS server is working or not.

NOTE: The dig command is not available on CentOS 7 by default. The dig command is included in the bind-utils package. You can install it as follows:

$ sudo yum install bind-utils

Now, run the following command to check whether the DNS name (let's say docker1.example.com) can be resolved:

$ dig docker1.example.com

As you can see, the IP address of the DNS name is printed. So, it's working.

dnsmasq can also act as a Caching DNS server. To test whether DNS caching is working, run the following command:

$ dig google.com

As you can see, the IP addresses of the DNS name google.com is printed on the screen. So, DNS caching is working as well.

Other dnsmasq Options:

I've shown you a very basic dnsmasq configuration above. But there are some other common options that you can use to configure dnsmasq. In this section, we are going to take a look at them.

For example,

user=dnsmasq
group=dnsmasq

For example, to set the cache-size to 1000, use

cache-size=1000

For example, you can create a new file /etc/hosts.example.com and add all the example.com DNS entries there. Then add the following line to your /etc/dnsmasq.conf file:

addn-hosts=/etc/hosts.example.com

For example, to use the file /etc/resolv2.conf as the dnsmasq resolv-file,

create the file as follows:

$ sudo vi /etc/resolv2.conf

Now, add your DNS servers there in the following format. Remember to put the high priority ones above others.

nameserver 192.168.21.2
nameserver 8.8.8.8

Then, add the following line to the /etc/dnsmasq.conf file.

resolv-file=/etc/resolv2.conf

There are many more options of dnsmasq. I recommend you check the default configuration of dnsmasq that you renamed to /etc/dnsmasq.conf.bk earlier for more information.

So, that's how you configure dnsmasq DNS server on CentOS 7. Thanks for reading this article.

Mouse left-click button not working on Windows 10
If you are using a dedicated mouse with your laptop, or desktop computer but the mouse left-click button is not working on Windows 10/8/7 for some rea...
Cursor jumps or moves randomly while typing in Windows 10
If you find that your mouse cursor jumps or moves on its own, automatically, randomly while typing in Windows laptop or computer, then some of these s...
How to reverse Mouse and Touchpads scrolling direction in Windows 10
Mouse and Touchpads not only make computing easy but more efficient and less time-consuming. We cannot imagine a life without these devices, but still...