When dnsmasq fails to resolve entries from /etc/hosts
while properly functioning as a local DNS server, several configuration aspects need verification:
# Verify dnsmasq is actually running
ps aux | grep dnsmasq
netstat -tulnp | grep :53
The minimal working configuration should include these directives in /etc/dnsmasq.conf
:
# Required for proper /etc/hosts resolution
no-hosts # Disable default /etc/hosts reading (if needed)
addn-hosts=/etc/hosts # Explicitly specify hosts file
local=/lan/ # Define local domain
expand-hosts # Append domain to simple names
domain=lan # Default domain for expand-hosts
Use these diagnostic commands to verify resolution:
# Test with +short for clean output
dig +short sun.lan @127.0.0.1
# Verify dnsmasq logs (Ubuntu specific)
tail -f /var/log/syslog | grep dnsmasq
# Alternative testing method
nslookup sun.lan 127.0.0.1
Here's a production-ready configuration for LAN DNS resolution:
# /etc/dnsmasq.conf
interface=eth0
listen-address=127.0.0.1
bind-interfaces
domain=lan
expand-hosts
local=/lan/
addn-hosts=/etc/hosts.dnsmasq
no-resolv
server=8.8.8.8
server=8.8.4.4
When basic configuration doesn't work:
# 1. Verify file permissions
ls -la /etc/hosts
# 2. Check for multiple dnsmasq instances
sudo killall dnsmasq
sudo service dnsmasq restart
# 3. Test with minimal configuration
dnsmasq --no-daemon --log-queries --conf-file=/dev/null --addn-hosts=/etc/hosts
Problem: Entries resolve only with FQDN
Solution: Add expand-hosts
and proper domain specification
Problem: Changes don't take effect
Solution: Clear dnsmasq cache with sudo killall -HUP dnsmasq
# Example working /etc/hosts entry:
192.168.1.13 sun.lan sun
When DNSMasq fails to resolve entries from /etc/hosts
while still functioning as a local DNS resolver, we're typically dealing with configuration conflicts or missing directives. The symptoms show DNSMasq responding to queries (confirmed by dig @127.0.0.1
) but not honoring the local hostfile entries.
First, verify these essential configuration points:
# Minimum required dnsmasq.conf for hostfile resolution
no-hosts # MUST be absent or commented out
read-ethers # Optional for additional host records
addn-hosts=/path/to/extra/hosts # Optional additional host files
local=/yourdomain.local/ # Important for FQDN resolution
expand-hosts # Combines with 'local' domain setting
Here's a functional configuration that resolves the issue:
# /etc/dnsmasq.conf
no-resolv
server=8.8.8.8
server=8.8.4.4
local=/lan/
expand-hosts
domain=lan
Corresponding /etc/hosts
entry:
192.168.1.13 sun.lan sun
Use these commands to verify functionality:
# Check if dnsmasq is reading hosts file
sudo dnsmasq --test --log-queries
# Query with verbose output
dig sun.lan @127.0.0.1 +trace +all
# Check configuration processing
sudo dnsmasq --test
1. Missing Local Domain: Without local=/domain/
directive, bare hostnames won't resolve
2. Conflicting Options: no-hosts
will disable /etc/hosts
reading entirely
3. Order Matters: Some distributions have config snippets that may override main config
For complex setups with multiple domains:
# /etc/dnsmasq.conf
local=/lan/
local=/internal/
host-record=server1.lan,192.168.1.10
host-record=server2.internal,10.0.0.5