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.


Option 1: Terminal + sudo nano
Free

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
Pros
  • No software needed
  • Full control over every line
Cons
  • Easy to break your internet
  • No undo or backup
  • Must remember to flush DNS
  • Intimidating for non-technical users
Option 2: Text Editor with Admin Privileges
Free

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.

Pros
  • Familiar editor interface
  • Syntax highlighting helps
Cons
  • Still no undo for hosts changes
  • No profiles or quick toggle
  • Must manually flush DNS
Option 3: The Sane Way
$6.99 One-Time

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.

Why it wins
  • 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
Get SaneHosts — $6.99

Compatible with macOS 14 (Sonoma) and later.