Block ads and trackers using the hosts file (2026 guide)
Block ads, trackers and malware at system level with the hosts file. Best blocklists, setup guide, false positive management. Alternative to browser extensions.
Locahl Team
Table of Contents
- Why use the hosts file to block?
- System-level blocking
- Advantages over extensions
- What you can block
- How blocking works
- 0.0.0.0 vs 127.0.0.1
- The best blocklists
- Steven Black's Hosts (Recommended)
- Other popular lists
- Setting up a blocklist
- Manual method
- With Locahl
- Handling false positives
- Common symptoms
- Identifying the problematic domain
- Unblocking a domain
- Optimization for large lists
- Automatic update script
- Blocking limitations
- What it doesn't block
- Recommended complements
- Specific use cases
- Basic parental control
- Focused work environment
- Block telemetry
- Conclusion
Ads and trackers are ubiquitous on the modern web. While browser extensions like uBlock Origin do an excellent job, the hosts file offers a more radical solution: blocking unwanted domains at the system level, before they even reach your browser.
Why use the hosts file to block?
System-level blocking
Unlike browser extensions that only protect the browser, hosts file blocking works for all applications on your Mac:
- Browsers (Chrome, Safari, Firefox)
- Native applications
- Electron apps
- Games with built-in ads
Advantages over extensions
Performance Extensions analyze each request in real-time. The hosts file blocks before the request is even made.
Privacy No third-party extension has access to your browsing. Blocking is 100% local.
Reliability No extension updates to manage, no conflicts with blocker detectors.
What you can block
- Ads: banners, popups, pre-roll videos
- Trackers: Google Analytics, Facebook Pixel
- Malware: known malicious domains
- Telemetry: data collection by applications
- Crypto-miners: mining scripts
If you're not yet familiar with the hosts file, check out our complete guide to the hosts file first.
Simplify your hosts file management
Locahl lets you manage your hosts file visually, without touching the terminal. Automatic DNS flush, multiple environments, and backups included.
How blocking works
When you add an entry like:
0.0.0.0 ads.example.comYou're telling your system: "When someone requests ads.example.com, return address 0.0.0.0 instead of the real IP".
The address 0.0.0.0 is non-routable. The connection fails instantly.
0.0.0.0 vs 127.0.0.1
0.0.0.0 (recommended)
- Fails immediately
- No connection attempt
- Faster
127.0.0.1 (localhost)
- Attempts a local connection
- Can create a delay
The best blocklists
Steven Black's Hosts (Recommended)
The reference for consolidated hosts lists.
URL: github.com/StevenBlack/hosts
Contents:
- Base version: ~60,000 domains (ads + malware)
- Extended versions available
- Regularly updated
Other popular lists
Dan Pollock's hosts
- URL: someonewhocares.org/hosts/
- About 14,000 domains
- Maintained for 20+ years
Energized Protection
- URL: github.com/EnergizedProtection/block
- Ultra-complete versions (up to 1M+ domains)
Setting up a blocklist
Manual method
1. Download the list
curl -o ~/Downloads/hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts2. Back up your current hosts file
sudo cp /etc/hosts /etc/hosts.backup3. Merge the files
cat /etc/hosts.backup ~/Downloads/hosts | sudo tee /etc/hosts > /dev/null4. Flush DNS cache
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponderFor more details on DNS flushing, check our complete guide to flushing DNS on Mac.
With Locahl
1. Download the list 2. Use the import function 3. Enable/disable individually 4. Automatic DNS flush
Handling false positives
Aggressive blocking can sometimes break legitimate sites.
Common symptoms
- Images not loading
- Login buttons not working
- Videos not playing
- Payment errors
Identifying the problematic domain
1. Open DevTools (F12 or Cmd+Option+I) 2. "Network" tab 3. Filter by "blocked" or look for errors
Unblocking a domain
In the hosts file, comment out the line:
# 0.0.0.0 necessary-domain.comOptimization for large lists
Automatic update script
#!/bin/bash
BACKUP=/etc/hosts.custom
BLOCKLIST=https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
# Save custom entries
sudo head -50 /etc/hosts > $BACKUP
# Download and merge
curl -s $BLOCKLIST | cat $BACKUP - | sudo tee /etc/hosts > /dev/null
# Flush DNS
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
echo "Hosts file updated!"Blocking limitations
What it doesn't block
First-party ads Ads served from the same domain as content (e.g., YouTube) can't be blocked without blocking the entire service.
Dynamic content Constantly changing domains can escape blocking.
Recommended complements
- Pi-hole: Filtering DNS for your entire network
- Little Snitch: Application firewall for Mac
- NextDNS: Cloud filtering DNS
Specific use cases
Basic parental control
0.0.0.0 facebook.com
0.0.0.0 www.facebook.com
0.0.0.0 instagram.com
0.0.0.0 tiktok.comFocused work environment
0.0.0.0 twitter.com
0.0.0.0 reddit.com
0.0.0.0 youtube.comBlock telemetry
0.0.0.0 telemetry.microsoft.com
0.0.0.0 ic.adobe.ioConclusion
The hosts file is a powerful tool for blocking ads, trackers, and malicious domains at system level. While more modern solutions exist (Pi-hole, NextDNS), the simplicity of the hosts file makes it an effective first line of defense.
For comfortable management of these thousands of entries, a tool like Locahl lets you import lists and enable/disable individual blocks.
Ready to simplify your workflow?
Stop wasting time with the terminal. Locahl lets you manage your hosts file in a few clicks, with automatic validation and no risk of errors.
- Intuitive visual interface
- Automatic DNS flush
- Multi-environment management
- Automatic backups
- JSON Import/Export
Reader Reviews
"The Steven Black list transformed my browsing. No more ads on my desktop apps, not just the browser."
November 28, 2025
"Great guide. A few false positives at first but the troubleshooting section helped me resolve them."
December 15, 2025
"Finally a blocking solution that works everywhere, even in games. The auto-update script is brilliant."
January 10, 2026
Frequently Asked Questions
Is hosts file blocking effective against ads?
Yes, very effective because blocking happens at system level, before the request even reaches the network. Unlike extensions, it works for all applications.
What's the difference between 0.0.0.0 and 127.0.0.1 for blocking?
Both work, but 0.0.0.0 is recommended. It fails instantly without attempting a connection, while 127.0.0.1 can create a delay.
How many domains can you block with the hosts file?
No strict limit. Popular lists contain 50,000 to 200,000 domains. Beyond 100,000 entries, slight slowdown possible.
Does hosts file blocking affect performance?
Negligible impact. The hosts file is loaded into memory and lookups are very fast. Blocking ads often improves browsing performance.
How do I unblock a site blocked by mistake?
Search for the domain in your hosts file and delete or comment out (with #) the line. Then flush DNS cache.
Related Articles
Pi-hole vs hosts file: which ad blocker should you use? (2026)
Detailed comparison of Pi-hole vs hosts file for blocking ads and trackers. Pros, cons, use cases. Which one to choose based on your situation.
Locahl Team
Terminal vs GUI: Why Developers Are Switching to Hosts File Apps
Compare editing hosts file via Terminal vs using Locahl GUI. Learn about permission issues, typo risks, backups, DNS flush, and why GUI hosts file managers are better for developers.
Locahl Team
Hosts File Not Working After Edit on Mac: Complete Fix Guide
Your hosts file changes not taking effect? Learn how to fix DNS cache issues, browser cache, file permissions, syntax errors, and encoding problems on macOS.
Locahl Team