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:
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 220.127.116.11 (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:
Once your computer boots, the IP address should be set as you can see in the screenshot below.
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:
Now, run the following command to 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.
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:
Now, create a new configuration file with the following command:
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 example.com in this article.
Now, restart the dnsmasq service with the following command:
Now, check whether the dnsmasq server is running correctly with the following command:
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:
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.
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:
Now, run the following command to check whether the DNS name (let’s say docker1.example.com) can be resolved:
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:
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.
- user and group – You can use these options to set a run user and group for dnsmasq.
- 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/hosts.example.com and add all the example.com 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:
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.