How to Configure dnsmasq for Wildcard Local Domain Resolution in Development Environments


1 views

I recently set up a development environment where multiple web applications need to be accessible via custom domains. Our network has a router at 192.168.3.1 which we can't configure, so we're using dnsmasq on our development server (devbox at 192.168.3.99) for local DNS resolution.

Initially, I added entries to /etc/hosts like:

192.168.3.99 website1.devbox
192.168.3.99 website2.devbox
192.168.3.99 website3.devbox

While this worked, it didn't scale well - we needed to add a new entry for each test domain.

Here's the proper way to configure wildcard *.devbox resolution:

1. Install and Configure dnsmasq

sudo apt-get install dnsmasq

2. Edit dnsmasq Configuration

Add these lines to /etc/dnsmasq.conf:

# Enable wildcard resolution for .devbox domains
address=/.devbox/192.168.3.99
local=/devbox/
expand-hosts
domain=devbox

3. Configure Network Manager

Edit /etc/NetworkManager/NetworkManager.conf to prevent it from overwriting resolv.conf:

[main]
dns=dnsmasq

4. Apache Virtual Host Setup

Configure Apache to handle wildcard subdomains:

<VirtualHost *:80>
  ServerName devbox
  ServerAlias *.devbox
  VirtualDocumentRoot /var/www/%1
</VirtualHost>

After restarting services:

sudo service dnsmasq restart
sudo service apache2 restart

Test with:

ping anyname.devbox
nslookup test.devbox

For even better separation, consider using .test (RFC 2606 reserved):

address=/.test/192.168.3.99
local=/test/
  • Check logs: tail -f /var/log/syslog
  • Verify DNS queries: dig @192.168.3.99 test.devbox
  • Clear local DNS cache: sudo systemd-resolve --flush-caches

When setting up a local development environment with multiple projects, manually editing /etc/hosts becomes cumbersome. The ideal solution should:

  • Automatically resolve all *.devbox domains
  • Work without router access
  • Maintain existing external DNS resolution

Here's the proper way to configure wildcard resolution:

# /etc/dnsmasq.conf
# Enable wildcard resolution for .devbox domains
address=/.devbox/192.168.3.99
local=/devbox/
expand-hosts
domain=devbox

Configure Apache to handle wildcard subdomains:

# /etc/apache2/sites-available/devbox.conf
<VirtualHost *:80>
    ServerName devbox
    ServerAlias *.devbox
    DocumentRoot /var/www/html
    <Directory "/var/www/html">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

On each developer's machine (Ubuntu/Debian):

# /etc/resolvconf/resolv.conf.d/head
nameserver 192.168.3.99
nameserver 8.8.8.8  # Fallback to Google DNS

Then run:

sudo resolvconf -u

Verify the configuration works:

dig anytest.devbox

;; ANSWER SECTION:
anytest.devbox.     0       IN      A       192.168.3.99
  • Check dnsmasq logs: sudo tail -f /var/log/syslog | grep dnsmasq
  • Restart services: sudo systemctl restart dnsmasq apache2
  • Clear DNS cache: sudo systemd-resolve --flush-caches

For more granular control:

# /etc/dnsmasq.d/dev-projects.conf
address=/project1.devbox/192.168.3.100
address=/project2.devbox/192.168.3.101
local=/devbox/