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
- 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.
- 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" sohostsshows up (it has no extension). - 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
- Save the file. Because you're saving into a protected system directory, Notepad will only succeed if it was launched elevated in step 1.
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.localat127.0.0.1to 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
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.
Discussion & Insights