sudo nano /etc/hostsOpen the hosts file in the nano editor with admin rights.
When to use it
Use the hosts file to preview a website on a new server before switching DNS, to give local development projects names like myapp.test, or to block a domain on this Mac only.
Steps
Back it up
Keep a copy so you can restore it.sudo cp /etc/hosts /etc/hosts.backupOpen it
nano is a simple editor in Terminal. Use the arrow keys to move.sudo nano /etc/hostsAdd a line
Format: IP address, whitespace, then one or more names. Lines starting with # are comments. Don't change the default localhost lines.Save and exit
Press Ctrl+O, Return to save, then Ctrl+X to exit.Flush the DNS cache
Apps may have the old answer cached.sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponderCheck it
dscacheutil resolves the way apps do, so it shows whether your entry applies.dscacheutil -q host -a name myapp.test
Output explained
The default macOS hosts file:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Example entries you might add:
203.0.113.10 example.com www.example.com # preview the new server
127.0.0.1 myapp.test # local development
0.0.0.0 ads.example.net # block a domain
Use .test (reserved by RFC 6761) for local names rather than .dev, which is a real top-level domain that browsers force to HTTPS.
Options and variations
| Flag | Meaning | Example |
|---|---|---|
| IP NAME [NAME…] | Map one or more names to an address | 127.0.0.1 api.test admin.test |
| # comment | Ignored text | # staging server |
| 0.0.0.0 NAME | Block a name (connections fail fast) | 0.0.0.0 tracker.example |
Common errors
| Error | Cause | Fix |
|---|---|---|
| Change doesn't apply in the browser | Browser or system DNS cache | Flush DNS; in Chrome clear chrome://net-internals/#dns. |
| Works in Safari, not in a VPN | Some VPN and DNS-filter apps handle DNS themselves | Disconnect the VPN to test. |
| Permission denied when saving | Opened without sudo | Reopen with sudo nano /etc/hosts. |
| Subdomains aren't covered | The hosts file has no wildcards | List each name, or run a local DNS server like dnsmasq. |
Do it in the app
LightAtom's connections view shows which IP an app actually connects to, so you can confirm a hosts entry took effect.
Frequently asked questions
Where is the hosts file on a Mac?
At /etc/hosts (which points to /private/etc/hosts). Open it with sudo nano /etc/hosts in Terminal.
Do I need to restart after editing the hosts file?
No. Flush the DNS cache with sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder, and restart the browser if it keeps an old connection.
Can the hosts file block websites?
Yes, point the domain to 0.0.0.0. It only affects this Mac, has no wildcards, and apps using their own DNS (DoH) or a VPN can bypass it.
Does Chrome use the hosts file?
Yes. Chrome reads the system hosts file even with secure DNS on. Clear its host cache after editing.