Building Your Local Network with DNSmasq and DHCP on AlmaLinux 9
Categories:
Introduction
Managing local networks can be challenging without proper tools. Enter Dnsmasq, a lightweight and versatile solution for providing DNS and DHCP services on a single server. By leveraging Dnsmasq on AlmaLinux 9, you can efficiently configure and manage your network for both name resolution and dynamic IP allocation.
In this guide, we’ll set up Dnsmasq to act as a DNS server and DHCP server to simplify local network management.
Part 1: Installing and Configuring Dnsmasq
Dnsmasq is a streamlined tool that combines DNS caching and DHCP management, making it ideal for small-to-medium networks.
Step 1: Install Dnsmasq
Install the package:
sudo dnf install -y dnsmasq
Enable and start the service:
sudo systemctl enable dnsmasq --now
Verify the installation:
systemctl status dnsmasq
Step 2: Configure Dnsmasq
Dnsmasq’s configuration file is located at /etc/dnsmasq.conf
. Here’s how to customize it for your network:
Edit the configuration file:
sudo nano /etc/dnsmasq.conf
Add or update the following settings:
Enable DNS caching:
cache-size=1000
Specify a local DNS domain:
domain=localdomain
Set the DHCP range and lease time:
dhcp-range=192.168.1.50,192.168.1.100,12h
Assign static IPs using MAC addresses (optional):
dhcp-host=00:11:22:33:44:55,192.168.1.10
Save and close the file, then restart Dnsmasq:
sudo systemctl restart dnsmasq
Step 3: Test Dnsmasq Configuration
Verify DNS functionality:
Query a domain:
dig example.com
Check cached responses:
dig example.com
Verify DHCP functionality:
On a client, release and renew the IP address:
sudo dhclient -r && sudo dhclient
Check assigned IPs:
View leases on the server:
cat /var/lib/misc/dnsmasq.leases
Part 2: Advanced Configuration for DNS and DHCP
Dnsmasq supports additional features to enhance your local network.
Step 1: Configure Custom Hostnames
Edit the
/etc/hosts
file:sudo nano /etc/hosts
Add custom hostname mappings:
192.168.1.10 server1.localdomain server1 192.168.1.11 server2.localdomain server2
Restart Dnsmasq:
sudo systemctl restart dnsmasq
Step 2: Integrate with External DNS Servers
Edit
/etc/dnsmasq.conf
to specify upstream DNS servers:server=8.8.8.8 server=8.8.4.4
Clear the DNS cache:
sudo systemctl restart dnsmasq
Step 3: Troubleshooting Dnsmasq
Check logs for issues:
sudo journalctl -u dnsmasq
Test configuration syntax:
sudo dnsmasq --test
Ensure no port conflicts:
Stop conflicting services:
sudo systemctl stop systemd-resolved
Part 3: Secure and Optimize Dnsmasq
Step 1: Restrict DNS Queries
Limit queries to internal clients:
Add the following to
/etc/dnsmasq.conf
:interface=eth0 bind-interfaces
Restart Dnsmasq:
sudo systemctl restart dnsmasq
Step 2: Enable Logging
Enable detailed logging for troubleshooting:
Add to
/etc/dnsmasq.conf
:log-queries log-facility=/var/log/dnsmasq.log
View logs:
tail -f /var/log/dnsmasq.log
Step 3: Optimize for Performance
Increase cache size:
Update
/etc/dnsmasq.conf
:cache-size=2000
Enable asynchronous DNS processing:
Add:
dns-forward-max=150
Conclusion
With Dnsmasq configured, you now have a lightweight and efficient solution for managing DNS and DHCP services on your AlmaLinux 9 server. This setup is perfect for small to medium networks, offering a robust way to handle name resolution and IP allocation.
What’s Next?
In the next post, we’ll delve deeper into configuring a full-fledged DNS server using BIND to manage internal and external domains with greater control.