Sleezr
Buy Sleezr
hosts fileblock websitead blockingproductivitytutorial

How to Block a Website with the Hosts File (2026)

Block any website system-wide using the hosts file. Redirect domains to 0.0.0.0 or 127.0.0.1 on Windows, Mac and Linux. Unblock mistakes and avoid pitfalls.

S

Sleezr Team

·Updated ·7 min read
How to Block a Website with the Hosts File (2026)

The hosts file is one of the simplest ways to block a website on your entire computer, not just in one browser, but in every application that uses the system DNS resolver. Redirect a domain to 0.0.0.0 and requests fail before they leave your machine. No extension required. No subscription.

How to block a website with the hosts file

To block a website with the hosts file, point its domain to 0.0.0.0 and flush DNS:

1
Open the hosts file as administrator (sudo nano /etc/hosts on Mac/Linux, Notepad as admin on Windows).
2
Add a line such as 0.0.0.0 example.com.
3
Add the www variant too: 0.0.0.0 www.example.com.
4
Save the file.
5
Flush DNS (ipconfig /flushdns on Windows, sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder on Mac).

Why 0.0.0.0 is preferred over 127.0.0.1, and how to block at scale, is covered below.

This guide shows how to block any website on Windows, Mac, and Linux, plus how to unblock mistakes, block multiple domains efficiently, and understand the limitations.

See also: Main editing guide · Block ads with the hosts file

How hosts file blocking works

When your computer tries to connect to facebook.com, it first checks the hosts file. If the file contains:

TEXT
0.0.0.0 facebook.com

The OS resolves facebook.com to 0.0.0.0, a non-routable address. The connection fails immediately. The request never reaches Facebook's servers or any DNS server.

This is system-wide blocking, it affects:

  • All browsers (Chrome, Firefox, Safari, Edge)
  • Desktop applications
  • Command-line tools (curl, wget)
  • Many Electron apps

It does not affect:

  • Apps using hardcoded IP addresses
  • Apps using DNS-over-HTTPS that bypass local resolution
  • Other devices on your network (blocking is local to one machine)

0.0.0.0 vs 127.0.0.1, which to use?

AddressBehaviorRecommended?
0.0.0.0Invalid route, instant failureYes, preferred
127.0.0.1Routes to localhost, may cause delayWorks, but slower
:: (IPv6)IPv6 invalid/null routeUse for IPv6-enabled apps

Use `0.0.0.0` for blocking. It is the standard in ad-blocking lists like Steven Black's hosts file.

For IPv6-capable applications, also add:

TEXT
:: facebook.com

Step-by-step: block a website

Windows

1
Open Notepad as administrator (see Windows guide)
2
Open C:\Windows\System32\drivers\etc\hosts
3
Add at the bottom:
TEXT
# Block Facebook, added 2026-06-11
0.0.0.0 facebook.com
0.0.0.0 www.facebook.com
0.0.0.0 m.facebook.com
0.0.0.0 web.facebook.com
4
Save (Ctrl+S)
5
Flush DNS:
CMD
ipconfig /flushdns

macOS

BASH
sudo nano /etc/hosts

Add blocking entries, save (Ctrl+O, Enter, Ctrl+X), then:

BASH
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder

See Mac guide.

Linux / Ubuntu

BASH
sudo nano /etc/hosts

Add entries, save, then:

BASH
sudo resolvectl flush-caches

See Linux guide · Ubuntu guide

Block multiple websites

Manual list

TEXT
# === Productivity blocks ===
0.0.0.0 twitter.com
0.0.0.0 www.twitter.com
0.0.0.0 x.com
0.0.0.0 www.x.com
0.0.0.0 reddit.com
0.0.0.0 www.reddit.com
0.0.0.0 old.reddit.com
0.0.0.0 instagram.com
0.0.0.0 www.instagram.com

Import a blocklist

Popular curated lists contain thousands of ad and tracker domains:

Steven Black's unified hosts file:

BASH
# macOS/Linux, back up first!
sudo cp /etc/hosts /etc/hosts.backup
curl -s https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts | sudo tee /etc/hosts

Windows (PowerShell as admin):

POWERSHELL
Copy-Item C:\Windows\System32\drivers\etc\hosts C:\Windows\System32\drivers\etc\hosts.backup
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts" -OutFile C:\Windows\System32\drivers\etc\hosts
ipconfig /flushdns

For ad-specific blocking see Block ads with the hosts file.

Important: block both www and non-www

A common mistake, blocking only facebook.com but not www.facebook.com:

TEXT
# Incomplete, www.facebook.com still works!
0.0.0.0 facebook.com

# Complete
0.0.0.0 facebook.com
0.0.0.0 www.facebook.com

Always block both variants. Some sites also use m., mobile., or api. subdomains.

Unblock a website

1
Open the hosts file as administrator
2
Find the blocking lines
3
Either delete them or comment out with #:
TEXT
# 0.0.0.0 facebook.com
# 0.0.0.0 www.facebook.com
4
Save and flush DNS cache

Verify blocking works

ping test

BASH
ping facebook.com

Expected on blocked domain:

TEXT
PING facebook.com (0.0.0.0): ...

Or "Request timed out" / "Destination host unreachable."

Browser test

Navigate to the blocked site, you should see:

  • "This site can't be reached" (Chrome)
  • "Unable to connect" (Firefox)
  • "Safari Can't Connect to the Server" (Safari)

nslookup

BASH
nslookup facebook.com

Should return 0.0.0.0 or 127.0.0.1.

Limitations of hosts file blocking

No wildcards

You cannot write:

TEXT
0.0.0.0 *.facebook.com # DOES NOT WORK

Each subdomain needs its own line. For wildcard blocking, use dnsmasq or Pi-hole.

Modern apps use many domains

YouTube, Netflix, and Spotify use dozens of CDN domains. Blocking the main domain often breaks functionality incompletely rather than blocking cleanly.

DNS-over-HTTPS bypass

Browsers with Secure DNS enabled may bypass the hosts file for some lookups:

  • Chrome: Settings → Privacy → Security → Use secure DNS → Off (for testing)
  • Firefox: Settings → Privacy → DNS over HTTPS → Off

VPN and custom DNS

If your system uses a VPN or custom DNS (1.1.1.1, 8.8.8.8) configured to bypass local resolution, hosts file blocking may not apply to all traffic. Test with ping first.

Mobile apps on the same machine

Most desktop apps respect the hosts file. Some sandboxed apps (macOS App Store apps with network entitlements) may behave differently.

Does not block other devices

Hosts file blocking is per machine. Your phone and tablet are unaffected. For network-wide blocking, use router-level DNS filtering or Pi-hole.

Blocking vs redirecting

Blocking (0.0.0.0) makes the site unreachable. Redirecting sends the domain to a different IP:

TEXT
# Redirect old-site.com to new server for testing
203.0.113.50 old-site.com

This is not blocking, it is routing. Useful for staging tests. See main guide.

Performance with large blocklists

Blocklists with 100,000+ entries:

  • Slightly increase file read time at boot (negligible on modern hardware)
  • May slow DNS lookup by microseconds per query
  • Can occasionally cause false positives (blocking legitimate CDN domains)

Start with a small manual list. Add curated lists incrementally. Keep a backup.

Best practices

  • Comment every block with reason and date
  • Back up before importing lists, sudo cp /etc/hosts /etc/hosts.backup
  • Block www AND non-www versions
  • Test after blocking, verify the site fails AND critical services still work
  • Review blocklists, Steven Black's list is maintained but may block domains you need
  • Use 0.0.0.0 not 127.0.0.1
  • Flush DNS after every change
  • Remove blocks when no longer needed, stale blocks cause confusing failures months later

Example: focus mode blocklist

TEXT
# === Focus mode, weekdays only (manual toggle) ===
# Created: 2026-06-11

0.0.0.0 twitter.com
0.0.0.0 www.twitter.com
0.0.0.0 x.com
0.0.0.0 www.x.com
0.0.0.0 reddit.com
0.0.0.0 www.reddit.com
0.0.0.0 old.reddit.com
0.0.0.0 news.ycombinator.com
0.0.0.0 instagram.com
0.0.0.0 www.instagram.com

Toggle focus mode by commenting/uncommenting the block and flushing DNS.

Blocking streaming and social platforms

Some platforms require blocking multiple related domains for effective blocking:

TEXT
# Netflix (partial, CDN domains also needed)
0.0.0.0 netflix.com
0.0.0.0 www.netflix.com

# TikTok
0.0.0.0 tiktok.com
0.0.0.0 www.tiktok.com
0.0.0.0 tiktokv.com

Complete blocking of streaming services is difficult because video delivery uses many dynamically named CDN endpoints. Hosts file blocking works best for simple websites with few domains, news sites, forums, and social networks with predictable hostname patterns.

Blocking websites on a work machine may violate employer acceptable use policies if it interferes with work-related access. Blocking on a shared family computer affects all users, communicate before deploying large blocklists. The hosts file is a local tool, not network surveillance; it does not log or report blocked attempts.

GUI management with Sleezr

Managing hundreds of blocking entries manually is tedious. Sleezr lets you enable/disable individual entries without deleting them, useful for toggling focus blocks. Automatic backup and DNS flush included.

---

*Last tested: Windows 11 24H2, macOS 15, Ubuntu 24.04, June 2026.*

Sources and further reading

Share this article

Frequently Asked Questions

How do I block a website using the hosts file?

Add a line redirecting the domain to 0.0.0.0 or 127.0.0.1, for example: 0.0.0.0 facebook.com. Save as administrator and flush DNS cache.

Should I use 0.0.0.0 or 127.0.0.1 to block websites?

Use 0.0.0.0, it fails instantly without attempting a connection. 127.0.0.1 works but may cause a brief delay as the system tries to connect to localhost.

Does blocking work in all browsers?

Yes, hosts file blocking is system-wide. It affects all browsers and most applications. Some apps with hardcoded DNS or DoH may bypass it.

Can I block YouTube with the hosts file?

Partially. You must block multiple domains (youtube.com, www.youtube.com, m.youtube.com, googlevideo.com, ytimg.com). YouTube uses many CDN domains, making complete blocking difficult.

How do I unblock a site I blocked by mistake?

Find the line in your hosts file, delete it or comment it out with #, save, and flush DNS cache.

Can I use wildcards to block all subdomains?

No. The hosts file does not support wildcards like *.facebook.com. Each subdomain must be listed on its own line.

Is hosts file blocking better than browser extensions?

Different strengths. Hosts file blocks system-wide (all apps). Extensions offer easier management and filter lists. Many people use both.

Related Articles

7 min read
hosts fileUbuntusystemd-resolved

How to Edit the Hosts File on Ubuntu (2026)

Edit /etc/hosts on Ubuntu 24.04 with nano, understand systemd-resolved interaction, flush DNS cache, and fix entries that systemd-resolved ignores.

S

Sleezr Team

7 min read
hosts filead blockingsecurity

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.

S

Sleezr Team

6 min read
hosts fileDNSlocal development

Hosts File: Location, Syntax and Uses (2026)

Learn what the hosts file does, where to find it on Mac, Windows and Linux, syntax examples, local dev uses and mistakes to avoid.

S

Sleezr Team