Mac Hosts File: Edit, Flush, and Fix
The Mac hosts file is a plain-text file that maps hostnames to IP addresses, letting you override DNS for specific domains. You can edit it to block websites, redirect domains, or test a server before changing DNS. This article explains where it lives, how to edit it safely, and how to fix common problems.
What the Hosts File Does
Every time your Mac connects to a domain like example.com, it first checks the hosts file before asking a DNS server. If the file contains an entry for that domain, the Mac uses the IP address listed there and skips DNS entirely. This makes it a powerful tool for local development, ad blocking, and network troubleshooting.
The file is managed by the system, so it requires administrator privileges to edit. It has no file extension and is located at /etc/hosts. The format is simple: each line contains an IP address, one or more hostnames, and optionally a comment after a # symbol.
Where the Hosts File Is Located
The hosts file is at /etc/hosts on every macOS version. You can view it in Terminal with:
cat /etc/hosts
It is a system file, so you cannot edit it with a standard text editor without using sudo. The file is owned by root, and the directory /etc is not writable by normal users. You must either use a command-line editor with sudo or copy the file to your desktop, edit it there, and copy it back with administrator rights.
How to Edit the Hosts File
To edit the hosts file, open Terminal and use a command-line editor. The most common method is nano:
- Open Terminal (Finder > Utilities > Terminal).
- Type
sudo nano /etc/hostsand press Return. - Enter your administrator password when prompted. You will not see characters as you type.
- Use the arrow keys to move to the bottom of the file.
- Add your entries, then press Control+O to save, and Control+X to exit.
If you prefer a graphical editor, you can copy the file to your desktop, edit it with TextEdit, and then copy it back with sudo cp. However, TextEdit may add smart quotes or other formatting, so it is safer to use a plain-text editor or Terminal.
Common Entries and Examples
Here are typical entries you might add:
- Block a website:
127.0.0.1 facebook.comand127.0.0.1 www.facebook.com– this sends the domain to your own Mac, effectively blocking it. - Redirect a domain:
192.168.1.10 myserver.local– this makesmyserver.localresolve to a local IP address. - Test a new server:
203.0.113.5 example.com– this overrides the real IP for testing purposes.
You must add entries for both the bare domain and the www subdomain, because they are treated as separate hostnames. Also, some sites use multiple subdomains, so you may need to add each one.
How to Flush the DNS Cache
After editing the hosts file, your Mac may still use the old DNS cache. To force it to re-read the file, flush the DNS cache. The command depends on your macOS version:
- macOS Ventura and later:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder - macOS Monterey and earlier:
sudo killall -HUP mDNSResponder
Run the command in Terminal and enter your password. You should not see any output if it succeeds. After flushing, test with ping example.com or nslookup example.com to verify the new IP is being used.
Common Problems and Fixes
If your hosts file changes do not take effect, check these issues:
- Typos or wrong IP: Double-check the IP address and hostname. A single wrong character can cause a failure.
- Missing www entry: Add both
example.comandwww.example.com. - DNS cache not flushed: Run the flush command above.
- VPN or proxy: Some VPNs and proxies ignore the hosts file. Disconnect and test again.
- File permissions: If you cannot save, you may have edited a copy. Ensure you are editing
/etc/hostsdirectly withsudo.
Another common issue is that the hosts file gets overwritten by security software or system updates. If your entries disappear, re-add them and consider backing up the file.
Safety and Security Considerations
Editing the hosts file is safe if you follow basic rules. Never remove the default entries for localhost or broadcasthost because they are needed for network services. Always back up the original file before making changes:
sudo cp /etc/hosts /etc/hosts.backup
Be cautious when adding entries that point to external IPs. A malicious or incorrect entry can redirect you to a phishing site. Only use IP addresses you trust. Also, be aware that some malware modifies the hosts file to block security sites, so if you see unexpected entries, investigate them.
If you ever need to restore the default file, you can replace it with the backup you made, or use a known-good copy from another Mac.
Alternatives to Editing the Hosts File
For many tasks, you do not need to edit the hosts file. Consider these alternatives:
- DNS server: If you control a domain, change its DNS records instead of editing each Mac.
- Browser extensions: For ad blocking or site blocking, extensions like uBlock Origin work at the browser level and are easier to manage.
- Local development tools: Tools like
dnsmasqor Docker can handle local domain mapping without touching the system file. - Parental controls: macOS Screen Time can block websites without requiring hosts file edits.
These methods are often more flexible and do not affect the entire system.
Frequently Asked Questions
Why is my hosts file not working?
Most often it is because the DNS cache has not been flushed, or you forgot to add the www subdomain. Run the flush command and verify the entry with ping.
Can I edit the hosts file with TextEdit?
Yes, but you must copy it to your desktop, edit it, and then copy it back with sudo cp. Be sure to use plain text formatting, not rich text.
Do I need to reboot after editing the hosts file?
No, a reboot is not required. Flushing the DNS cache is sufficient. Some applications may need to be restarted to pick up the change.
What is the default hosts file on macOS?
It contains a few lines for localhost and broadcasthost. You can view it with cat /etc/hosts.
Can I block all traffic to a domain with the hosts file?
You can block the domain by pointing it to 127.0.0.1, but the browser may show a connection error. This does not stop traffic if the app uses a hardcoded IP or a proxy.
Is editing the hosts file reversible?
Yes, simply remove the lines you added and flush the cache. If you backed up the original file, you can restore it with sudo cp /etc/hosts.backup /etc/hosts.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.