☕ Buy a Coffee
Home / IT Support & Troubleshooting

How to Block a Website via the 'Hosts' File

Create system-wide domain redirects to 127.0.0.1 to block distracting websites and telemetry domains without browser extensions.

Sachin Siju
Sachin Siju
Lead Systems Engineer & Tech Blogger
Jul 17, 2026 3 min read
How to Block a Website via the 'Hosts' File

What the Hosts File Actually Does

Before your computer ever asks a DNS server "what's the IP address for this domain," it checks a local text file called the hosts file. This is a holdover from before DNS existed, and every major OS still honors it. Any entry you add there overrides DNS resolution completely for that domain, on that machine, for every browser and app — no extension required. Point a domain at 127.0.0.1 (your own machine's loopback address) and the OS will try to connect to your local computer instead of the real site, which fails instantly and effectively blocks it.

This is a blunt instrument compared to browser extensions or router-level DNS filtering, but it has real advantages: it works system-wide across every browser, it can't be disabled by clearing browser data, and it blocks telemetry/tracking domains for apps that don't even have a UI to configure that.

Locating the Hosts File

On Windows, the file lives at:

C:\Windows\System32\drivers\etc\hosts

On macOS and Linux, it's at:

/etc/hosts

It has no file extension — it's a plain text file that just happens to be named hosts.

Editing the Hosts File on Windows

  1. Open Notepad as administrator (right-click Notepad in the Start menu, choose "Run as administrator"). You cannot save changes to this file without elevated permissions.
  2. In Notepad, go to File > Open, navigate to C:\Windows\System32\drivers\etc\, and change the file type filter from "Text Documents" to "All Files" so hosts shows up (it has no extension).
  3. Open it, scroll to the bottom, and add a new line for each site you want to block:
127.0.0.1 www.example.com
127.0.0.1 example.com
  1. Save the file. Because you're saving into a protected system directory, Notepad will only succeed if it was launched elevated in step 1.
Tip: Always add both the bare domain (example.com) and the www subdomain as separate lines. Blocking one doesn't automatically block the other, and sites often redirect between them.

Flushing the DNS Cache

Windows caches DNS lookups, so a site you visited recently might still resolve to its real IP for a few minutes even after editing the hosts file. Force an immediate refresh from an elevated Command Prompt or PowerShell window:

ipconfig /flushdns

Then close and reopen your browser to be safe — some browsers (Chrome in particular) keep their own internal DNS cache separate from the OS. You can clear Chrome's by visiting chrome://net-internals/#dns and clicking "Clear host cache."

Common Uses

  • Blocking distracting sites — social media, news sites, or anything else you want off-limits during focus time.
  • Blocking telemetry and ad domains — many privacy-focused hosts file projects (like StevenBlack's hosts) maintain long lists of known tracking and ad-serving domains you can append in bulk.
  • Local development — the same mechanism, used the opposite way, lets you point a domain like myapp.local at 127.0.0.1 to test a local web server under a friendly hostname.

Removing or Disabling a Block

Delete the line, or comment it out by putting a # at the start of it, then save and flush DNS again:

#127.0.0.1 www.example.com

Limitations and Caveats

Limitations: This only blocks the exact hostnames you list — it does nothing against other subdomains, alternate domains the same service uses, or apps that hardcode IP addresses instead of hostnames. It's also easy to circumvent for a determined user (or yourself, five minutes later) since editing the file back is trivial and requires no special software. For serious parental controls or productivity enforcement, pair this with router-level DNS filtering or a dedicated blocking tool.

Also worth knowing: some antivirus and security software monitors the hosts file for unauthorized changes, since malware has historically abused it to hijack sites like banking domains. Don't be surprised if your AV flags or prompts you when you edit it manually — that's expected behavior, not a sign anything is wrong.

Featured Infrastructure Partner

Deploy on High-Performance Hostinger Cloud

Get up to 75% OFF + free domain & SSL. Powering xube.me's sub-second response times.

Claim Discount ↗

Discussion & Insights

Related Technical Essays