The hosts file on a Mac is located at /private/etc/hosts. Most users cannot find it by browsing folders because the /private directory is hidden by the system by default.
You cannot simply double-click this file to open it in a text editor because it is a protected system file. Attempting to save changes in a standard window will result in a permission error, as you must use administrative privileges to modify the file.
How to Open the Hosts File Using Terminal
The most reliable way to access and edit the hosts file is through the Terminal application. This method allows you to use the nano text editor, which operates entirely within the command line and supports administrative overrides.
- Open Finder and navigate to Applications > Utilities.
- Double-click Terminal to launch the command line interface.
- Type the following command exactly:
sudo nano /private/etc/hosts. - Press Enter.
- The system will prompt you for your administrator password. Type your password and press Enter. Note that the cursor will not move and no characters will appear as you type; this is a standard security feature of the terminal.
Once these steps are complete, the hosts file will open in the nano editor. You can use the arrow keys on your keyboard to move the cursor to the bottom of the file where you can add your new entries. Labels and menu paths may differ slightly depending on your version of the operating system.
How to Save Changes in the Nano Editor
Because nano is a command-line editor, it does not have a "Save" button in a menu. You must use keyboard sequences to commit your changes and exit the program.
- After typing your desired entries, press Control + O (the letter O). This command stands for "Write Out."
- The editor will ask you to confirm the filename to write to. Press Enter to confirm that you want to save to /private/etc/hosts.
- Press Control + X to exit the editor and return to the main Terminal prompt.
If you make a mistake and want to exit without saving any changes, simply press Control + X. If you have modified the text, nano will ask if you want to save the changes; type 'N' for no and press Enter.
What Entries Should Look Like in the Hosts File
The hosts file follows a strict syntax consisting of an IP address, a space or tab, and the hostname you wish to associate with that address. If the formatting is incorrect, the system will ignore the entry or may cause network resolution errors.
| Entry Type | Format Example | Result |
|---|---|---|
| Local Redirect | 127.0.0.1 example.com | Sends traffic for example.com back to your own machine. |
| Remote Redirect | 192.168.1.50 dev.local | Sends traffic for dev.local to a specific server IP. |
| Blocking Site | 0.0.0.0 ads.website.com | Prevents the computer from connecting to the specified domain. |
Each new entry must be placed on its own line. Do not add commas, semicolons, or quotes around the IP addresses or the domain names. You can add a comment to your file by starting a line with the hash symbol (#), which tells the system to ignore everything on that line.
Redirecting a Local Development Site
Imagine you are building a website and want to use the domain name "mysite.test" instead of an IP address while working on your local machine. Your local server is running on the standard loopback address 127.0.0.1.
You would open the hosts file and add the following line at the bottom:
127.0.0.1 mysite.test
After saving and exiting, typing "mysite.test" into your web browser will direct the request to your own computer rather than searching the public internet for that domain. If you later move the site to a staging server with the IP 93.184.216.34, you would change the line to:
93.184.216.34 mysite.test
Why Are My Hosts File Changes Not Working?
The most common reason changes do not take effect immediately is the DNS cache. Your Mac stores a temporary record of DNS lookups to speed up internet browsing, meaning it may still be using the old address even after you have edited the hosts file.
To force the system to recognize the changes, you must flush the DNS cache. In the Terminal, type the following command and press Enter:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
You will be prompted for your administrator password again. Once the command executes, the system will clear the cached records and read the updated hosts file on the next request.
Correcting the "Read-Only" Misconception
A common misconception is that the hosts file is "read-only" and cannot be changed. While it is true that the file is protected to prevent malware from hijacking your traffic, it is not permanently locked. The "read-only" status you see when opening the file in a basic text editor is a permission restriction, not a file attribute. Using the sudo command in Terminal bypasses this restriction by granting you temporary root (superuser) privileges.
How to Restore the Hosts File to Default
If you have added too many entries or accidentally deleted a critical system line, you may find that certain websites or system services stop working. The default hosts file is very minimal and primarily contains instructions for the local machine.
To restore the file, open it via Terminal as described previously and ensure it contains only these basic lines:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Remove any other lines you added manually. If you have deleted the original lines and cannot remember them, you can find the default configuration by searching for the "macOS default hosts file" on an official Apple support page or a trusted technical documentation site. Always make a backup copy of your hosts file before making significant changes by running the command cp /private/etc/hosts ~/Desktop/hosts.backup, which places a copy of the current file on your desktop.
How to Find the File Using the Finder Interface
If you prefer a visual interface over the Terminal, you can navigate to the hosts file using the "Go to Folder" feature in Finder, though you will still encounter permission issues when trying to save changes.
- Open a Finder window.
- Click the "Go" menu at the top of the screen.
- Select "Go to Folder..." from the dropdown.
- Type
/private/etc/and press Enter. - Locate the file named "hosts" in the list.
You can open this file with TextEdit to view its contents. However, when you try to save, TextEdit will likely tell you that the document cannot be saved because it is locked or you do not have permission. To save using this method, you would have to drag the hosts file to your desktop, edit it there, save it, and then drag it back into the /private/etc/ folder, confirming the replacement with your administrator password. The Terminal method remains the fastest and most reliable approach.