How to Edit the Hosts File in Mac OS X with Terminal
Need to edit or modify the hosts file on a Mac? This guide will show you exactly how to edit the hosts file in Mac OS. You’ll find hosts in Mac OS X is stored at /private/etc/hosts but it can also be accessed at the more traditional location of /etc/hosts. That said, if you’re looking to edit hosts, you’ll want to target the file located in /private/etc/ though.
We’ll walk through how to manually edit the hosts file in macOS Big Sur, MacOS Mojave, MacOS Catalina, MacOS High Sierra, MacOS Sierra, OS X El Capitan, Yosemite, OS X Lion, OS X Mountain Lion, and OS X Mavericks, this will be done with the command line using the simple text editor called nano. Don’t let the command line or Terminal sound intimidating though because it’s not, we’ll make the entire process of editing a Mac hosts file super easy.
How to Edit Hosts File on Mac OS
Let’s get started making some edits to /etc/hosts in macOS and Mac OS X!
- Launch Terminal, found in /Applications/Utilities/ or launched through Spotlight
- Type the following command at the prompt:
- Enter the administrator password when requested, you will not see it typed on screen as usual with the command line
- Once the hosts file is loaded within nano, use the arrow keys to navigate to the bottom of the hosts file to make your modifications
- When finished, hit Control+O followed by ENTER/RETURN to save changes to /private/etc/hosts, then hit Control+X to exit out of nano
- Quit out of Terminal when finished
sudo nano /private/etc/hosts
You can verify your hosts modifications immediately with ping, Safari, or any other network app.
Changes take effect immediately though some adjustments may need to be accompanied by a DNS flush which can be done with the following command in macOS 10.12+ through OS X 10.9:
dscacheutil -flushcache;sudo killall -HUP mDNSResponder
When flushing DNS cache with that command you will need to enter the admin password.
If you’d like to see how this entire process is accomplished before doing it yourself, watch the video below to see a demonstration of the hosts file being modified on Mac OS X to block the website ‘yahoo.com’ from loading:
Note: the procedure is the same with older versions of Mac OS X, though the path to hosts could be /etc/hosts if the version of Mac OS X is dated significantly.
Tips to Consider When Editing Hosts Files
The following tips go beyond Mac OS X and apply to any hosts file, be it on a Mac, Windows, or Linux.
- The preceding IP address is where the following domain will resolve to
- Always add new hosts to their own unique line
- The # symbol functions as a comment, it can be used to add comments to hosts entries or to comment out hosts modifications
- You can block websites through hosts by adding them to the file and sending them nowhere, preventing access
- You can redirect websites locally using the same logic, perfect for setting up test domains
- With some modifications, it can be necessary to flush DNS cache with dscacheutil before the changes take effect
- For juggling multiple hosts files consider using a manager app like GasMask
- If the hosts file claims to be locked, it’s because you did not prefix the edit with the “sudo” command
- Consider making a backup of hosts if you plan on making significant modifications, or it’s your first time editing the file (process described below)
Making a backup of the hosts file can be a good idea if you plan on making significant changes or just want to play around with modifications and see what happens, a simple way to do that would be to use this command, which would store a backup in your home ~/Documents/ folder:
sudo cp /private/etc/hosts ~/Documents/hosts-backup
Then, if you wanted to restore the modified hosts to the backup of the original file, you just have to swap the paths like so and rename the file again:
sudo cp ~/Documents/hosts-backup /private/etc/hosts
That’s it, though again you may need to flush the DNS for changes to take effect.
Finally, it’s worth mentioning that if you’d prefer to avoid the Terminal and the command line completely, you could try the easier method of using a preference pane to modify the contents of hosts that way through System Preferences instead. Generally speaking though, we recommend just using the tools that are built directly into the Mac.
Let us know in the comments if you have any questions or tips.