
Wildcard Local Domains: hosts File Limits & dnsmasq
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.testSee 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 dnsmasqThen tell macOS to send .test lookups to dnsmasq:
sudo mkdir -p /etc/resolver
echo 'nameserver 127.0.0.1' | sudo tee /etc/resolver/testNow 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 dnsmasqOn systemd-resolved setups, ensure dnsmasq is the resolver for .test or point /etc/resolv.conf accordingly, then flush DNS on Linux.
Verify it works
dig foo.myapp.test @127.0.0.1
ping bar.myapp.testBoth should resolve to 127.0.0.1. Pick a safe TLD first, see which TLD to use for local development.
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._
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
Complete Guide to /etc/hosts Syntax and FormatEditing the hosts file
Complete Guide to /etc/hosts Syntax and Format
9 min read
Which TLD to Use for Local Development (.test vs .localhost)Local development
Which TLD to Use for Local Development (.test vs .localhost)
2 min read
How to Use Subdomains on localhost for Local Development (2026)Local development
How to Use Subdomains on localhost for Local Development (2026)
4 min read