LightAtom

How to Edit the Hosts File on Mac

The hosts file maps names to addresses before DNS is asked. Edit it to test a site on a new server, use local development domains or block a domain.

sudo nano /etc/hosts

Open the hosts file in the nano editor with admin rights.

beginner5 minNeeds: Terminal, Administrator password

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

  1. Back it up

    Keep a copy so you can restore it.
    sudo cp /etc/hosts /etc/hosts.backup
  2. Open it

    nano is a simple editor in Terminal. Use the arrow keys to move.
    sudo nano /etc/hosts
  3. Add a line

    Format: IP address, whitespace, then one or more names. Lines starting with # are comments. Don't change the default localhost lines.
  4. Save and exit

    Press Ctrl+O, Return to save, then Ctrl+X to exit.
  5. Flush the DNS cache

    Apps may have the old answer cached.
    sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
  6. Check 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

FlagMeaningExample
IP NAME [NAME…]Map one or more names to an address127.0.0.1 api.test admin.test
# commentIgnored text# staging server
0.0.0.0 NAMEBlock a name (connections fail fast)0.0.0.0 tracker.example

Common errors

ErrorCauseFix
Change doesn't apply in the browserBrowser or system DNS cacheFlush DNS; in Chrome clear chrome://net-internals/#dns.
Works in Safari, not in a VPNSome VPN and DNS-filter apps handle DNS themselvesDisconnect the VPN to test.
Permission denied when savingOpened without sudoReopen with sudo nano /etc/hosts.
Subdomains aren't coveredThe hosts file has no wildcardsList 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.

LightAtom team

The people who build LightAtom, a network monitor for macOS.