> For the complete documentation index, see [llms.txt](https://chunhthanhde.gitbook.io/google-learning-programs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chunhthanhde.gitbook.io/google-learning-programs/google-it-support-professional-certificate/course-4-system-admin-it-infrastructure/module-2-network-and-inastructure-services/6.configuring-network-services/2.configuring-dhcp-with-dnsmasq.md).

# Configuring DHCP with Dnsmasq 🌐

Dnsmasq is a versatile tool that provides DNS, DHCP, TFTP, and PXE services. Here, we focus on using Dnsmasq as a DHCP server. Here's a summary of the key points:

### **Overview of DHCP Configuration**

1. **DHCP Server Basics**
   * A DHCP server assigns IP addresses to devices on a network.
   * It operates on a machine or device with a static IP address and is connected to the network it serves.
   * In practice, DHCP servers and clients usually run on separate machines, but for simulation, we use a single machine.
2. **Interface Configuration**
   * **DHCP Server Interface (`eth_srv`)**: This interface will serve DHCP requests. Example IP: `192.168.1.1` with a subnet mask `/24`.
   * **DHCP Client Interface (`eth_cli`)**: This interface will request an IP address from the DHCP server. Initially, it has no IP configured.
3. **Basic DHCP Configuration with Dnsmasq**
   * Create a configuration file (`dhcp.config`) with the following options:
     * `interface`: Specifies which interface Dnsmasq listens to for DHCP requests.
     * `bind-interfaces`: Ensures Dnsmasq only listens on the specified interface.
     * `domain`: Provides the network domain name to clients.
     * `dhcp-option`: Configures additional information for clients (e.g., default gateway, DNS servers).
     * `dhcp-range`: Defines the range of IP addresses available for lease, and the lease duration (e.g., 12 hours).
4. **Starting Dnsmasq**
   * Run Dnsmasq with the command:

     ```bash
     sudo dnsmasq -d -q -C dhcp.config
     ```
   * This command starts Dnsmasq with debugging information and the specified configuration file.
5. **Testing DHCP Functionality**
   * Use a DHCP client (`dhclient`) to request an IP address from `eth_cli`:

     ```bash
     sudo dhclient -i eth_cli -v
     ```
   * This command shows the exchange of packets and acquisition of an IP address (e.g., `192.168.1.80`).
6. **Verification and Results**
   * After acquiring an IP address, use `dig` to query the DNS server:

     ```bash
     dig @localhost instance-1
     ```
   * Dnsmasq not only assigns IP addresses but also provides DNS services, resolving local queries based on its configuration.
7. **Best Practices**
   * Perform simulations and tests on a separate network from production to avoid disruptions.

## Summary

* **Dnsmasq** is a lightweight solution that can serve both DNS and DHCP functions effectively.
* **Configuration** involves setting up interfaces, configuring DHCP options, and starting Dnsmasq with appropriate settings.
* **Testing** with `dhclient` and `dig` ensures that Dnsmasq is functioning correctly.

This setup demonstrates the dual role of Dnsmasq in managing both DNS and DHCP services, offering a compact yet powerful solution for network management. 🚀🔧
