Skip to content
Wildcard Local Domains: hosts File Limits & dnsmasq

Wildcard Local Domains: hosts File Limits & dnsmasq

Updated 5 min read

The hosts file does not support wildcards like *.myapp.test. Learn why, and how to set up wildcard local domains with dnsmasq on macOS and Linux.

**The hosts file cannot do wildcards, a line like 127.0.0.1 *.myapp.test will not work. The hosts file matches exact** hostnames only, so every subdomain must be listed on its own line. When you need *.myapp.test (any subdomain) to resolve locally, use dnsmasq, a lightweight DNS resolver that supports wildcard rules.

Why the hosts file has no wildcards

The hosts file is a flat lookup table: one IP, one or more exact names per line. There is no pattern matching, so *, regexes, and ranges are ignored. For a few known subdomains, just list them:

127.0.0.1 myapp.test api.myapp.test admin.myapp.test
How many times will you retype this command this month?Automate it with ToggleHosts

See the hosts file syntax guide for the exact format. This breaks down only when subdomains are dynamic or too many to maintain.

Wildcard local domains with dnsmasq

dnsmasq resolves a domain and all its subdomains with one rule.

macOS (Homebrew)

brew install dnsmasq
echo 'address=/myapp.test/127.0.0.1' >> $(brew --prefix)/etc/dnsmasq.conf
sudo brew services start dnsmasq

Then tell macOS to send .test lookups to dnsmasq:

sudo mkdir -p /etc/resolver
echo 'nameserver 127.0.0.1' | sudo tee /etc/resolver/test

Now anything.myapp.test resolves to 127.0.0.1 without editing the hosts file.

Linux

sudo apt install dnsmasq
echo 'address=/myapp.test/127.0.0.1' | sudo tee /etc/dnsmasq.d/myapp.conf
sudo systemctl restart dnsmasq

Linux with systemd-resolved (Ubuntu, Fedora)

On these systems the stub resolver already holds port 53, so a default dnsmasq fails to start with "address already in use". Make dnsmasq listen on the loopback address only, and keep it out of the way of the system resolver:

# /etc/dnsmasq.d/local-dev.conf
listen-address=127.0.0.1
bind-interfaces
no-resolv
address=/test/127.0.0.1

Restart it, then route only .test lookups to it:

sudo systemctl restart dnsmasq
sudo resolvectl dns lo 127.0.0.1
sudo resolvectl domain lo '~test'

The ~test entry is a routing domain: names ending in .test go to dnsmasq, and every other lookup keeps using your normal resolver. These per-link settings do not survive a reboot and details vary by distribution, so check them with resolvectl status lo and resolvectl query foo.myapp.test. Then flush DNS on Linux.

Rules you will actually use

# Every .test name, not just one project
address=/test/127.0.0.1
address=/test/::1

# One name pointed somewhere else (the most specific rule wins)
address=/api.myapp.test/10.0.0.5

# Answer .test locally and never forward it upstream
local=/test/

address=/myapp.test/127.0.0.1 covers myapp.test and everything under it. Use address=/test/127.0.0.1 when you want every project on your machine to work without editing the configuration again. Adding the ::1 rule matters because browsers often try IPv6 first.

Apply and check your configuration

dnsmasq --test                      # syntax check
sudo brew services restart dnsmasq  # macOS
sudo systemctl restart dnsmasq      # Linux
scutil --dns | grep -B1 -A4 test    # macOS: confirm the /etc/resolver entry

Verify it works

dig foo.myapp.test @127.0.0.1
ping bar.myapp.test

Both should resolve to 127.0.0.1, but they test different things. dig ... @127.0.0.1 asks dnsmasq directly, so it proves the server works. It does not prove your system sends .test lookups there: on macOS, dig ignores the files in /etc/resolver. To test the whole path as an application sees it, use ping, or dscacheutil -q host -a name foo.myapp.test on macOS and getent hosts foo.myapp.test on Linux.

Pick a safe TLD first, see which TLD to use for local development.

Windows

Windows does not ship dnsmasq. If you need wildcard names there, the practical options are a third-party DNS proxy such as Acrylic DNS Proxy, which supports wildcard entries in its own hosts file (evaluate any such tool before installing it), or *.localhost names that browsers resolve without any setup, see subdomains on localhost.

HTTPS for wildcard names

A wildcard DNS rule does not give you a certificate. Generate one that covers the name and one level of subdomains:

mkcert myapp.test "*.myapp.test"

See local HTTPS with mkcert for the full setup.

Troubleshooting

  • "failed to create listening socket for port 53: Address already in use": another service owns port 53, usually the system resolver or a second dnsmasq. Find it with sudo lsof -i :53, and use listen-address=127.0.0.1 with bind-interfaces.
  • dig works but the browser does not: the system is not sending .test lookups to dnsmasq. Check /etc/resolver/test on macOS or the resolvectl routing on Linux, and test the browser's secure DNS setting.
  • A name resolves to the wrong IP: an entry in the hosts file wins over DNS, so remove a leftover line, or look for a more specific address= rule.
  • It stopped working after a reboot (Linux): the resolvectl per-link settings are not persistent. Reapply them, or script them at startup.
  • A VPN broke it: some VPN clients replace the system DNS settings. Reconnect after the VPN is up, or reapply the routing.

hosts file vs dnsmasq: which to use

  • A few fixed subdomains: the hosts file is simpler and needs no service.
  • Dynamic or many subdomains: dnsmasq with a wildcard rule.

For everyday fixed mappings, ToggleHosts manages hosts entries with safe toggles and automatic DNS flushing, so you only reach for dnsmasq when you genuinely need wildcards.

_Last tested: June 2026 on macOS and Ubuntu 24.04._

If you only need a few fixed names, subdomains on localhost compares the three approaches, including the free *.localhost option.

Sources and further reading

Frequently Asked Questions

No. The hosts file matches exact hostnames only. A line like 127.0.0.1 *.myapp.test does not work, each subdomain must be listed explicitly.

Use dnsmasq. A single rule like address=/myapp.test/127.0.0.1 resolves myapp.test and every subdomain to 127.0.0.1.

Create /etc/resolver/test with "nameserver 127.0.0.1" so macOS sends all .test lookups to dnsmasq.

If you only have a handful of fixed subdomains, list them in the hosts file. Use dnsmasq when subdomains are dynamic or numerous.

Related Articles

© 2026 ToggleHosts · All rights reserved[email protected]