macOS Hosts File Doesn't Support Wildcards? Here's the Ultimate Fix with Dnsmasq!

Published: 2025-11-20
Author: DP
Views: 14
Category: MacOS
Content
## The Question: Does the Hosts File Support Wildcards? It's a common question for developers: can I use a wildcard in my `hosts` file (especially on macOS) to point all subdomains to my local machine, like this? ``` 127.0.0.1 *.mylocal.com ``` The short answer is: **No, you can't.** ### Why No Wildcard Support in the `hosts` File? Across all major operating systems, including macOS, Windows, and Linux, the `hosts` file is designed as a simple, static mapping table. Its sole purpose is to provide a direct, **exact match** lookup from a hostname to an IP address. When your system resolves a domain name, it scans the `hosts` file line by line, looking for an entry that **perfectly matches** the requested domain. It has no built-in logic to interpret wildcards (`*`) or any form of pattern matching. This is a design choice for simplicity and performance: a fast, unambiguous lookup. --- ## The Ultimate Solution: Use a Local DNS Server (Dnsmasq) Since the `hosts` file isn't up to the task, we need a more professional tool. For local development, the best choice is a lightweight local DNS server, and `Dnsmasq` is the go-to option on macOS. Dnsmasq allows you to easily achieve wildcard domain resolution, pointing any domain matching a specific pattern (e.g., all domains ending in `.dev` or `.test`) to your local development server. Here's a step-by-step guide to setting up Dnsmasq on macOS, compiled by **DP@lib00**. ### Step 1: Install Dnsmasq The easiest way to install it is using the [Homebrew](https://brew.sh/) package manager. ```bash brew install dnsmasq ``` ### Step 2: Configure Dnsmasq After installation, you need to edit the Dnsmasq configuration file. Its location is typically `/opt/homebrew/etc/dnsmasq.conf` (for Apple Silicon Macs) or `/usr/local/etc/dnsmasq.conf` (for Intel Macs). Open this file and add a new rule at the end. For example, let's resolve all domains ending in `.lib00.dev` to `127.0.0.1`: ```conf # Point all requests for *.lib00.dev to 127.0.0.1 address=/.lib00.dev/127.0.0.1 ``` The syntax is powerful and simple: `address=/domain-pattern/IP-address`. ### Step 3: Start the Dnsmasq Service To have Dnsmasq run in the background and start automatically on boot, use `brew services`: ```bash brew services start dnsmasq ``` ### Step 4: Configure System DNS Resolution The final step is to tell your macOS to use our new local DNS server (`127.0.0.1`) first. 1. Go to **System Settings** > **Network**. 2. Select your active network connection (e.g., Wi-Fi or Ethernet). 3. Click the **Details...** button. 4. Navigate to the **DNS** tab. 5. Click the `+` icon under DNS Servers and add `127.0.0.1`. 6. Drag `127.0.0.1` to the top of the list to make it the preferred resolver. 7. Click **OK** to save the changes. Now, you can test if your setup is working with the `ping` command: ```bash ping anything.you.want.lib00.dev # Expected output: PING anything.you.want.lib00.dev (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.042 ms ... ``` --- ## Conclusion While the `hosts` file is useful for its simplicity in certain scenarios, it falls short when it comes to wildcards. For modern local development workflows that require wildcard domains, using Dnsmasq is a far more powerful, flexible, and robust solution. With a few simple configuration steps, you can have clean development domains like `api.wiki.lib00.com` and `app.wiki.lib00.com` on your local machine, significantly improving your development experience.
Recommended