How to Edit Hosts File on Mac: Complete Guide
In this article
Editing the hosts file on a Mac lets you override DNS for specific domains, which is useful for blocking websites, testing local servers, or redirecting traffic during development. This guide covers everything from locating the file to applying changes, with common pitfalls and troubleshooting tips.
What You'll Need
- A Mac running macOS (any recent version, from Catalina to Sonoma)
- Administrator (admin) password for your user account
- Access to Terminal (found in /Applications/Utilities/)
- A text editor – TextEdit works, but a plain-text editor like BBEdit or Visual Studio Code is easier
Step-by-Step
- Open Terminal. Go to Finder > Applications > Utilities > Terminal, or search for “Terminal” in Spotlight (Cmd+Space).
- Back up the current hosts file. Run this command to make a copy with a timestamp:
sudo cp /etc/hosts /etc/hosts.backupEnter your admin password when prompted (you won’t see characters as you type). - Open the hosts file in a text editor. In Terminal, run:
sudo nano /etc/hostsThis opens the file in the nano editor with admin privileges. Alternatively, usesudo vi /etc/hostsfor vim, or open it in a GUI editor withsudo open -a TextEdit /etc/hosts. - Understand the file structure. The file contains lines with an IP address, whitespace, and a hostname. Lines starting with
#are comments. The default file has several comments and two active lines:127.0.0.1 localhostand::1 localhost. - Add your entries. To block a site, add a line like:
127.0.0.1 example.com
To redirect a domain to a local server, use:127.0.0.1 myproject.local
For a specific IP (e.g., a staging server), use that IP instead of 127.0.0.1. - Save and exit. In nano, press Ctrl+O to save, then Enter to confirm the filename, then Ctrl+X to exit. In vim, press Esc, type
:wq, and press Enter. - Flush the DNS cache. The change takes effect immediately for most commands, but browsers may cache DNS. To flush the system cache, run:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponderThis is the standard command for macOS Catalina and later. For older versions (Mojave and earlier), usesudo killall -HUP mDNSResponderalone. - Verify the change. Test with
ping example.comorcurl -I example.com. If you redirected to 127.0.0.1, the response should come from your local machine. You can also open a browser and go to the domain.
Common Mistakes
- Using the wrong IP format: For IPv4, use 127.0.0.1; for IPv6, use ::1. Mixing them up can cause failures.
- Forgetting to flush DNS: Even after saving, your Mac may still use cached DNS. Always run the flush command.
- Editing with the wrong permissions: If you open the file without
sudo, you can’t save changes. Always usesudoin Terminal. - Adding extra spaces or tabs: The format is IP followed by whitespace (space or tab) then hostname. Extra characters or missing whitespace can break the entry.
- Blocking the wrong domain: For subdomains, you need separate entries. Blocking
example.comdoes not blockwww.example.com. - Not backing up: If you mess up the file, you might break network connectivity. Always back up first.
Tips
- Use a GUI editor if you prefer: After running
sudo open -a TextEdit /etc/hosts, you can edit visually. Make sure TextEdit is in plain-text mode (Format > Make Plain Text). - Group entries with comments: Add lines like
# Block social mediato keep things organized. - Test with multiple domains: If you’re blocking a site that uses many subdomains, add entries for each, e.g.,
facebook.com,www.facebook.com,m.facebook.com. - Use a hosts file manager app: Tools like Gas Mask or iHosts provide a GUI and can toggle entries, but manual editing is fine for occasional use.
- Remember that hosts file overrides DNS: If you remove an entry, the domain will resolve normally again after flushing DNS.
Troubleshooting
- Changes don’t take effect: First, flush DNS again. If it still doesn’t work, check that the file has no syntax errors – open it and verify each line has an IP and a hostname. Also, check if the browser has its own DNS cache (try a different browser or incognito mode).
- “Permission denied” when saving: You must use
sudoto edit. In nano, if you didn’t use sudo, exit without saving and reopen withsudo nano /etc/hosts. - Terminal says “command not found”: Make sure you’re typing the commands exactly. The flush command uses semicolons; if you’re on an older macOS, use only the
killallpart. - Can’t access any websites after editing: You likely broke the file. Restore the backup:
sudo cp /etc/hosts.backup /etc/hostsand then flush DNS. - Entry works for ping but not browser: Browsers may have their own DNS cache or use DNS-over-HTTPS (DoH). Disable DoH in browser settings or try a different browser.
- Hosts file gets overwritten: Some software or macOS updates may reset it. Keep a backup and reapply after major updates.
Frequently Asked Questions
Do I need to restart my Mac after editing the hosts file?
No. Flushing the DNS cache is enough. A restart works too, but it’s unnecessary.
Can I block ads with the hosts file?
Yes, by adding entries that point ad servers to 127.0.0.1. Many people use pre-made lists, but be careful – some lists are huge and can slow down your system.
What is the default hosts file content?
It typically contains comments and two lines: 127.0.0.1 localhost and ::1 localhost. The exact comments vary by macOS version.
Can I edit the hosts file without Terminal?
Yes, you can use a GUI text editor with admin privileges. For example, run sudo open -a TextEdit /etc/hosts in Terminal to open it in TextEdit. You still need Terminal to get the admin rights.
Why does my hosts file entry not work for a specific website?
Possible reasons: the site uses a different subdomain, your browser uses DoH, or the entry has a typo. Check the exact domain and try adding both example.com and www.example.com.
Is it safe to delete the hosts file?
No. The hosts file is a system file; deleting it can cause DNS resolution issues. Always keep a backup and only edit, not delete.