Dnsmasq Network Configuration and Management

Author

Reads 3.4K

A sleek WiFi 6 router with antennas and cable on a wooden desk, perfect for modern home networks.
Credit: pexels.com, A sleek WiFi 6 router with antennas and cable on a wooden desk, perfect for modern home networks.

Dnsmasq is a popular open-source DNS forwarder that can be used to manage network configuration and provide DNS services to devices on your network. It's a lightweight and efficient solution that can be configured to meet the needs of your network.

One of the key benefits of using Dnsmasq is its ability to provide DNS caching, which can improve the performance and speed of your network by reducing the number of DNS requests made to external DNS servers. This is especially useful for networks with multiple devices.

To configure Dnsmasq, you'll need to edit the configuration file, which is usually located at /etc/dnsmasq.conf. This file contains various settings that control the behavior of Dnsmasq, such as the DNS port and the IP address of the DNS server.

Dnsmasq can also be used to manage network leases, which are used to assign IP addresses to devices on your network. This is done using the DHCP protocol, which is a built-in feature of Dnsmasq.

See what others are reading: Cisco Network Registrar

Installation

Credit: youtube.com, Implementing DNS via dnsmasq

To install dnsmasq, you'll need to install the dnsmasq package. Then, start and enable the dnsmasq service.

You'll also need to restart your network so the DHCP client can create a new /etc/resolv.conf file. This will allow you to access devices on your network by their hostname.

One example of this in action is being able to ping a host or device by its hostname.home.arpa address, as long as it's defined in your /etc/hosts file.

Server

Dnsmasq can act as a DNS server, which is perfect for small setups. To set it up, specify a listen-address directive with the localhost IP address.

To use it on a LAN IP address, assign a static LAN IP. This is recommended for other computers on the network to listen on. You can also assign a network interface for this purpose.

Dnsmasq can cache domain names, and you can set the number of cached domain names with the cache-size option. The default is 150. To validate DNSSEC, load the DNSSEC trust anchors provided by the dnsmasq package and set the option dnssec.

Credit: youtube.com, Dnsmasq Review: The Best DNS + DHCP Server for Home Labs? (2025)

For a DHCP server, dnsmasq has the functionality turned off by default, so you must turn it on. This involves setting the important settings and configuring a computer to use DHCP for automatic IP address assignment.

Dnsmasq can be combined with Unbound to act as a "connector", where DHCP leases are queried directly by Unbound. This combination is very resilient and easy to manage.

A unique perspective: Unbound (DNS Server)

PXE Server

A PXE server is a critical component for network booting, and it's surprisingly easy to set up. To get started, you'll need to configure dnsmasq to provide both DHCP and TFTP services.

Dnsmasq can handle both DHCP and TFTP, making it a convenient choice for setting up a PXE server. In the dnsmasq configuration file, you'll need to set up the TFTP server and DHCP server in full DHCP or proxy mode.

The TFTP root directory is where you'll copy and configure a PXE compatible boot loader, such as PXELINUX. File paths in this directory are relative to the TFTP root path.

Credit: youtube.com, Understanding PXE: A Guide for IT Professionals: Preboot Execution Environment

To enable PXE in dnsmasq, you'll need to add some specific options to the configuration file. Be sure to exclude any .0 suffix from file paths when using pxe-service options.

If you're having trouble identifying the architecture of your clients, especially those with UEFI, you can use a combination of dhcp-match and dhcp-boot. This is described in RFC 4578 2.1, which lists additional client-arch numbers for use with the dhcp boot protocol.

Server

Dnsmasq can be used as a DNS server, and to set it up, you specify a listen-address directive, adding in the localhost IP address. This allows the computer to listen on its LAN IP address for other computers on the network.

To use Dnsmasq as a DNS caching daemon, you need to specify a static LAN IP address. This is recommended to avoid any potential issues.

The number of cached domain names can be set with the cache-size option, with a default size of 150. This can be adjusted to suit your needs.

Credit: youtube.com, How to Build a Budget Home Server and WHY You Should!

To validate DNSSEC, you need to load the DNSSEC trust anchors provided by the dnsmasq package and set the option dnssec. This is an important step to ensure the security of your DNS server.

Dnsmasq can also be used as a DHCP server, but by default, the DHCP functionality is turned off. To use it, you need to turn it on by adding the necessary settings to the configuration file.

To use DHCP for automatic IP address assignment, you need to configure the client computer to use DHCP. This can be done by setting the client to obtain an IP address automatically.

The DHCP leases are stored in the /var/lib/misc/dnsmasq.leases file on the server. This file can be inspected to see the current leases.

Dnsmasq can be combined with Unbound to act as a "connector", where DHCP leases are queried directly by Unbound. This is a useful feature for large networks.

Dnsmasq is a non-recursive forwarding DNS server, and it always needs a recursive DNS resolver to forward its queries to. This can be Unbound or another DNS service on the internet.

For small to medium-sized setups, Dnsmasq is a great choice for a DHCP server, with a straightforward configuration and support for DNS registration. However, for larger enterprise setups, KEA DHCP may be a better option.

File Handling

Credit: youtube.com, Dnsmasq

When editing dnsmasq's configuration file, you should set the upstream DNS server addresses with the server=server_address syntax. This is necessary to specify the external DNS server that dnsmasq will forward queries to.

To protect /etc/resolv.conf from modification, you can make sure to add a line to prevent it from being overwritten, as described in the article. This is crucial to prevent any potential issues with dnsmasq's configuration.

To generate dnsmasq configuration files, you can use openresolv, which can be configured to write out dnsmasq configuration. This can be done by editing /etc/resolvconf.conf and adding the loopback addresses as name servers.

Tftp Server

To set up a TFTP server, you'll need to create a root directory for TFTP, like /srv/tftp, to store transferable files.

This directory should be owned by the dnsmasq user, which is a requirement for using dnsmasq's TFTP secure mode.

In secure mode, only files owned by the dnsmasq user will be served over TFTP, which adds an extra layer of security to your setup.

You'll need to change the ownership of the TFTP root directory and all its files to the dnsmasq user to use this feature.

By following these steps, you'll be able to set up a secure TFTP server using dnsmasq.

File Handling

Close Up Photo of Network Switch
Credit: pexels.com, Close Up Photo of Network Switch

File handling is an essential part of configuring dnsmasq. You need to add the localhost addresses as the only nameservers in /etc/resolv.conf.

This causes all queries to be sent to dnsmasq. You can use openresolv to generate configuration files for dnsmasq, but first, you need to edit /etc/resolvconf.conf and add the loopback addresses as name servers.

Make sure to configure openresolv to write out dnsmasq configuration. If the configuration files do not exist, dnsmasq.service will fail to start.

You can use the following command to generate the configuration files: resolvconf -u. This will create the necessary files for dnsmasq to function properly.

To avoid modifying /etc/resolv.conf directly, you can use openresolv to generate the configuration files. This is especially useful if your network manager supports resolvconf.

Here are the steps to follow:

  • Edit /etc/resolvconf.conf and add the loopback addresses as name servers.
  • Configure openresolv to write out dnsmasq configuration.
  • Run resolvconf -u to generate the configuration files.
  • Edit dnsmasq's configuration file to use openresolv's generated configuration.

Remember to protect /etc/resolv.conf from modification by following the instructions in Domain name resolution#Overwriting of /etc/resolv.conf.

Configuration

You can configure Unbound to forward zones to Dnsmasq. To do this, you'll need to configure Unbound to forward the zones to Dnsmasq.

Credit: youtube.com, Configuring DNS With Dnsmasq and Ubuntu Server

You can use the .internal TLD for internal use, but if you own a custom TLD, you can create a zone like lan.internal.example.com.

Press Apply to activate the new configuration. This will update the settings to forward the zones to Dnsmasq.

To configure options that aren't available in the GUI, you can add custom configuration files on the firewall itself. These files should be placed in the /usr/local/etc/dnsmasq.conf.d/ directory.

Files in this directory should have a .conf extension, like custom-options.conf. The files will be included in alphabetical order, so name them accordingly.

Domain Management

Domain management with Dnsmasq is quite straightforward. You can assign a custom domain by adding it to the hosts entries, but first, you need to uncomment the expand-hosts setting.

This setting is crucial because without it, you'll have to manually add the domain to the entries in /etc/hosts. You can find more options for this in the dnsmasq(8) documentation.

Proxy and Firewall

Credit: youtube.com, Block DNS exfiltration with dnsmasq proxy - Magnus Longva - NDC Security 2023

Dnsmasq can behave as a proxy DHCP server to interoperate with an existing DHCP server on the network. This mode is only available with IPv4.

To set up proxy DHCP, you'll need to provide the address of the existing DHCP server. This allows dnsmasq to serve only the PXE server specific information to the client.

dnsmasq's proxy DHCP mode is a useful feature when you want to work with an existing network setup.

Block Google Redirects

You can block Google redirects by using the address option in dnsmasq without specifying the IP address. This will answer queries for the domain with NXDOMAIN.

To do this, add an entry like address=/google.com/ to your dnsmasq configuration file. Wildcards are also supported, so you can block all subdomains of a domain by adding a * to the start of the pattern, like address=/google.com/*.

Some specific subdomains can be unblocked using # as the server address, but the options address=/example.com/# and server=/example.com/# are not equivalent.

Credit: youtube.com, How to Remove cse.google.com Google Custom Search Proxy Redirects Permanently 2017 Latest

For ease of use, you can place the blocklist in a separate file, like /etc/dnsmasq.d/blocklist.conf, and load it from the main configuration file /etc/dnsmasq.conf with conf-file=/etc/dnsmasq.d/blocklist.conf or conf-dir=/etc/dnsmasq.d/,*.conf.

  • A list of potential sources for the blocklist can be found in OpenWrt's adblock package's README.
  • A hosts file blocklist can be used with the addn-hosts=hosts.txt option or it can be converted to a dnsmasq blocklist with this awk command: awk '/^[^#]/ { print "address=/"$2"/"$1"" }' hosts.txt.

Proxy

Proxy can be a powerful tool for network management.

In some cases, you might need to interoperate with an existing DHCP server on your network.

dnsmasq can be set to behave as a "proxy DHCP" in this situation, serving only #PXE server specific information to the client.

This mode is only available with IPv4.

To enable proxy DHCP, you'll need to provide the existing DHCP server address.

Firewall Alias (IPset)

Firewall Alias (IPset) is a powerful feature in Dnsmasq that allows you to add resolved IP addresses to firewall aliases. This is particularly useful in restricted networks or for gathering statistics.

To use this feature, Dnsmasq must be your primary DNS server for all clients, and access to any other DNS servers must be blocked. This ensures that Dnsmasq can manage the alias correctly.

Credit: youtube.com, pfSense Aliases: Simplify Your Firewall Rules | Tutorial

A managed firewall alias in Dnsmasq is more useful for allowlists than blocklists. This is because adding IPv4 and IPv6 addresses to the alias could unintentionally block access to shared hosting services.

Try to be selective with the domains you add to the alias, as adding a Top Level Domain (TLD) like com could inflate the alias to the point it becomes unusable. A good rule of thumb is to use one alias per service domain, which can later be nested under a parent alias.

Here's an example of how to create a managed firewall alias in Dnsmasq:

Router Advertisements

Router Advertisements play a crucial role in IPv6 networks, and understanding how they work is essential for setting up a reliable and efficient network. DHCPv6 does not have a router option like DHCPv4, so you must use Router Advertisements to push the default gateway to clients.

To use Router Advertisements, you can configure Dnsmasq or a different service like Services ‣ Router Advertisements. For example, adding a DHCPv6 range and Router Advertisements to your LAN interface can be done with the following configuration:

This configuration sets stateless DHCPv6 and SLAAC, meaning clients will use a SLAAC address but query additional DHCPv6 options, like the DNS Server.

Credit: youtube.com, Packet Tuesday - IPv6 Router Advertisements

You can also use the RA Mode setting to control how clients generate addresses. For instance, setting ra-stateless will only generate a SLAAC address, while setting slaac will generate a DHCPv6 address as well. If you want to automatically register DNS names for SLAAC addresses, you can set ra-names in addition to ra-stateless.

If you don't want to use Router Advertisements, you can leave the RA Mode on default and disable the Router Advertisement global setting. However, ensure that the RA service you use allows for an assisted setup with SLAAC and DHCPv6.

Cache and Statistics

Dnsmasq provides a way to view cache statistics using chaos requests and the drill utility from the ldns package.

You can query cache statistics to see the number of cache misses and hits.

Cache statistics can also be queried using various options such as cachesize.bind, insertions.bind, evictions.bind, auth.bind, and servers.bind.

Here's a list of some of the options you can use:

  • cachesize.bind
  • insertions.bind
  • evictions.bind
  • auth.bind
  • servers.bind

Network and Reservations

Credit: youtube.com, Dnsmasq configuration with network-manager

When working with Dnsmasq, it's essential to understand how to set up network reservations. A reservation is a way to assign a specific IP address to a device, so it's not offered to other devices on the network.

A DHCP reservation can be set up in a range that already exists, as long as the range is not set to static. This means you can have a dynamic range like 192.168.1.100-192.168.1.199 and a reservation like 192.168.1.101, and there will be no conflicts.

Reservations can also be outside the dynamic range, but it's not recommended for simple setups, as dynamic DNS registration with DHCP-fqdn will not work correctly.

To set up a reservation, you'll need to go to Services ‣ Dnsmasq DNS & DHCP ‣ Hosts and fill in the required information.

Here are the key options to fill in:

Note that setting a domain in the reservation has no effect on the dynamic DNS registration, but it will create a static host override.

Credit: youtube.com, DHCP Explained - Dynamic Host Configuration Protocol

In the case of IPv6 reservations, you'll need to use the device's unique identifier (DUID) instead of the hardware address. This example uses the common DUID-LL type: Client identifier (e.g. 00:03:00:01:aa:bb:cc:dd:ee:ff).

You can also combine IPv4 and IPv6 reservations in the same configuration item, as long as you use the correct options.

Multiple Instances and HA

Dnsmasq allows you to have multiple instances, which is useful for setups where you need to have two or more servers working per interface.

You can configure Dnsmasq to work with High Availability (HA) setups, especially in small and medium-sized network environments. This is a viable option when you don't need lease synchronization.

To set up HA with Dnsmasq, you'll need to disable HA sync for DHCP general settings and DHCP ranges. This ensures that the DHCP pools are not synchronized between the master and backup servers.

Here's a breakdown of the settings for the master and backup servers:

By having separate DHCP pools, you can avoid IP address conflicts between clients. The master server will respond to most DHCP discovers, and if it doesn't, the backup server will serve an IP address from its available pool.

This setup allows for a simple and efficient HA setup with automatic DNS registration. However, for larger scalable setups with big IP address ranges in many VLANs, KEA DHCP might be a better choice due to its robust HA synchronization options.

Troubleshooting and Tips

Credit: youtube.com, Troubleshooting dnsmasq

To prevent OpenDNS from redirecting all Google queries to their own search server, add to /etc/dnsmasq.conf.

If you're having issues with DNS resolution, try checking your dnsmasq configuration file for any errors.

One common mistake is forgetting to restart the dnsmasq service after making changes to the configuration file.

Exit Codes

Exit Codes can be a mystery, but they're actually quite straightforward. If you see a 1, it means there's a problem with your configuration.

A 2 exit code indicates a network access issue, such as trying to use a privileged port without permission. This can be frustrating, especially if you're trying to get your network up and running.

A 3 exit code points to a filesystem operation issue, like a missing file or directory, or incorrect permissions. This can happen if you've recently made changes to your system.

Memory allocation failure is what causes a 4 exit code. This can be a sign of a larger issue, like a resource-intensive program running in the background.

Credit: youtube.com, Fixing Command Failed with Exit Code ENOENT: Troubleshooting Guide

Other miscellaneous problems will give you a 5 exit code. This is a catch-all for any issues that don't fit into the other categories.

If you see an exit code of 11 or higher, it means a non-zero return code was received from the lease-script process or a conf-script file. The exit code from dnsmasq is the script's exit code with 10 added.

Tips and Tricks

To prevent OpenDNS from redirecting all Google queries to their own search server, add a specific line to your /etc/dnsmasq.conf file.

You can also customize your DNS settings by adding a specific line to your /etc/dnsmasq.conf file to prevent OpenDNS from hijacking your Google searches.

Authoritative Configuration

Dnsmasq's configuration file is usually located at /etc/dnsmasq.conf, but this can be changed using the -C option.

This file contains a list of options that can be used to customize Dnsmasq's behavior.

Some common options include setting the DNS server's IP address, enabling or disabling specific features, and configuring caching.

Dnsmasq can also be configured to use a separate configuration file for specific interfaces or networks.

This is done by creating a file with the same name as the interface or network, but with a .conf extension.

Description and Notes

Credit: youtube.com, raspberry on a boat #3: Access point creation with dnsmasq and hostapd

Dnsmasq is a lightweight, easy-to-use DNS forwarder that can also provide DHCP and TFTP services. It's a great tool for small networks.

Dnsmasq is designed to be a simple and efficient solution for DNS and DHCP needs. It can be used on a variety of systems, including Linux and other Unix-like operating systems.

One of the key features of Dnsmasq is its ability to cache DNS lookups, which can improve performance and reduce the load on DNS servers. This can be especially useful on networks with limited bandwidth.

Dnsmasq can also be configured to provide DHCP services, which can be useful for managing IP addresses on a network. It can also be used to assign hostnames to IP addresses.

Dnsmasq is a versatile tool that can be used in a variety of scenarios, from small home networks to larger enterprise environments. It's a great choice for anyone looking for a simple and efficient DNS and DHCP solution.

Frequently Asked Questions

Is dnsmasq necessary?

Dnsmasq is no longer necessary for caching DNS proxy in Fedora, as systemd-resolved provides a similar function since release 33

Is dnsmasq safe?

Unfortunately, dnsmasq has a history of critical vulnerabilities, including DNS cache poisoning, DoS, and RCE issues, making it a security risk. We recommend exploring alternative DNS servers to ensure the security of your network.

Melba Kovacek

Writer

Melba Kovacek is a seasoned writer with a passion for shedding light on the complexities of modern technology. Her writing career spans a diverse range of topics, with a focus on exploring the intricacies of cloud services and their impact on users. With a keen eye for detail and a knack for simplifying complex concepts, Melba has established herself as a trusted voice in the tech journalism community.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.