How to Block Websites on Mac Using Terminal (Hosts File) — 2026
The hosts file is the oldest free way to block a website on a Mac, and it still works in 2026. No app, no extension, no account — just one line in a system file and the site stops loading in every browser. This guide shows you exactly how to block websites on Mac using Terminal, step by step, then explains the real limits of the method so you know when to reach for something stronger.
The fast answer
To block a website on Mac using Terminal, open Terminal and run sudo nano /etc/hosts, enter your password, and add a line like 127.0.0.1 www.example.com below the existing entries. Save with Control-O, exit with Control-X, then run sudo dscacheutil -flushcache to clear the DNS cache. The site now fails to load in Safari, Chrome, Firefox, and every other browser on the Mac. It's free and system-wide — but it's also easy to undo, so it's a speed bump rather than a locked door.
How to block a website on Mac using Terminal, step by step
-
Open Terminal (Applications → Utilities, or search with Spotlight).
-
Back up the file first:
sudo cp /etc/hosts /etc/hosts.backup. Enter your password when asked. -
Open the hosts file for editing:
sudo nano /etc/hosts. -
Move the cursor below the existing lines and add one line per site:
127.0.0.1 www.youtube.com 127.0.0.1 youtube.com 127.0.0.1 www.reddit.com 127.0.0.1 reddit.com -
Save with Control-O then Enter, and exit with Control-X.
-
Clear the DNS cache so the change takes effect:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder.
That's it. Open the blocked site in any browser and it fails to connect. Add both the www and non-www version of every domain — the hosts file matches the exact hostname, so blocking reddit.com alone won't stop www.reddit.com.
Why the hosts file blocks every browser at once
The reason this method beats a browser extension on coverage is where it sits. The hosts file is checked before your Mac asks a DNS server for an address, so when you point a domain at 127.0.0.1 (your own machine), the request goes nowhere. That happens at the system level, beneath every browser, which is why Safari, Chrome, Firefox, and Arc all hit the same wall without you installing anything in each one.
A Chrome extension, by contrast, only controls Chrome. The moment you open Safari, its block is gone. So for pure cross-browser coverage at zero cost, the hosts file genuinely wins — the catch is everything else, which is the next section.
Where the Terminal method falls short
The hosts file is blunt. It's worth knowing its limits before you rely on it:
- It's trivial to undo. Anyone with admin access can reopen
/etc/hostsand delete the line in seconds. There's no timer, password, or session — nothing stops a moment of weakness. - No scheduling. You can't block a site only during work hours or only on weekdays. It's on until you manually remove the line.
- Exact hostnames only. You must list every subdomain. Block
youtube.comandwww.youtube.com, or one of them slips through. - It doesn't reliably block apps. Many native apps connect by IP or use their own resolver, ignoring the hosts file entirely.
- DNS-over-HTTPS can route around it. A browser set to use its own secure DNS resolver can bypass the hosts file completely.
None of that makes the method useless. For stopping autopilot visits — the reflexive youtube.com you type without deciding to — a hosts file block is often enough, because the friction of editing a system file is more than the reflex can be bothered with. It's the deliberate, determined bypass it can't stop.
Hosts file vs a blocking app, compared
| Feature | Hosts file (Terminal) | Focuh Mac app | SelfControl |
|---|---|---|---|
| Free | Yes | Yes | Yes |
| Blocks all browsers | Yes | Yes | Yes |
| Blocks native apps | No | Partially | Network-level |
| Scheduling / sessions | No | Yes | Timed block |
| Hard to undo | No | Medium | High |
| Needs Terminal | Yes | No | No |
| Attempt counter | No | Yes | No |
If the hosts file's "easy to undo" row is the dealbreaker for you, that's the signal to use an app. For a fuller comparison of Mac options, see the best free app blocker for Mac, and for the general approach, how to block websites on Mac covers every method side by side.
How to unblock a site you blocked in Terminal
Reversing it is the same process. Run sudo nano /etc/hosts, delete the lines you added, save with Control-O and Control-X, then flush the cache again with sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder. If you made the backup in step 2, you can also restore it wholesale with sudo cp /etc/hosts.backup /etc/hosts and flush. The site loads again immediately. This ease of reversal is exactly why the method is honest as a habit-breaker but weak as a lockdown.
When to use an app instead
Reach for a dedicated blocker when the manual method's weaknesses are the things you actually need solved. If you want a block you can't cancel in a weak moment, SelfControl sets a timer that can't be lifted until it runs out. If you want blocking tied to focus sessions, an attempt counter, and no system-file editing, the free Focuh Mac app handles it through a normal interface and can block apps as well as sites during a session. And if your distraction lives in Chrome and you also want a browser layer, the free Focuh Chrome extension covers that.
The hosts file and an app aren't mutually exclusive. A common setup is a hosts file as a permanent baseline for the handful of sites you never want, plus an app for time-boxed focus sessions where you want stronger, harder-to-cancel blocking. Why OS-level blocking beats the browser-only kind is covered in system-level vs browser website blocking.
Which approach should you pick?
- You want a free, no-install block and you're fine in Terminal — use the hosts file method above for your permanent blocklist.
- You want scheduling, sessions, or app blocking without editing system files — use the free Focuh Mac app.
- You want a block you can't undo in a weak moment — use SelfControl's timed block.
- Your distraction is mostly in Chrome — add the free Focuh Chrome extension on top.
The hosts file is the right first tool for honest, autopilot habits — free, system-wide, and already on your Mac. When you need scheduling or a block you can't cancel, that's where an app earns its place. Get the free Focuh Mac app when the manual method isn't enough on its own.