Hosts File Not Working on Mac? Try This Fix
Some Mac users have discovered that the hosts file in MacOS does not appear to work, or changes to the /etc/hosts file on the Mac are seemingly ignored. Given that the hosts file is used to map IP addresses to host names, and is frequently modified by advanced users, this is an understandably annoying problem.
This is fairly obvious issue when it happens, because after editing the hosts file on a Mac from the command line or even with TextEdit, and flushing DNS cache, there does not appear to be any change to hosts.
Changes to the hosts file being ignored, or edits to the hosts file not working, are actually a fairly common occurrence, particularly with modern versions of MacOS system software. Fortunately it’s also usually a really easy fix.
Fix Hosts File Changes Ignored / Hosts File Not Working in MacOS
The most likely reason the hosts file is not working on the Mac is that it has been corrupted, or is no longer an ASCII file format. This can happen sometimes when attempting to edit the hosts file with a Rich Text editor or a third party app, or if the wrong file type was saved when using vim/vi/nano etc.
First, we’re going to backup/move the old hosts file by renaming it, this allows you to revert the change if desired:
sudo mv /etc/hosts /etc/hostsbackup
Copy the contents of the hostsbackup file into your clipboard, an easy way to do this is by using cat and then selecting the text and copying it to your clipboard:
cat /etc/hostsbackup
Now create a new hosts file with nano:
sudo nano /etc/hosts
Paste the contents of the original hosts file into your newly created hosts file.
Hit Control+o and Control+X to save and exit out of nano.
Next, you’ll probably want to flush DNS cache for changes to take effect.
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
That should be sufficient for the hosts change to carry forward, and you should not need to relaunch any browsers or other internet connected apps.
NOTE: Some Mac users report needing to restart their Mac for the new hosts file to be recognized, this is rare but may apply to some Macs running macOS Catalina or later.
There’s also a chance that you may need to enable the root account in Mac OS before you’ll be able to modify the hosts file, depending on how battened down your macOS installation is.
Some users continue to experience issues with the hosts file being ignored in macOS Monterey and macOS Ventura in particular, in which case you can also use a third party app like GasMask to manage a hosts file on the Mac, or even a browser extension to modify hosts if you’re looking to do this at the browser level. For example, for Google Chrome, a Chrome extension like LiveHosts does the job.
Have you experienced issues with the hosts file in MacOS before? Did the above solution of creating a new hosts file from the command line resolve the problem for you? Did you find another solution? Share your experiences with us in the comments!
Thanks — much appreciated.
you can use pbcopy to copy stdin directly to the clipboard
Yes great trick Elwood, and you can make it easy to copy/paste by catting the hosts and piping stdin to pbcopy like so:
cat /etc/hosts | pbcopy
Then you just need to make the new hosts file and paste, done.