Overview and Quick Answer
To configure DNSmasq for DNS forwarding and service discovery, install DNSmasq, edit its main configuration to set a trusted upstream resolver, define any required server-specific addresses, enable authoritative mode for your LAN, and add static DHCP mappings or DNS records for services. DNS forwarding sends recursive queries to upstream servers; service discovery maps hostnames to local IPs via DNSmasq’s built-in DHCP or static leases. This evergreen setup guide includes exact configuration snippets, common options, and verification commands you can reuse on home and small office networks.
What Is DNSmasq and Its Main Use Cases
DNSmasq is a lightweight, open source networking utility that provides DNS forwarding, DHCP, router advertisement, and small-scale service discovery on IP networks. It is designed for small networks where native BIND is unnecessary. Common use cases include:
- Home routers and SOHO environments to cache DNS and reduce upstream queries.
- Local development and lab networks to map friendly hostnames to machines and containers.
- Edge devices and embedded systems needing low-footprint DNS and DHCP in a single process.
By combining caching, forwarding, and DHCP, DNSmasq gives you deterministic name resolution for local services without running a full authoritative DNS server.
Understanding DNS Forwarding Versus Caching
Forwarding Sends Queries Upstream; Caching Stores Answers Locally
DNS forwarding in DNSmasq means the software forwards recursive queries to one or more upstream DNS resolvers instead of answering questions itself. Caching stores responses from upstream servers so repeated lookups are served locally with lower latency. With forwarding enabled, DNSmasq accepts queries from clients, optionally filters or rewrites them, and then relays them to remote resolvers. It can also cache the results to speed subsequent requests. The two behaviors—forwarding and caching—can coexist, and most deployments use both to improve reliability and reduce bandwidth.
Planning Your DNSmasq Deployment
Before editing files, clarify scope, network segments, and IP management approach. Decide whether DNSmasq will be authoritative for your LAN, whether it should forward all non-local traffic, and whether you need conditional forwarding for specific domains. Inventory the set of services and their hostnames, and define a consistent naming convention. Determine where the upstream resolvers are (router, ISP, public DNS, or DoH relays). Also plan failover by listing at least two upstream servers so queries succeed when one resolver is unreachable.
Core Configuration for DNS Forwarding
Essential Options and Security Considerations
Minimal DNSmasq configuration for DNS forwarding starts with specifying upstream servers, binding to the right interfaces, and enabling required features. Key options include:
| Option | Effect | Typical Value |
|---|---|---|
| server | Upstream resolver address | 8.8.8.8, 1.1.1.1, provider DNS |
| no-resolv | Ignore /etc/resolv.conf | enabled to use static server list |
| bind-dynamic | Bind dynamically on interfaces | useful on multihomed hosts |
| domain-needed | Suppress queries without a dot | prevents leaking local names upstream |
| bogus-priv | Reject or log RFC1918 upstream replies | protects against misconfigured resolvers |
| local | Answer local-zone queries first | use for domain.local authority |
| expand-hosts | Add domain to simple names | enables host.example.local style lookups |
| domain | Local domain name | set to match your local zones |
Place server lines for each upstream resolver before interface-specific settings. Use no-resolv when you want full control, and prefer specific addresses over relying on /etc/resolv.conf. For encrypted upstream transport, consider using dnsmasq with a DoH helper or a local stub resolver that supports DoH.
Configuring Service Discovery with Static Mappings
Map Hostnames to Local IPs for Reliable Resolution
To enable service discovery, add explicit address records for services so clients can resolve names like database.local or printer.local to local IPs. In DNSmasq, use address=/name/ip or host-record lines. When a client asks for database.local, DNSmasq answers with the IP you configured regardless of DHCP churn. For environments with predictable services, these static mappings act as a lightweight service registry. Combine them with descriptive names and a consistent domain to make discovery predictable and scriptable.
Integrating DHCP to Build Dynamic Service Records
Leverage DHCP Leases for Automatic Name and Service Discovery
DNSmasq can assign names and record addresses via DHCP, turning leases into service entries. Enable DHCP on an interface, set an address range, and specify authoritative mode so hosts accept leases from DNSmasq. Use dhcp-host to create static mappings by MAC, and use dhcp-range for dynamic allocations. For service discovery, pair DHCP with DNS updates or static address directives to ensure service names resolve to current lease addresses. Optionally configure dns-forwardrev to control reverse lookup behavior and keep PTR records consistent with forward lookups.
Verification, Testing, and Ongoing Maintenance
Confirm Forwarding, Caching, and Local Name Resolution
After applying configuration, restart DNSmasq and validate behavior. Use dig or nslookup to confirm that queries reach upstream resolvers and that local names resolve correctly. Check logs for errors, timeouts, or bogus priv violations indicating misconfiguration. Monitor query volume and cache hit ratio to size upstream capacity. Refresh DHCP ranges and lease times periodically, and review static mappings when services move or IPs change. Keep an eye on upstream resolver availability to avoid outages if a preferred resolver becomes unreachable.
Example Minimal Configuration
A concise, reusable DNSmasq configuration for forwarding and local service discovery may look like this:
# Listen on LAN interface only
listen-address=192.168.1.1
bind-interfaces
# Do not read /etc/resolv.conf
no-resolv
# Upstream resolvers (primary, fallback)
server=8.8.8.8
server=8.8.4.4
# Local domain and domain-needed
local=/lan/
domain=lan
domain-needed
bogus-priv
# Authoritative for LAN domain
authoritative
# Static service mappings
address=/nas.local/192.168.1.10
address=/printer.local/192.168.1.20
# DHCP range with names
DHCP range=192.168.1.50,192.168.1.150,12h
dhcp-host=aa-bb-cc-dd-ee-ff,pc01,192.168.1.30
# Log for troubleshooting
log-queries
log-facility=/var/log/dnsmasq.log
Adapt addresses, domains, and ranges to your environment. Verify with dig nas.local @192.168.1.1 and confirm you receive 192.168.1.10. Confirm forwarding by querying an external name and inspecting logs for the upstream server used.
Common Issues and Practical Tips
- Name resolution fails for local services: ensure address or host-record lines exist and DNSmasq reloads after config changes.
- Upstream timeouts: verify server lines are reachable, check firewall rules for outbound UDP 53, and consider adding a secondary resolver.
- Local domain leaks: use domain-needed and bogus-priv to prevent queries for internal names from being sent upstream.
- DHCP and static mappings conflict: prefer static mappings for critical services and use DHCP dynamic ranges for general clients.
- Clients ignoring DNSmasq: ensure the DHCP option for router/domain DNS points to your DNSmasq host and that firewall allows DHCP server ports.