Blocking an entire subdomain with a single line in your hosts file is technically impossible due to the lack of wildcard support. The only reliable one-line solution is to redirect that specific subdomain to 127.0.0.1, which prevents access to that exact name while leaving other subdomains untouched.
The Limitation of Wildcards in Hosts Files
Many users seek a magic bullet configuration to block all subdomains (likea.example.com, b.example.com) using just one entry. However, modern operating systems do not support wildcards in the /etc/hosts file or the Windows equivalent. A line such as *.example.com 127.0.0.1 will be ignored by the resolver because the hosts file parser expects explicit, literal domain names.
This limitation exists for security and predictability reasons. If wildcards were allowed, a misconfigured entry could inadvertently block critical services or internal network resources. Therefore, you cannot achieve true "subdomain blocking" via the hosts file alone if you need to cover multiple varying subdomains. You must either list each subdomain individually or use a different method entirely.
The Single-Line Solution: Exact Redirect
If your goal is to block a specific, known subdomain (for example,ads.tracking.com), you can use a single line to redirect it to localhost. This effectively blocks access to that specific address.
To implement this, open your hosts file with administrative privileges. On Linux and macOS, this file is located at /etc/hosts. On Windows, it is typically found at C:\Windows\System32\drivers\etc\hosts.
Add the following line to the bottom of the file:
127.0.0.1 ads.tracking.com
This tells your system's DNS resolver to return the loopback address instead of querying the actual DNS server for that specific hostname. When you visit http://ads.tracking.com in your browser, the request fails locally because there is no service listening on port 80 at 127.0.0.1.
For IPv6 systems, you may also want to add:
::1 ads.tracking.com
This ensures compatibility with systems that prefer IPv6 connections. While this does not block *all* subdomains, it is the correct way to block a *single* subdomain using one line.
Alternative Methods for Full Subdomain Blocking
Since the hosts file cannot handle wildcards, you must look elsewhere if you need to block an entire subdomain tree. Here are the most effective alternatives.Using Local DNS Servers
Local DNS servers like Pi-hole or AdGuard Home support wildcard blocking. You can configure a rule to block*.example.com. This approach intercepts DNS queries before they leave your network and returns a null response for any subdomain under that domain. This is far more powerful than the hosts file and requires no manual maintenance of individual entries.
Browser Extensions and Hosts File Managers
Tools like uBlock Origin or specialized hosts file managers can generate thousands of lines automatically. While these result in many lines in the hosts file, they provide comprehensive coverage. For a single-subdomain block, this is overkill, but for broader privacy, it is the standard practice.DNS Configuration Changes
If you control the DNS zone, you can create a catch-all record. However, this affects everyone who uses that DNS zone, not just your local machine. For personal blocking, local tools are superior. See DNS Settings with external hosted domain for more context on how DNS resolution works globally.Verifying Your Configuration
After editing the hosts file, you should verify that the change has taken effect. Modern operating systems cache DNS lookups, so you may need to flush your DNS cache. On Linux, you can often restart the networking service or usesudo systemd-resolve --flush-caches. On macOS, use sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder. On Windows, run ipconfig /flushdns in the command prompt.
To test the block, open a terminal and use the ping command:
ping ads.tracking.com
You should see responses from 127.0.0.1 (or ::1). If you see a real IP address, the hosts file entry was not applied correctly, or your cache has not been flushed.
It is also worth noting that some applications bypass the system hosts file and use their own DNS resolvers. Browsers like Chrome sometimes have their own caching mechanisms. If the block does not work immediately, try restarting your browser or checking if the application has its own proxy settings.
For more information on managing domains and IPs, you might find Is it possible to register 2 domains for the same IP? helpful, as it touches on the fundamental relationship between hostnames and IP addresses.
Common Pitfalls
One common mistake is adding comments incorrectly. Ensure that lines starting with# are treated as comments and not parsed as domain names. Another issue is syntax errors; ensure there is a space or tab between the IP address and the domain name. Multiple spaces are fine, but missing separators will cause the entry to fail.
Also, remember that this method only blocks HTTP and HTTPS traffic if the application respects the system DNS. It does not block raw socket connections that resolve DNS internally. For complete isolation, firewall rules are necessary.