How to edit the hosts file on a Mac, and where it falls short
In short: In Terminal, type sudo nano /etc/hosts, enter your Mac's password, add a line such as 0.0.0.0 example.com at the end, save with Control-O and Return, and quit with Control-X. Then run sudo killall -HUP mDNSResponder. Every app on the Mac now gets nowhere when it looks up that name. To undo it, delete the line the same way.
The hosts file is a plain text file at /etc/hosts that pairs names with addresses. macOS reads it when an app looks up a name: according to its manual page, the file "is read by mDNSResponder" and used to answer lookups alongside DNS (man hosts). Write a name there with an address of your choosing, and every app on the Mac gets that address for that name.
People use it for two things: to block a site, by pointing it at an address that goes nowhere, and to test a website on a new server before the world sees it, by pointing its name at that server.
Step by step
- Open Terminal, from Applications › Utilities, or by searching for it with Spotlight.
- Make a backup, so you can always go back:
sudo cp /etc/hosts /etc/hosts.backupType your Mac's login password when asked. Nothing appears on screen as you type; that's normal.
- Open the file:
sudo nano /etc/hosts - Move to the end with the arrow keys and add one line per name:
0.0.0.0 example.com 0.0.0.0 www.example.com0.0.0.0is an address that leads nowhere.127.0.0.1, your own Mac, also works for blocking. - Save and quit. Press Control-O, then Return to confirm the name, then Control-X.
- Clear the cache, so the Mac forgets addresses it already looked up (Apple):
sudo killall -HUP mDNSResponder - Quit and reopen your browser, which may also remember the old address for a while.
Leave the lines that are already in the file alone. The file itself warns that the localhost entry is used when the Mac starts up.
How to undo it
Open the file the same way and delete your lines, or put the backup back:
sudo cp /etc/hosts.backup /etc/hosts
Then clear the cache again with sudo killall -HUP mDNSResponder.
Where it falls short
One line per name. The file has no wildcards. Blocking example.com doesn't touch www.example.com, m.example.com or video.example.com, and large sites often use many names. You add each one by hand.
It affects the whole Mac. Every app and every user on the Mac gets the same answer. You can't block a site in one browser only, or for one person.
No timer, no record. A line stays until you remove it, and nothing tells you when an app tried to reach a blocked name.
It only works on names. An app that connects straight to an IP address, without looking up a name, never consults the file.
Mistakes happen in Terminal. A typo, a missing space or an edited localhost line can cause problems that are hard to trace later. Keep the backup.
What to use instead
For blocking sites in Safari for a child, Screen Time is easier and needs no Terminal; our guide to blocking a website on a Mac compares the options.
For blocking across all apps without the limits above, an outgoing firewall does the same job with more care. In Ranger, a rule for A domain and its subdomains covers every subdomain at once, for all apps or one app, until removed or for an hour or a day, and each blocked attempt appears in Traffic. If you already have a list in hosts format, Ranger can use it as it is: in Blocklists › Add List…, a list can be one domain per line, a hosts file, or simple Adblock rules, and a list from a web address is refreshed once a day.
The hosts file is still worth knowing: it's free, it's already on every Mac, and for one or two names it's quick.