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 <Enter>.

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

Now, select Manual and press <Enter>.

Now, navigate to <Show> and press <Enter>.

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 (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 <Space Bar> to check them if they are not checked already.

Then, navigate to <OK> and press <Enter>.

Finally, press <Esc> 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 <Enter> to continue.

Press y and then press <Enter> 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 <Esc>. Finally, type in :wq! and press <Enter> 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 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.

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 can be resolved:

$ dig

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

As you can see, the IP addresses of the DNS name 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.

  • user and group – You can use these options to set a run user and group for dnsmasq.

For example,

  • cache-size – If you want to increase the number of DNS records that will be cached by the dnsmasq server, then use this option.

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

  • addn-hosts – Earlier, I used the default /etc/hosts file to add new DNS entries. But, if you want to use different hosts file for different domain names, then this option is useful for you.

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

  • resolv-file – By default dnsmasq uses the /etc/resolv.conf file to learn about what DNS servers to use for name caching. But if you want dnsmasq to get this information from some other file, just create the file, add DNS servers to that file and finally add resolv-file option to your dnsmasq configuration file /etc/dnsmasq.conf.

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.


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


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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

DNSMasq Ubuntu Tutorial

DNSmasq is a lightweight tool to provide both DNS service and DHCP service for small-scale networks. DNSmasq’s local DNS...

Google DNS Tutorial

On 3 December 2009, Google announced its free alternative Domain Name System (DNS) service, Google Public DNS, which can...

Premium DNS services

Premium DNS Extremely quick websites always give a good impression to the visitors. The difference between the lookup speed...