How to Edit the Hosts File on Mac: Block Sites and Test Local Domains

Mahesh Makvana
·
12 min read

Switching to Mac is reader-supported. We may earn a commission when you buy through links on our site. Learn more.

If you’ve stumbled on a forum post that says “add a line to your hosts file,” you may have wondered where that file lives on a Mac. Windows users know it from C:\Windows\System32\drivers\etc. Your Mac has one too, and it’s worth a look.

The hosts file is a small plain-text file that maps hostnames to IP addresses. macOS checks it before it asks a DNS server. One line in it can block a distracting site, point a domain at a dev server on your own machine, or override DNS while you troubleshoot.

The short version: open Terminal and run sudo nano /etc/hosts. Add a line like 127.0.0.1 example.com, then save with Control + O, exit with Control + X, and flush the DNS cache. The rest of this guide covers each step and the gotchas that make it seem broken when it isn’t.

Terminal window running nano with /etc/hosts open, showing the default localhost entries plus a new "127.0.0.1 mysite.test" line highlighted at the bottom, with nano's shortcut bar visible

What the Hosts File Is Good For

These are the jobs it handles well:

  • Blocking a domain on one Mac by pointing it at your own machine
  • Testing a website locally under a realistic name like shop.test
  • Previewing a site on a new server before you change its public DNS
  • Overriding DNS temporarily while you troubleshoot a connection

It’s a blunt tool. It knows hostnames and IP addresses and nothing else. That limit matters most for blocking, which I’ll cover below.

Where the Hosts File Lives on Mac

The file is at /etc/hosts. You’ll also see it written as /private/etc/hosts. Both point to the same file, because /etc on a Mac is a link into the system’s /private folder.

You can view it in Finder. Press Cmd + Shift + G (or choose Go > Go to Folder), type /etc/hosts, and press Return.

Finder with Go > Go to Folder dialog open and "/etc/hosts" typed into the path field, with the matching suggestion highlighted below it

Viewing it is fine. Editing it from Finder is a pain, because the file belongs to the system and TextEdit won’t save changes to it without extra permission juggling. macOS has no built-in graphical editor for the hosts file, and nothing in System Settings touches it. Terminal is the dependable route, and it takes about a minute.

Before You Start

You need an administrator account on the Mac. Standard accounts can’t save changes to /etc/hosts without an admin password.

If your Mac is managed by work or school, device-management software, security tools, or a VPN client may block your edits or quietly overwrite them. If your changes keep vanishing, that’s the likely culprit (more on that in Troubleshooting).

The steps below work the same way on every recent macOS release, from Monterey, Ventura, Sonoma, Sequoia, and Tahoe through the current macOS 27 Golden Gate. The file path, syntax, and commands don’t depend on your macOS version.

How to Edit the Hosts File on Mac

1. Open Terminal

Press Cmd + Space to open Spotlight, type Terminal, and press Return. You’ll also find it in Applications > Utilities > Terminal.

Spotlight search bar open on the macOS desktop with "Terminal" typed and the Terminal app highlighted as the top hit

2. Back up the file

Make a copy before you change anything:

sudo cp /etc/hosts /etc/hosts.backup

It takes two seconds and saves you from a broken hosts file later. (I’d do this even if you’re only adding one line.)

3. Open the hosts file in nano

Run:

sudo nano /etc/hosts

nano is a simple text editor built into Terminal. On macOS 12.3 and later, the nano command actually opens pico, a near-identical editor whose title bar reads “UW PICO,” and every shortcut in this guide works the same way. sudo runs it with administrator privileges, which you need in order to save changes. If you leave out sudo, nano still opens the file, but you’ll get a “Permission denied” error when you try to save.

Terminal window with the command "sudo nano /etc/hosts" typed at the prompt, cursor at the end of the line

4. Enter your password

Terminal asks for your Mac login password. As you type it, nothing appears on screen: no dots, no asterisks. That’s normal. Type it and press Return.

If you’ve been staring at a blank prompt wondering whether your keyboard died, don’t panic. It’s taking your keystrokes. This trips up nearly every Windows switcher the first time.

Terminal window showing the "Password:" prompt after running sudo nano /etc/hosts, with the cursor blinking and no visible characters

How to Read the Hosts File

A default Mac hosts file looks like this:

##
# 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

Lines that start with # are comments, and macOS ignores them. Each active line has two parts: an IP address on the left and a hostname on the right, separated by spaces or a tab.

nano editor in Terminal showing the default /etc/hosts contents, with the "127.0.0.1 localhost" line highlighted

Take 127.0.0.1 localhost. 127.0.0.1 is the loopback address, meaning your own Mac. When you type localhost into a browser, macOS reads this line and connects to your own machine. Every entry you add follows the same pattern. It’s a clever little system, and it has barely changed in decades.

Leave the three default entries alone. Removing them can break things that expect localhost to resolve.

Point a Domain at a Local Test Site

This is where the hosts file earns its keep for developers. Say you’re running a site on your Mac and want to reach it at mysite.test instead of localhost.

  1. In nano, use the arrow keys to move to the end of the last line, then press Return for a new line.
nano editor in Terminal with /etc/hosts open and the cursor on a new empty line below the "::1 localhost" entry
  1. Type the IP address you want the domain to point to. For a server on your own Mac, that’s 127.0.0.1. If the server runs on another machine on your network, use that machine’s address (for example, 192.168.1.25).
nano editor in Terminal showing "127.0.0.1" typed at the start of a new line below the default localhost entries
  1. Press Tab, then type the hostname. Here, that’s mysite.test.
nano editor in Terminal with the completed line "127.0.0.1 mysite.test" highlighted below the default localhost entries

The finished entry looks like this. A comment above it helps future-you remember why it’s there:

# Local testing for my site
127.0.0.1    mysite.test

Stick with the reserved .test ending for local projects instead of borrowing a real domain. If you map a real production domain to localhost and forget about it, you get confusing failures in browsers, APIs, and sign-in flows weeks later.

One caveat for HTTPS: the hosts file only handles name resolution. If your local site uses HTTPS, you still need a certificate that macOS and your browser trust for mysite.test. The hosts entry alone won’t make a certificate valid.

That same rule means the old trick of pointing facebook.com at another site’s IP address mostly fails today. Modern sites run on HTTPS, so your browser sees a certificate that doesn’t match and shows a warning instead of loading the other site.

Save the file and exit nano

  1. Press Control + O to write the file.
  2. Press Return to confirm the filename.
  3. Press Control + X to exit nano.

That’s the Control key, not Command. Pressing Cmd + O here does nothing useful. Switchers hit this constantly, so don’t feel bad if you did.

Flush the DNS cache

macOS caches DNS lookups, so it may keep using an old answer after you edit the file. Clear the cache with:

sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder

Or run both on one line:

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Your edit often takes effect without this step, but running it rules out a stale cached answer. Restarting mDNSResponder (the second command) is what clears the system resolver’s cache. You won’t see a success message, and that’s expected.

Terminal window showing "sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder" entered at the prompt with no output returned, and a fresh prompt below

Flushing the cache only clears stored lookups. If your entry has a typo, flushing won’t fix it.

Check that it worked

Confirm what’s in the file:

cat /etc/hosts

Then ask macOS how it resolves your hostname:

dscacheutil -q host -a name mysite.test

You should see 127.0.0.1 in the result. ping -c 1 mysite.test works too, and curl -I http://mysite.test shows whether your local server answers.

Each check covers a different layer. dscacheutil and ping confirm how the name resolves, while curl confirms that a server actually answers. If your local server uses another port or HTTPS, include it in the curl test (for example, curl -I http://mysite.test:8080).

If Terminal shows the right address but your browser doesn’t, the browser is holding on to its own cache or an open connection. Quit it completely with Cmd + Q and reopen it.

Find a site’s IP address

Sometimes you need the IP address for a domain, say, to preview a site on its new server. Terminal can look it up:

ping -c 1 wikipedia.org

Swap in whatever domain you need. The -c 1 sends a single ping and stops. Leave it off and ping runs until you press Control + C.

Terminal window with the command "ping -c 1 wikipedia.org" typed at the prompt

The IP address appears in parentheses on the first line of the output.

Terminal window showing ping output for wikipedia.org with the resolved IP address in parentheses on the first line highlighted

Large sites often use several addresses that change over time, so treat what you see as a snapshot.

Block Websites by Editing the Hosts File on Mac

This is the use most readers come for. The hosts file lets you block sites without installing any third-party apps, and for keeping yourself off one or two distracting sites, it works.

  1. Open the hosts file with sudo nano /etc/hosts.
  2. Move to the end of the last line and press Return.
  3. Type 127.0.0.1, press Tab, and type the domain you want to block, such as instagram.com.
  4. Add a second line for the www version. Blocking instagram.com does not block www.instagram.com.
127.0.0.1    instagram.com
127.0.0.1    www.instagram.com
nano editor in Terminal with /etc/hosts open showing two new lines, "127.0.0.1 instagram.com" and "127.0.0.1 www.instagram.com", highlighted below the default entries
  1. Press Control + O, then Return to save.
  2. Press Control + X to exit.
  3. Flush the DNS cache:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Terminal window after exiting nano, showing the DNS flush command "sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder" at the prompt with no output

Now when your Mac looks up the blocked domain, it gets your own machine back and the page fails to load.

If you also look after a Windows computer, our video walks through the same trick there: How to Block Websites on Windows: Using the Host File.

What hosts-file blocking can’t do

It’s a nice trick for self-control. It’s a poor choice for parental controls or security. Keep these limits in mind:

  • You must list every hostname. The file has no wildcards, so *.example.com won’t work. Big services use many domains and subdomains, and you’d have to find and list each one.
  • IP addresses bypass it. If something connects by IP address directly, the hosts file never comes into play.
  • Open connections survive. An app that’s already connected may keep working until you quit it.
  • It can’t filter content. It has no idea about URLs, paths, ads, or categories.
  • IPv6 can slip past. If an app connects over IPv6, you may need matching ::1 lines (for example, ::1 instagram.com). Test before you rely on it.
  • Other routes can skip it. It only covers this Mac, so other devices on your network aren’t affected. A VPN, a proxy, or a browser set to use its own secure DNS may also resolve names in ways that bypass your entries, so test in the browser you actually use.
  • 127.0.0.1 can mean slow failures when nothing on your Mac is listening. Some people use 0.0.0.0 instead, but apps handle that differently, so it isn’t automatically better.

Disable or Remove an Entry

To turn an entry off without deleting it, put # at the start of the line:

# 127.0.0.1    instagram.com

Save, exit, and flush the DNS cache again. Delete the # later to turn the block back on. It’s a neat way to keep a “focus mode” ready.

Troubleshooting

“Permission denied” when saving

You opened the file without sudo. Exit nano with Control + X, choose not to save, and run sudo nano /etc/hosts instead. If your account isn’t an administrator, you’ll need an admin’s password.

Your password “doesn’t work”

Terminal hides password characters entirely. Type it carefully and press Return. If it fails three times, run the command again.

The change does nothing

Work through this list in order:

  1. Make sure the line doesn’t start with #.
  2. Check the spelling of the IP address and hostname.
  3. Confirm the IP address and hostname are on the same line.
  4. Flush the DNS cache again.
  5. Quit and reopen your browser.
  6. Test with dscacheutil -q host -a name yourdomain to see what macOS actually returns.
  7. Check whether the site uses a different subdomain than the one you listed.

macOS ignores your entry

The format is strict. The IP address comes first, then the hostname, with no protocols, ports, or paths.

127.0.0.1    example.com

All of these fail:

example.com    127.0.0.1
127.0.0.1:80   example.com
http://example.com    127.0.0.1

Your changes keep disappearing

Something is rewriting the file. Common causes are device-management software, endpoint-security tools, VPN clients, or a developer utility that regenerates the hosts file. Check the file after a restart and after you connect to a work VPN to narrow it down. And make sure you edited /etc/hosts itself and not a copy.

You broke the file

Restore your backup:

sudo cp /etc/hosts.backup /etc/hosts
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder

No backup? Reopen the file with sudo nano /etc/hosts and retype the three default entries shown earlier. Don’t paste in a “clean” hosts file from a random website.

Permissions look odd

Run ls -l /etc/hosts to check them. Don’t change permissions unless something is clearly wrong. A file that anyone can write to is a security risk, and if a tool changed the permissions, find out why before you fix them.

When Another Tool Is the Better Pick

The hosts file is perfect for one Mac and a handful of domains. Past that, you’ll want something else.

For blocking across a household, filter at the network level. Many routers have DNS filtering built in, a managed DNS filtering service covers every device on your network, and a Pi-hole on a Raspberry Pi blocks domains for every device with full wildcard support. For a child’s account on a Mac, Screen Time in System Settings gives you limits and content restrictions that a text file can’t.

For local development with several projects, a local DNS service like dnsmasq or a reverse proxy like Caddy, nginx, or Traefik handles wildcard domains, routing, and HTTPS certificates for you. Plenty of development tools also set up local domains automatically. For one quick mapping, though, editing /etc/hosts is still the fastest way.

Wrapping Up

The hosts file is one of those Mac gems that hides in plain sight: one text file, one command, and you control how your Mac resolves any hostname. Back it up first, remember that nano uses Control instead of Command, and flush the DNS cache if a change doesn’t show up right away.