
Block Ads With the Hosts File (2026)
Block ads, trackers and malware at system level with the hosts file. Best blocklists, setup guide, false positive management. Alternative to browser extensions.
Manage hosts files without the terminal
ToggleHosts helps you manage environments visually on Windows, macOS, and Linux, with automatic DNS flush and backups.
One-time payment
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.
How to block ads with the hosts file
To block ads with the hosts file, add a curated blocklist that points ad and tracker domains to 0.0.0.0:
- Open the hosts file as administrator (
sudo nano /etc/hostson Mac/Linux, Notepad as admin on Windows). - Add entries like
0.0.0.0 ads.example.com, or paste a maintained blocklist (e.g. StevenBlack hosts). - Save the file.
- Flush DNS so the changes take effect immediately.
- Reload a page to confirm ad domains no longer load.
Recommended blocklists, maintenance tips, and limits are covered below.
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.
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
- Download the list
curl -o ~/Downloads/hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts- Back up your current hosts file
sudo cp /etc/hosts /etc/hosts.backup- Merge the files
cat /etc/hosts.backup ~/Downloads/hosts | sudo tee /etc/hosts > /dev/null- 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 ToggleHosts
- Download the list
- Use the import function
- Enable/disable individually
- Automatic DNS flush
Prefer a visual walkthrough? This demo shows how to paste a whole blocklist at once:
Bulk Import Hosts File Entries — Paste Your Whole List at Once · @ToggleHosts_app
Safe setup workflow
Do not replace your entire hosts file blindly. A safe setup keeps system entries, your project mappings and blocklist entries separated.
Recommended file structure
# System defaults
127.0.0.1 localhost
::1 localhost
# Local development
127.0.0.1 myproject.test
127.0.0.1 api.myproject.test
# Ad/tracker blocking
0.0.0.0 ads.example.com
0.0.0.0 tracker.example.comThis makes rollback easier. If a blocklist breaks a site, you can inspect only the blocking section instead of scanning your local development mappings too.
Before importing a list
- Back up
/etc/hosts. - Keep your custom entries in a separate file.
- Start with one conservative list.
- Avoid importing several huge lists on day one.
- Flush DNS and restart affected apps after the import.
Backup command:
sudo cp /etc/hosts /etc/hosts.backup.$(date +%Y%m%d)Choosing the right blocklist size
Blocklists come in different levels of aggressiveness.
Small lists
Best for people who want low maintenance. They block common ad and malware domains but rarely break websites.
Use a small list if:
- You work with many client dashboards.
- You use payment, analytics or marketing tools daily.
- You do not want to troubleshoot false positives often.
Medium lists
Best default for technical users. Steven Black's standard list is a good example: broad enough to matter, conservative enough for daily use.
Use a medium list if:
- You want noticeable blocking.
- You can troubleshoot occasional false positives.
- You want a good balance between privacy and usability.
Aggressive lists
Useful for locked-down environments, kiosks or strict focus setups. They can block telemetry, social domains, tracking, adult content or gambling.
Use with care. Aggressive lists can break login flows, embedded media, comment systems, checkout pages and corporate SaaS apps.
How to test whether blocking works
After installing a blocklist, test at three levels.
System resolution
dscacheutil -q host -a name ads.example.com
ping ads.example.comBlocked domains should resolve to 0.0.0.0, 127.0.0.1 or fail immediately depending on the list.
Browser cache
Browsers may cache the old result. In Chrome:
- Open
chrome://net-internals/#dns. - Click "Clear host cache".
- Open
chrome://net-internals/#sockets. - Flush socket pools.
Application behavior
Test apps that showed ads before. Hosts blocking works outside the browser too, but some apps use first-party domains that cannot be blocked without breaking the service.
Updating blocklists
A stale blocklist loses value over time. New tracker domains appear constantly, and old false positives get fixed upstream.
For most users, updating weekly or monthly is enough. Daily updates are usually unnecessary unless you manage a security-focused environment.
A safer update script should:
- Keep custom entries.
- Download the new list.
- Validate that the file is not empty.
- Create a backup.
- Replace the hosts file.
- Flush DNS.
If you use ToggleHosts, keep imported lists separate from custom project entries so you can update one without touching the other.
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
- Open DevTools (F12 or Cmd+Option+I)
- "Network" tab
- 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
Hosts file vs Pi-hole vs extensions
Use the hosts file when you want system-wide blocking on one Mac. Use Pi-hole when you want network-wide filtering for many devices. Use uBlock Origin when you need cosmetic filtering inside the browser.
They work well together:
- Hosts file for local machine rules.
- Pi-hole for home or office devices.
- Browser extension for page-level cleanup.
For a detailed comparison, read Pi-hole vs hosts file.
Rollback plan
If something goes wrong, restore your backup:
sudo cp /etc/hosts.backup /etc/hosts
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponderThen clear browser DNS cache if the site remains broken.
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 ToggleHosts lets you import lists and enable/disable individual blocks.
Frequently Asked Questions
Yes, very effective because blocking happens at system level, before the request even reaches the network. Unlike extensions, it works for all applications.
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.
No strict limit. Popular lists contain 50,000 to 200,000 domains. Beyond 100,000 entries, slight slowdown possible.
Negligible impact. The hosts file is loaded into memory and lookups are very fast. Blocking ads often improves browsing performance.
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 Blocks Ads Better?Comparisons & tools
Pi-hole vs Hosts File: Which Blocks Ads Better?
8 min read
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)Troubleshooting
Fix NET::ERR_CERT_COMMON_NAME_INVALID with Hosts File (2026)
4 min read
Fix EACCES: permission denied on /etc/hosts (2026)Troubleshooting
Fix EACCES: permission denied on /etc/hosts (2026)
3 min read