Hosts File Not Working After Edit on Mac: Complete Fix Guide
Your hosts file changes not taking effect? Learn how to fix DNS cache issues, browser cache, file permissions, syntax errors, and encoding problems on macOS.
Locahl Team
Table of Contents
- Understanding how the hosts file works
- Issue #1: DNS cache not flushed (most common)
- Why DNS cache matters
- Solution: Flush DNS cache
- Verify DNS cache flush
- Issue #2: Browser DNS cache
- Chrome/Chromium browsers
- Firefox
- Safari
- Edge and other Chromium browsers
- Issue #3: File permissions
- Check current permissions
- Correct permissions
- Fix permissions
- Verify permissions fix
- Issue #4: Syntax errors
- Correct format
- Common syntax mistakes
- Validate syntax
- Issue #5: File encoding problems
- Check encoding
- Fix encoding issues
- Issue #6: System Integrity Protection (SIP)
- Check SIP status
- Issue #7: Network location or VPN interference
- Check active DNS servers
- VPN DNS override
- Network location
- Issue #8: IPv6 vs IPv4 conflicts
- Add IPv6 entries
- Disable IPv6 (if needed)
- Issue #9: mDNSResponder not responding
- Restart mDNSResponder
- Check mDNSResponder status
- Complete troubleshooting checklist
- Testing your hosts file
- Step 1: Terminal test
- Step 2: DNS lookup test
- Step 3: Browser test
- Step 4: curl test
- Prevention: Best practices
- Use a GUI tool
- Regular backups
- Test incrementally
- Document entries
- Advanced: Debugging DNS resolution
- When to seek help
- Conclusion
You've edited your hosts file, added the entries correctly, but nothing seems to work. The domain still resolves to the old IP address, or worse, doesn't resolve at all. This frustrating situation is more common than you think, and the good news is that it's almost always fixable.
In this comprehensive troubleshooting guide, we'll walk through every possible reason why your hosts file changes aren't taking effect on macOS, and provide step-by-step solutions for each issue.
Understanding how the hosts file works
Before diving into troubleshooting, it's essential to understand the DNS resolution hierarchy on macOS. When your Mac tries to resolve a domain name, it checks in this order:
1. Hosts file (/etc/hosts) - checked first 2. DNS cache - macOS caches previous DNS lookups 3. DNS servers - configured via Network settings or DHCP
If your hosts file entry isn't working, something in this chain is likely interfering. Let's identify and fix each potential issue.
Simplify your hosts file management
Locahl lets you manage your hosts file visually, without touching the terminal. Automatic DNS flush, multiple environments, and backups included.
Issue #1: DNS cache not flushed (most common)
This is by far the most common reason hosts file changes don't work. macOS aggressively caches DNS lookups to improve performance, but this means your hosts file changes won't take effect until the cache is cleared.
Why DNS cache matters
When you first visit a domain, macOS stores the resolved IP address in its DNS cache. Subsequent requests use this cached value instead of checking the hosts file again. This is why your changes seem to be ignored.
Solution: Flush DNS cache
For macOS Sierra and later:
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponderFor macOS versions before Sierra:
sudo killall -HUP mDNSResponderFor macOS Big Sur and later (additional step):
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
sudo killall mDNSResponderHelperVerify DNS cache flush
After running the command, verify it worked:
ping yourdomain.localYou should see it resolving to the IP address specified in your hosts file.
Issue #2: Browser DNS cache
Even after flushing system DNS cache, browsers maintain their own separate DNS cache. This is why your hosts file might work in Terminal (ping, curl) but not in your browser.
Chrome/Chromium browsers
1. Open chrome://net-internals/#dns in the address bar 2. Click "Clear host cache" 3. Close and restart Chrome completely
Alternatively, restart Chrome with cache disabled:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --dns-prefetch-disableFirefox
1. Open about:networking#dns in the address bar 2. Click "Clear DNS Cache" 3. Restart Firefox
Safari
Safari uses the system DNS cache, so flushing system DNS should be sufficient. However, if issues persist:
1. Safari menu > Preferences > Advanced 2. Check "Show Develop menu" 3. Develop menu > Empty Caches 4. Restart Safari
Edge and other Chromium browsers
Follow the same steps as Chrome, using edge://net-internals/#dns for Edge.
Issue #3: File permissions
Incorrect file permissions can cause macOS to ignore the hosts file entirely. The hosts file must have specific ownership and permissions to function correctly.
Check current permissions
ls -la /etc/hostsYou should see something like:
-rw-r--r-- 1 root wheel 1234 Feb 6 10:30 /etc/hostsCorrect permissions
- Owner: root
- Group: wheel
- Permissions: 644 (rw-r--r--)
Fix permissions
If permissions are incorrect:
sudo chmod 644 /etc/hosts
sudo chown root:wheel /etc/hostsVerify permissions fix
After fixing permissions, flush DNS cache again and test:
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
ping yourdomain.localIssue #4: Syntax errors
Even a small syntax error can prevent the entire hosts file from working correctly. macOS is strict about hosts file format.
Correct format
Each entry must follow this exact format:
IP_ADDRESS DOMAIN_NAME- Use a single tab or multiple spaces between IP and domain
- Each entry on a single line
- No trailing spaces
- Comments start with
#
Common syntax mistakes
Wrong: Multiple domains on one line
# ❌ Wrong
127.0.0.1 domain1.com domain2.comCorrect: One domain per line
# ✅ Correct
127.0.0.1 domain1.com
127.0.0.1 domain2.comWrong: Missing separator
# ❌ Wrong
127.0.0.1domain.comCorrect: Tab or spaces
# ✅ Correct
127.0.0.1 domain.comWrong: Extra spaces
# ❌ Wrong
127.0.0.1 domain.com Correct: Clean format
# ✅ Correct
127.0.0.1 domain.comValidate syntax
Check your hosts file for syntax errors:
cat /etc/hosts | grep -v "^#" | grep -v "^$" | awk '{print $1, $2}'This shows all active entries. Each line should have exactly two fields: IP and domain.
Issue #5: File encoding problems
The hosts file must be in UTF-8 encoding without BOM (Byte Order Mark). Some text editors save files with incorrect encoding, which can cause macOS to ignore entries.
Check encoding
file /etc/hostsShould show: ASCII text or UTF-8 Unicode text
Fix encoding issues
If encoding is wrong, recreate the file:
# Backup first
sudo cp /etc/hosts /etc/hosts.backup
# Edit and save as UTF-8 without BOM
sudo nano /etc/hostsWhen saving in nano, ensure you're not adding any special characters. For GUI editors, check encoding settings before saving.
Issue #6: System Integrity Protection (SIP)
While SIP shouldn't prevent hosts file edits, it can interfere in rare cases. SIP protects system files, but the hosts file is designed to be editable.
Check SIP status
csrutil statusIf SIP is enabled (normal), it shouldn't affect hosts file functionality. Only disable SIP if absolutely necessary and you understand the security implications.
Issue #7: Network location or VPN interference
Sometimes network configurations or VPNs can override hosts file entries by using their own DNS servers.
Check active DNS servers
scutil --dns | grep nameserverVPN DNS override
If you're using a VPN, it might be routing DNS queries through VPN servers, bypassing the hosts file. Try:
1. Disconnect VPN temporarily 2. Flush DNS cache 3. Test hosts file entries 4. If it works, configure VPN to use local DNS
Network location
macOS Network Locations can have different DNS settings:
1. Apple menu > System Preferences > Network 2. Location dropdown (if multiple locations exist) 3. Check DNS settings for each location
Issue #8: IPv6 vs IPv4 conflicts
If you're only adding IPv4 entries but your system prefers IPv6, resolution might fail or use the wrong address.
Add IPv6 entries
For localhost entries, include both IPv4 and IPv6:
127.0.0.1 yourdomain.local
::1 yourdomain.localDisable IPv6 (if needed)
If you want to force IPv4 only:
networksetup -setv6off Wi-Fi
networksetup -setv6off EthernetRe-enable with:
networksetup -setv6automatic Wi-Fi
networksetup -setv6automatic EthernetIssue #9: mDNSResponder not responding
The mDNSResponder daemon handles DNS resolution on macOS. If it's not working correctly, hosts file changes won't take effect.
Restart mDNSResponder
sudo killall mDNSResponder
sudo killall mDNSResponderHelperThe system will automatically restart these services.
Check mDNSResponder status
ps aux | grep mDNSResponderYou should see the process running.
Complete troubleshooting checklist
Follow this checklist in order:
1. ✅ Verify hosts file syntax - Check for errors 2. ✅ Check file permissions - Should be 644, owned by root:wheel 3. ✅ Flush DNS cache - Run the flush command for your macOS version 4. ✅ Restart browser - Close completely and reopen 5. ✅ Clear browser DNS cache - Use browser-specific methods 6. ✅ Test with Terminal - Use ping or curl to verify 7. ✅ Check for VPN interference - Disconnect VPN if active 8. ✅ Verify encoding - Ensure UTF-8 without BOM 9. ✅ Restart mDNSResponder - If all else fails 10. ✅ Check network location - Verify DNS settings
Testing your hosts file
After applying fixes, test systematically:
Step 1: Terminal test
ping -c 3 yourdomain.localShould show your hosts file IP address.
Step 2: DNS lookup test
dscacheutil -q host -a name yourdomain.localShould return your hosts file IP.
Step 3: Browser test
1. Clear browser cache completely 2. Open in incognito/private mode 3. Navigate to your domain 4. Check browser developer tools Network tab for resolved IP
Step 4: curl test
curl -v http://yourdomain.localCheck the resolved IP address in the output.
Prevention: Best practices
To avoid hosts file issues in the future:
Use a GUI tool
GUI applications like Locahl automatically handle:
- Syntax validation
- Permission management
- DNS cache flushing
- Backup creation
- Encoding verification
Regular backups
Always backup before editing:
sudo cp /etc/hosts /etc/hosts.backup.$(date +%Y%m%d)Test incrementally
Add one entry at a time and test immediately. This makes it easier to identify problematic entries.
Document entries
Use comments to document your entries:
# Local development - Project Alpha
127.0.0.1 alpha.local
127.0.0.1 api.alpha.local
# Local development - Project Beta
127.0.0.1 beta.localAdvanced: Debugging DNS resolution
For persistent issues, enable detailed DNS logging:
sudo log config --subsystem com.apple.network.dns --mode level:debugThen check logs:
log show --predicate 'subsystem == "com.apple.network.dns"' --last 5mThis shows exactly how macOS is resolving domains.
When to seek help
If you've tried all troubleshooting steps and hosts file still isn't working:
1. Check system logs - Look for DNS-related errors 2. Try safe mode - Boot into safe mode and test 3. Check for conflicting software - VPNs, firewalls, security software 4. Verify macOS version - Some older versions have known DNS issues
Conclusion
Hosts file not working after editing is frustrating, but it's almost always fixable. The most common issues are DNS cache not being flushed and browser DNS cache. By following this guide systematically, you should be able to identify and resolve the problem.
Remember: flush DNS cache, restart browser, check syntax, verify permissions. These four steps solve 90% of hosts file issues.
For a hassle-free experience managing your hosts file with automatic validation, DNS flushing, and backup management, consider using Locahl. At just €9.99, it eliminates the common pitfalls that cause hosts file problems and saves you time troubleshooting.
Ready to streamline your local development workflow? Learn more about editing hosts file on Mac or check out our complete hosts file guide for advanced techniques.
Ready to simplify your workflow?
Stop wasting time with the terminal. Locahl lets you manage your hosts file in a few clicks, with automatic validation and no risk of errors.
- Intuitive visual interface
- Automatic DNS flush
- Multi-environment management
- Automatic backups
- JSON Import/Export
Reader Reviews
"This guide saved me hours of frustration! The DNS cache flush was exactly what I needed. Clear step-by-step instructions."
February 6, 2026
"Comprehensive troubleshooting guide. Covered all the issues I was experiencing. The browser cache section was particularly helpful."
February 6, 2026
"Great article! Would have liked more details on Chrome's DNS cache specifically, but overall very thorough."
February 6, 2026
Frequently Asked Questions
Why are my hosts file changes not working on Mac?
The most common reason is that DNS cache hasn't been flushed. macOS caches DNS lookups, so you need to run: sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder after editing the hosts file.
How do I flush DNS cache on macOS?
Run this command in Terminal: sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder. For older macOS versions (before Sierra), use: sudo killall -HUP mDNSResponder.
Do I need to restart my browser after editing hosts file?
Yes, browsers cache DNS lookups independently. After flushing system DNS cache, restart your browser or clear its DNS cache. Chrome: chrome://net-internals/#dns, Firefox: about:networking#dns.
What if my hosts file has syntax errors?
Check that each entry follows the format: IP[TAB or SPACES]domain. Ensure there are no extra spaces, tabs are used correctly, and each entry is on a single line. Comments should start with #.
Can file permissions prevent hosts file from working?
Yes, if the hosts file has incorrect permissions, macOS may ignore it. The file should be owned by root:wheel with permissions 644. Fix with: sudo chmod 644 /etc/hosts && sudo chown root:wheel /etc/hosts.
Why does my hosts file work in Terminal but not in browser?
Terminal tools like ping use system DNS, while browsers have their own DNS cache. Clear the browser's DNS cache or restart it completely after flushing system DNS.
Related Articles
Hosts File Permission Denied on Mac: Every Fix Explained
Getting "permission denied" when editing hosts file? Learn how to fix sudo access, System Integrity Protection (SIP), file ownership, chmod permissions, and disk permissions on macOS.
Locahl Team
Why Your Hosts File Changes Disappear After Reboot (and How to Fix It)
Hosts file changes gone after restarting Mac? Learn why macOS resets hosts file, how System Integrity Protection (SIP) affects it, daemon configurations, and backup strategies to persist your changes.
Locahl Team
Terminal vs GUI: Why Developers Are Switching to Hosts File Apps
Compare editing hosts file via Terminal vs using Locahl GUI. Learn about permission issues, typo risks, backups, DNS flush, and why GUI hosts file managers are better for developers.
Locahl Team