TechBuzzPro

How to Edit Your Mac's Hosts File (and Why You'd Want To)

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Editing your Mac's hosts file lets you manually override domain name resolution, mapping hostnames to specific IP addresses before any DNS lookup happens. This is useful for blocking websites, redirecting domains during development, or testing a new server without changing DNS settings. You'll need administrator access to your Mac and a text editor that can handle plain text files.

What You'll Need

  • Administrator (admin) password for your Mac
  • Terminal app (found in /Applications/Utilities/)
  • A text editor – TextEdit works, but a dedicated code editor like Visual Studio Code, Sublime Text, or nano (built into Terminal) is easier.

Step-by-Step

  1. Back up your hosts file. Before making changes, create a backup so you can restore the original if something goes wrong. Open Terminal and run:
    sudo cp /etc/hosts /etc/hosts.backup
    You'll be prompted for your admin password. This creates a copy named hosts.backup in the same folder.
  2. Open the hosts file in a text editor. The easiest way is to use nano, a simple terminal-based editor. In Terminal, run:
    sudo nano /etc/hosts
    Alternatively, you can use a GUI editor like TextEdit, but you must first unlock the file (see Tips).
  3. Understand the file structure. The hosts file contains lines with an IP address followed by one or more hostnames, separated by spaces or tabs. Lines starting with # are comments and are ignored. The default file has several comments and a couple of active lines (like 127.0.0.1 localhost).
  4. Add your custom entries. Move the cursor to the end of the file (or anywhere you like) and type a new line. For each entry, use the format:
    IP_address hostname
    For example, to block Facebook, add:
    127.0.0.1 facebook.com
    To redirect a domain to a specific server, use that server's IP:
    192.168.1.10 myserver.local
  5. Save and exit. In nano, press Control+O (the letter O) to save, then Control+X to exit. If you used a GUI editor, save the file normally.
  6. Flush the DNS cache. For the changes to take effect immediately, clear the system's DNS cache. In Terminal, run the appropriate command for your macOS version:
    For macOS Ventura (13) and later:
    sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
    For older versions (Monterey, Big Sur, Catalina):
    sudo killall -HUP mDNSResponder
  7. Verify your changes. Test that the new mapping works by pinging the hostname or trying to access it in a browser. For example:
    ping facebook.com
    If it resolves to 127.0.0.1, the block is working.

Common Mistakes

  • Forgetting to use sudo. The hosts file is protected; you must edit it with administrator privileges. Without sudo, you'll get a permission denied error.
  • Using the wrong IP format. Always use the IP address first, then the hostname. Reversing the order will break the entry.
  • Adding extra spaces or tabs incorrectly. While multiple spaces/tabs are allowed, make sure there's at least one between the IP and hostname. A missing separator will cause the line to be ignored.
  • Editing with a rich text editor. TextEdit defaults to rich text, which adds formatting characters. Use plain text mode (Format > Make Plain Text) or a code editor.
  • Not flushing DNS. If you don't flush the cache, the old DNS entries may persist for a while, making it seem like your edit didn't work.
  • Blocking only the base domain. If you block facebook.com, subdomains like www.facebook.com or m.facebook.com may still load. Add entries for each subdomain you want to block.

Tips

  • Use a dedicated editor. Programs like Visual Studio Code or Sublime Text handle plain text well and show line numbers, making it easier to spot errors.
  • Keep a backup. Always maintain a backup copy (e.g., hosts.backup) so you can restore the file if you mess up.
  • Comment out entries to disable them. Instead of deleting a line, add a # at the beginning to disable it. This makes it easy to re-enable later.
  • Use 127.0.0.1 for blocking. Redirecting unwanted sites to 127.0.0.1 (localhost) effectively blocks them because your own computer refuses the connection.
  • Test with multiple browsers. Some browsers use their own DNS (like DNS over HTTPS). If a block doesn't work in Chrome but works in Safari, the browser is bypassing the hosts file.
  • Be careful with wildcards. The hosts file does not support wildcards like *.example.com. You must list each subdomain individually.

Troubleshooting

Changes don't take effect. First, flush the DNS cache again. If it still doesn't work, check that you saved the file correctly and that the syntax is right. Also, verify the IP address is correct and reachable. If you're blocking a site, try using ping to see what IP it resolves to.

Permission denied when saving. This means you didn't use sudo or your editor doesn't have write access. Always edit with sudo nano or unlock the file first (see Tips).

Hosts file is read-only. In newer macOS versions, the file may have extended attributes that prevent editing. Run sudo chmod 644 /etc/hosts to make it writable, then edit.

Accidentally deleted the file or corrupted it. Restore from your backup: sudo cp /etc/hosts.backup /etc/hosts. If you didn't make a backup, you can recreate the default file (see Apple's support page).

Browser still shows the old site. This could be due to browser caching or DNS over HTTPS. Try a different browser, clear your browser cache, or disable DNS over HTTPS temporarily.

Frequently Asked Questions

Is editing the hosts file safe?

Yes, as long as you're careful. The hosts file is a standard system file, and editing it is a common practice. Just make a backup first and only add entries you understand.

Can I use the hosts file to block ads?

Yes, you can add entries for known ad servers and redirect them to 127.0.0.1. However, there are easier tools like ad-blockers that do this automatically.

Will editing the hosts file speed up my internet?

Not usually. The hosts file doesn't speed up DNS lookups in a meaningful way. It's more for overriding specific domains, not for performance.

How do I undo changes to the hosts file?

Simply remove the lines you added, or restore the backup you made. Then flush the DNS cache again.

Does the hosts file affect all users on the Mac?

Yes, the hosts file is system-wide, so changes apply to all user accounts on that Mac.

Can I use the hosts file to redirect a domain to another server?

Yes, that's a common use case. For example, during development you can point example.com to your local machine's IP (127.0.0.1) or a staging server.