How to Edit the Hosts File on Mac (3 Methods Compared)
The hosts file on macOS lives at /etc/hosts. It maps domain names to IP addresses before your Mac even contacts a DNS server. By adding entries like 0.0.0.0 ads.example.com, you can block any domain at the system level.
Editing it wrong can break your internet. Here are three ways to do it, from risky to safe.
The classic method. Open Terminal and edit the file directly with admin privileges.
sudo nano /etc/hosts
Add your entries at the bottom, press Ctrl+O to save, Ctrl+X to exit, then flush DNS:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
- ✓ No software needed
- ✓ Full control over every line
- ✗ Easy to break your internet
- ✗ No undo or backup
- ✗ Must remember to flush DNS
- ✗ Intimidating for non-technical users
Use a GUI text editor like BBEdit or VS Code to open /etc/hosts. Some editors can save with admin privileges when prompted. Slightly friendlier than the terminal, but you still manage entries manually.
- ✓ Familiar editor interface
- ✓ Syntax highlighting helps
- ✗ Still no undo for hosts changes
- ✗ No profiles or quick toggle
- ✗ Must manually flush DNS
SaneHosts is a native Mac app built specifically for managing your hosts file. Create profiles, import blocklists, activate with one click. If something breaks, just deactivate.
- ✓ Visual Editor: See and search all entries in a native GUI
- ✓ Profiles: Save multiple configurations and switch between them
- ✓ Auto DNS Flush: DNS cache cleared automatically on every change
- ✓ Crash Resilient: Automatic backups with one-click restore
Compatible with macOS 14 (Sonoma) and later.