Linux Tactic

Mastering DNS and DHCP: A Complete Guide to Configuring dnsmasq on CentOS 7

Setting Up Static IP Address: Configuring a Static IP Address on CentOS 7

As we all know, IP addresses are the unique identifiers used to connect devices on a network. An IP address can be either dynamic or static.

Dynamic IP is assigned by the network for a specific period and changes if you disconnect or restart your device. On the other hand, a static IP address is a permanent address assigned to a device that never changes.

Static IP addresses are useful for devices that need a stable, consistent connection, such as servers. In this article, we will guide you on how to configure a static IP address on CentOS 7, which is one of the most widely used server operating systems.

Step 1: Check Current IP Address

Before assigning a static IP, you need to find out the current IP configuration of your machine. In CentOS 7, you can use

ifconfig command to check the current network interface configuration. To do this, open the terminal and type the following command.

ifconfig

This command will list all the available network interfaces and their current configuration. Step 2: Edit Network Configuration

To set a static IP address on CentOS 7, you need to edit the network configuration file.

This file stores all the network settings, including IP addresses and DNS servers. The network configuration file is located at /etc/sysconfig/network-scripts/ directory.

To edit the configuration file, you can use the nano text editor or any other text editor of your choice. Open the terminal and type the following command to edit the network configuration file.

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0

Replace “eth0” with the interface name that you want to assign a static IP address to. If you have multiple network interfaces, you need to repeat this step for each interface.

Step 3: Configure IP address

Once you have opened the network configuration file, you need to add the following lines at the end of the file. BOOTPROTO=static

IPADDR=

NETMASK=

GATEWAY=

Replace , , and with your own values.

For example, if you want to assign the following static IP address:

IP address: 192.168.0.100

Netmask: 255.255.255.0

Gateway: 192.168.0.1

The configuration file should look like this:

TYPE=”Ethernet”

BOOTPROTO=static

IPADDR=192.168.0.100

NETMASK=255.255.255.0

GATEWAY=192.168.0.1

DEFROUTE=yes

IPV4_FAILURE_FATAL=no

NAME=”eth0″

DEVICE=”eth0″

Save the changes and exit the text editor. Step 4: Restart Network Services

After editing the network configuration file, you need to restart the network services for changes to take effect.

To restart the network services, run the following command.

sudo systemctl restart network

This command will reload the network configuration and apply the changes that you have made. Step 5: Verify IP Address

To verify that the static IP address is configured correctly, use the

ifconfig command to check the network interface configuration again.

ifconfig

You should see the network interface with the configured static IP address displayed. Congratulations! You have successfully configured a static IP address on your CentOS 7 machine.

Installing dnsmasq: Installing dnsmasq using YUM

Dnsmasq is a lightweight DNS and DHCP server designed to provide DNS and DHCP services to a small network. Dnsmasq is easy to install and configure and is ideal for home networks or small business networks.

In this article, we will guide you on how to install dnsmasq using the yum package manager on CentOS. Step 1: Update System

Before installing dnsmasq, you need to update the system with the latest version of the packages installed on your machine.

To do this, open the terminal and run the following command.

sudo yum update -y

This command will update the package list and install the latest version of the packages on your CentOS machine. Step 2: Install dnsmasq

After updating the system, you can install dnsmasq by running the following command.

sudo yum install dnsmasq -y

This command will install dnsmasq and its dependencies using the yum package manager. Step 3: Configure dnsmasq

Once you have installed dnsmasq, you need to configure it to provide DNS and DHCP services to your network.

The dnsmasq configuration file is located at /etc/dnsmasq.conf. Open the dnsmasq configuration file using your preferred text editor.

sudo nano /etc/dnsmasq.conf

Depending on your network setup, you may need to modify some of the default settings in the configuration file. For example, you can configure the DNS server IP address and domain name by adding the following lines to the configuration file.

# specify DNS server

server=8.8.8.8

# specify domain name

domain=mydomain.com

Save the changes and exit the text editor. Step 4: Start dnsmasq

After editing the configuration file, you can start the dnsmasq service by running the following command.

sudo systemctl start dnsmasq

This command will start the dnsmasq service and apply the changes that you have made in the configuration file. Step 5: Verify dnsmasq

To verify that dnsmasq is running, you can use the following command to check the status of the dnsmasq service.

sudo systemctl status dnsmasq

This command should display the status of the dnsmasq service and indicate that it is running. Congratulations! You have successfully installed and configured dnsmasq on your CentOS machine.

Conclusion

In this article, we have discussed two topics related to network configuration on CentOS. We have shown you how to configure a static IP address and install dnsmasq using the yum package manager.

By following these steps, you can customize your network settings and provide DNS and DHCP services to your network. These are essential skills for network administrators and will help you configure and manage your own network.

Configuring dnsmasq: Renaming the Default Configuration File and Creating a New One

Dnsmasq is a lightweight DNS and DHCP server designed to provide DNS and DHCP services to a small network. By default, dnsmasq uses the configuration file located at /etc/dnsmasq.conf.

However, it is possible to use a different configuration file by renaming the default configuration file and creating a new one. In this section, we will guide you on how to rename the default configuration file and create a new one.

Step 1: Backup Default Configuration File

Before renaming the default configuration file, it is important to create a backup of the original configuration file. To do this, open the terminal and run the following command.

sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

This command will create a backup of the default configuration file with the name dnsmasq.conf.bak. Step 2: Rename Default Configuration File

To rename the default configuration file, run the following command.

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

This command will rename the default configuration file to dnsmasq.conf.orig. Step 3: Create New Configuration File

Now that the default configuration file has been renamed, you can create a new configuration file.

To do this, open the terminal and run the following command. sudo nano /etc/dnsmasq-new.conf

This command will open the nano text editor with an empty file named dnsmasq-new.conf.

Step 4: Configure dnsmasq

To configure dnsmasq, you can add the required options and settings to the new configuration file. For example, you can specify the DNS server and domain name by adding the following lines to the configuration file.

# specify DNS server

server=8.8.8.8

# specify domain name

domain=mydomain.com

Save the changes and exit the text editor. Step 5: Point dnsmasq to the New Configuration File

After creating the new configuration file, you need to specify the location of the new configuration file to dnsmasq.

To do this, open the terminal and run the following command. sudo nano /etc/dnsmasq.conf

This command will open the configuration file with the nano text editor.

In this file, add the following line to specify the location of the new configuration file. conf-file=/etc/dnsmasq-new.conf

Save the changes and exit the text editor.

Step 6: Restart dnsmasq Service

After configuring dnsmasq, you need to restart the dnsmasq service for the changes to take effect. To do this, open the terminal and run the following command.

sudo systemctl restart dnsmasq

This will restart the dnsmasq service and apply the changes that you have made in the new configuration file. Configuring dnsmasq: Setting up Port and Domain Options

In dnsmasq, you can also customize the port and domain options to suit your specific requirements.

By default, dnsmasq uses UDP port 53 for DNS queries. However, you can change this port by editing the configuration file.

You can also set up an internal domain name that overrides public DNS records. In this section, we will guide you on how to set up port and domain options in dnsmasq.

Step 1: Edit Configuration File

To configure the port and domain options in dnsmasq, you need to edit the configuration file. To do this, open the terminal and run the following command.

sudo nano /etc/dnsmasq.conf

This will open the configuration file with the nano text editor. Step 2: Set up Port

By default, dnsmasq uses UDP port 53 for DNS queries.

However, you can change this port by adding the following line to the configuration file. port=5353

This will change the port used by dnsmasq to 5353.

Step 3: Set up Domain

You can also set up an internal domain name that overrides public DNS records. To do this, add the following line to the configuration file.

domain=mydomain.local

This will configure dnsmasq to use the internal domain name mydomain.local. Save the changes and exit the text editor.

Step 4: Restart dnsmasq Service

After configuring the port and domain options in dnsmasq, you need to restart the dnsmasq service for the changes to take effect. To do this, open the terminal and run the following command.

sudo systemctl restart dnsmasq

This will restart the dnsmasq service and apply the changes that you have made in the configuration file. Configuring dnsmasq: Restarting the dnsmasq Service

After editing the dnsmasq configuration file, you need to restart the dnsmasq service for the changes to take effect.

In this section, we will guide you on how to restart the dnsmasq service on CentOS. Step 1: Open Terminal

To restart the dnsmasq service, open the terminal on your CentOS machine.

Step 2: Restart dnsmasq Service

To restart the dnsmasq service, run the following command.

sudo systemctl restart dnsmasq

This command will stop and then start the dnsmasq service. Step 3: Verify dnsmasq Service

To verify that dnsmasq is running, you can use the following command to check the status of the dnsmasq service.

sudo systemctl status dnsmasq

This command should display the status of the dnsmasq service and indicate that it is running. Congratulations! You have successfully restarted the dnsmasq service on your CentOS machine.

Adding New DNS Entries to dnsmasq: Adding DNS Entries Using /etc/hosts File

In dnsmasq, you can add new DNS entries by using the /etc/hosts file. The /etc/hosts file is a local file that is used to map IP addresses to hostnames.

You can add new entries to this file to provide name resolution for local systems. In this section, we will guide you on how to add new DNS entries to dnsmasq using the /etc/hosts file.

Step 1: Edit /etc/hosts File

To add new DNS entries, open the /etc/hosts file using your preferred text editor. sudo nano /etc/hosts

This will open the file with the nano text editor.

Step 2: Add New DNS Entries

To add new DNS entries, enter the IP address followed by the hostname for each entry. For example, the following line maps the IP address 192.168.0.10 to the hostname myserver.

192.168.0.10 myserver

You can add multiple entries to the /etc/hosts file, each on a new line. Step 3: Save Changes

Save the changes to the /etc/hosts file and exit the text editor.

Step 4: Restart dnsmasq Service

After adding new DNS entries to the /etc/hosts file, you need to restart the dnsmasq service for the changes to take effect. To do this, open the terminal and run the following command.

sudo systemctl restart dnsmasq

This will restart the dnsmasq service and apply the changes that you have made in the /etc/hosts file. Step 5: Verify DNS Entries

To verify that the new DNS entries are working, use the nslookup or dig command to query the DNS records for the new entries.

nslookup myserver

dig myserver

Both of these commands should return the IP address that you have specified in the /etc/hosts file. Congratulations! You have successfully added new DNS entries to dnsmasq using the /etc/hosts file.

Testing DNS Server: Checking Whether DNS Server is Working Using dig Command

After setting up a DNS server using dnsmasq, it is essential to test whether the DNS server is functioning correctly. One way to do this is by using the dig command, which is a powerful tool for querying DNS records.

In this section, we will guide you on how to use the dig command to check whether your DNS server is working as expected. Step 1: Open Terminal

To perform DNS tests using the dig command, open the terminal on your machine.

Step 2: Query DNS Records

To test whether your DNS server is working correctly, you can query DNS records using the dig command. For example, suppose you have set up a DNS server with an IP address of 192.168.0.100 and a domain name of mydomain.com.

To check the A record for a specific hostname, run the following command:

dig A hostname.mydomain.com @192.168.0.100

Replace hostname.mydomain.com with the desired hostname and 192.168.0.100 with the IP address of your DNS server. This command will query the DNS server specified by the IP address and retrieve the A record for the hostname.

Step 3: Review the Output

After running the dig command, you will see the output that includes information about the queried DNS record. Check the ANSWER SECTION of the output to see if it contains the information you expected.

If the output shows the correct IP address or other relevant data, it indicates that your DNS server is working as expected. Congratulations! You have successfully tested your DNS server using the dig command.

Testing DNS Server: Testing DNS Caching

DNS caching is a feature of dnsmasq that allows it to cache DNS records for faster subsequent queries. To ensure that DNS caching is working correctly, you can perform some tests to verify its functionality.

In this section, we will guide you on how to test DNS caching in dnsmasq. Step 1: Open Terminal

To test DNS caching, open the terminal on your machine.

Step 2: Query DNS Records

To test DNS caching, you need to query a DNS record multiple times and observe the response times. For example, you can run the following command to query the A record for a specific hostname:

time dig A hostname.mydomain.com @192.168.0.100

Replace hostname.mydomain.com with the desired hostname and 192.168.0.100 with the IP address of your DNS server.

Step 3: Review the Response Time

After running the command, you will see the response time displayed in the output. The response time can vary depending on the initial cache state and the performance of your DNS server.

Repeat the same command multiple times and observe if the response time decreases after the first query. If the response time decreases significantly after the initial query, it indicates that DNS caching is working correctly.

Step 4: Flush the DNS Cache

To ensure accurate testing, you can flush the DNS cache before repeating the queries. To flush the DNS cache, run the following command:

sudo systemctl restart dnsmasq

This command will restart the dnsmasq service and clear the DNS cache. Step 5: Repeat the Tests

After flushing the DNS cache, repeat the previous steps and observe the response times.

If the response time decreases significantly after the first query, it further confirms that the DNS caching feature of dnsmasq is working as expected. Congratulations! You have successfully tested DNS caching in dnsmasq.

Other dnsmasq Options: Using user and group Options

In dnsmasq, you can customize the user and group that the dnsmasq process runs under. By default, dnsmasq runs as the user “nobody” and the group “nobody”.

However, you may want to change this for security or compatibility reasons. In this section, we will guide you on how to use the user and group options in dnsmasq.

Step 1: Edit Configuration File

To customize the user and group options in dnsmasq, you need to edit the configuration file. Open the terminal and run the following command to open the configuration file.

sudo nano /etc/dnsmasq.conf

Step 2: Set up user and group Options

In the configuration file, you can add the following lines to specify the user and group that dnsmasq should run under. user=myusername

group=mygroupname

Replace myusername with the desired username and mygroupname with the desired group name.

Make sure that the specified user and group exist on your system. Save the changes and exit the text editor.

Step 3: Restart dnsmasq Service

After specifying the user and group options in the configuration file, you need to restart the dnsmasq service for the changes to take effect. To do this, open the terminal and run the following command.

sudo systemctl restart dnsmasq

This will restart the dnsmasq service with the specified user and group. Congratulations! You have successfully configured the user and group options in dnsmasq.

Other dnsmasq Options: Setting cache-size Option to Increase the Number of DNS Records Cached

The cache-size option in dnsmasq allows you to specify the maximum number of DNS records that can be cached by dnsmasq. By default, dnsmasq has a small cache size of 150 entries.

However, you can increase this value to cache a larger number of DNS records. In this section, we will guide you on how to set the cache-size option in dnsmasq.

Step 1: Edit Configuration File

To configure the cache-size option, open the terminal and run the following command to open the configuration file. sudo nano /etc/dnsmasq.conf

Step 2: Set the cache-size Option

In the configuration file, add the following line to specify the cache-size:

cache-size=1000

Replace 1000 with the desired cache size.

This value represents the maximum number of DNS records that can be cached by dnsmasq. Save the changes and exit the text editor.

Step 3: Restart dnsmasq Service

After setting the cache-size option in the configuration file, you need to restart the dnsmasq service for the changes to take effect. To do this, open the terminal and run the following command.

sudo systemctl restart dnsmasq

This will restart the dnsmasq service with the specified cache size. Congratulations! You have successfully configured the cache-size option in dnsmasq.

Other dnsmasq Options: Using addn-hosts Option to Use Different Hosts File for Different Domain Names

The addn-hosts option in dnsmasq allows you to use a different hosts file for different domain names. With this option, you can specify additional hosts files to be read by dnsmasq, each containing domain-specific host entries.

In this section, we will guide you on how to use the addn-hosts option in dnsmasq. Step 1: Edit Configuration File

To use the addn-hosts option, open the terminal and run the following command to open the configuration file.

sudo nano /etc/dnsmasq.conf

Step 2: Set up addn-hosts Option

In the configuration file, you can add the following line to specify the path to the additional hosts file:

addn-hosts=/etc/dnsmasq-hosts.domain1

Replace /etc/dnsmasq-hosts.domain1 with the path to your additional hosts file for the specific domain name. You can have multiple addn-hosts entries to use different hosts files for different domain names.

Save the changes and exit the text editor. Step 3: Create Additional Hosts File

You need to create the additional hosts file specified in the addn-hosts option.

For example, if you specified /etc/dnsmasq-hosts.domain1, create a file at that path and add the necessary host entries for the domain. Step 4: Restart dnsmasq Service

After setting up the addn-hosts option in the configuration file and creating the additional hosts file, you need to restart the dnsmasq service for the changes to take effect.

To do this, open the terminal and run the following command.

sudo systemctl restart dnsmasq

This will restart the dnsmasq service with the specified additional hosts file. Congratulations! You have successfully used the addn-hosts option in dnsmasq.

Other dnsmasq Options: Using resolv-file Option to Get DNS Server Information from a Different File

The resolv-file option in dnsmasq allows you to specify a different file from which dnsmasq can get DNS server information. By default, dnsmasq reads the DNS server information from /etc/resolv.conf.

However, you may want to use a different file for various reasons. In this section, we will guide you on how to use the resolv-file option in dnsmasq.

Step 1: Edit Configuration File

To use the resolv-file option, open the terminal and run the following command to open the configuration file. sudo nano /etc/dnsmasq.conf

Step 2: Set up resolv-file Option

In the configuration file, you can add the following line to specify the path to the file containing DNS server information:

resolv-file=/etc/alternative-resolv.conf

Replace /etc/alternative-resolv.conf with the path to your preferred file containing DNS server information.

Save the changes and exit the text editor. Step 3: Create File Containing DNS Server Information

You need to create the file specified in the resolv-file option and add the necessary DNS server information.

For example, if you specified /etc/alternative-resolv.conf, create a file at that path and add the desired DNS server information. Step 4: Restart dnsmasq Service

After setting up the resolv-file option in the configuration file and creating the file containing DNS server information, you need to restart the dnsmasq service for the changes to take effect.

To do this, open the terminal and run the following command.

sudo systemctl restart dnsmasq

This will restart the dnsmasq service with the specified file containing DNS server information. Congratulations! You have successfully used the resolv-file option in dnsmasq.

In this comprehensive guide, we have explored various aspects of configuring and utilizing dnsmasq as a DNS and DHCP server on CentOS 7. We started by setting up a static IP address, ensuring a stable connection.

Then, we walked through the installation process of dnsmasq using YUM. Additionally, we covered renaming the default configuration file, customizing port and domain options, and restarting the dnsmasq service to reflect changes accurately.

Furthermore, we delved into adding new DNS entries through the /etc/hosts file and tested the DNS server’s functionality using the dig command. Additionally, we explored DNS caching and how to verify its effectiveness.

Lastly, we examined other dnsmasq options such as user and group configuration, cache-size for increased record caching, addn-hosts to differentiate hosts files, and resolv-file for alternative DNS server information. As a reader, you now have a strong foundation in configuring dnsmasq and have learned essential skills for managing your network effectively.

By utilizing these techniques, you can ensure reliable network connections and efficient DNS resolution.

Popular Posts