Block a Website by Editing /etc/hosts

Point a domain to 127.0.0.1 so every browser on this Mac fails to reach it.

macOSnetworkDNShosts-fileMedium risk
Run `printf "127.0.0.1 www.distracting-site.com\n" | sudo tee -a /etc/hosts && sudo dscacheutil -flushcache` to block the site immediately.

01Command

Command
$ printf "127.0.0.1 www.distracting-site.com\n" | sudo tee -a /etc/hosts && sudo dscacheutil -flushcache

02What it does

The hosts file is checked before DNS, so mapping a domain to 127.0.0.1 makes it unreachable in every browser and app — a simple, ad-blocker-free way to cut distractions. Flush the lookup cache afterwards so the change takes effect without logging out. Block each hostname variant you actually use (with and without www) on its own line.

03Risk

RiskMedium risk
RequirementsAdministrator access (sudo)
!Warning: Editing /etc/hosts with the wrong syntax can break name resolution — always keep a backup copy and revert with the undo command if anything misbehaves.

04Undo

Undo
$ sudo sed -i '' '/www\.distracting-site\.com/d' /etc/hosts && sudo dscacheutil -flushcache

05Sources

Last verified: 2026-09-18 · Category: Network