Where is the hosts file on Mac, and how to edit it
The hosts file on a Mac is /etc/hosts, a plain text file in a system folder that Finder hides. To read it, press ⇧ ⌘ G in Finder and type /etc; to change it, run sudo nano /etc/hosts in Terminal, because only an administrator can save it. Your own settings files, such as .zshrc, are hidden too, but they sit in your home folder and need no password.
1. Where the hosts file is
The path is /etc/hosts. On a Mac, /etc is a link to /private/etc, so /private/etc/hosts is the same file, not a second copy. There is one hosts file for the whole Mac, shared by every user account. It belongs to the system (the root user): everyone can read it, and only an administrator can change it.
The file maps host names to IP addresses. macOS looks in it when an app asks for the address of a name, alongside its normal DNS lookups. Each line is an address, then one or more names, separated by spaces or tabs. A line that starts with # is a comment. On a Mac nobody has edited, the entries are:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Leave those three lines as they are; the comment at the top of the file warns against changing the localhost entry.
2. Open the hosts file in Finder
- Click the desktop or a Finder window and choose Go › Go to Folder (⇧ ⌘ G).
- Type
/etcand press Return. Finder opens the folder as/private/etc. - Find
hostsin the list. It has no file extension, so Control-click it and choose Open With › TextEdit.
This is fine for reading. Your account is not allowed to save a file in that folder, so make the changes in Terminal instead. To print the file without opening an editor:
cat /etc/hosts
3. Edit the hosts file in Terminal
- Open Terminal (in Applications › Utilities).
- Keep a copy of the current file:
cp /etc/hosts ~/Desktop/hosts-backup - Run
sudo nano /etc/hostsand type the password of an administrator account. Nothing appears while you type it. - Use the arrow keys to go to the end of the file and add your line, for example
127.0.0.1 myapp.testto point a test name at your own Mac. - Press ⌃ O and then Return to save, and ⌃ X to leave the editor.
The Control key is used here, not Command. To take an entry out later, open the file the same way and delete the line, or put # in front of it.
If a name still goes to the old address, clear the lookup cache and then quit and reopen the browser:
sudo dscacheutil -flushcache
One wrong line in the hosts file can make a website unreachable on this Mac for every app and every user. If something stops loading after an edit, check the file first, and put the backup back with sudo cp ~/Desktop/hosts-backup /etc/hosts.
4. Dotfiles: .zshrc and the other hidden settings files
A dotfile is a file or folder whose name starts with a period. Finder and a plain ls leave those names out, which is why command-line tools use them for settings. Most are in your home folder, so each user has their own and no password is needed to edit them. ~ stands for your home folder (how to find your home folder on Mac).
| File or folder | What it is |
|---|---|
~/.zshrc | Settings for zsh, the default shell on a Mac: aliases, the prompt, changes to PATH. Read each time a Terminal window or tab opens. |
~/.zprofile, ~/.zshenv | More zsh startup files. .zprofile is read by login shells, .zshenv by every zsh, scripts included. |
~/.zsh_history | The commands you have typed. |
~/.gitconfig | Your Git name, email and options. |
~/.ssh/ | SSH keys, config and known_hosts. Keep the private keys private. |
~/.config/ | A folder many command-line tools keep their settings in. |
.gitignore, .env | Per-project files, found inside project folders, not in your home folder. |
A new Mac has no ~/.zshrc. If Terminal says there is no such file, create it with touch ~/.zshrc. The system-wide versions are /etc/zshrc and /etc/zprofile, next to the hosts file; put your own settings in the files in your home folder and leave those alone. The default PATH comes from /etc/paths and the files in /etc/paths.d.
To edit one, either of these works:
# in TextEdit
open -e ~/.zshrc
# in Terminal
nano ~/.zshrc
# apply the changes to the window you are in
source ~/.zshrc
5. Show dotfiles in Finder
- In any Finder window, or an Open or Save dialog, press ⌘ ⇧ . (Command, Shift and the period key). Hidden items appear dimmed; the same keys hide them again.
- To go straight to one, choose Go › Go to Folder (⇧ ⌘ G) and type its path, such as
~/.ssh. - In Terminal,
ls -la ~lists everything in your home folder, dotfiles included.
The other ways, including making Finder show hidden files all the time, are in how to find hidden files on Mac. The Library folder in your home folder is hidden in a different way, with a flag instead of a dot (how to find the Library folder on Mac).
6. Find dotfiles anywhere on the Mac
The files in the table have fixed places. Per-project dotfiles do not: a .env or .gitignore can be in any folder. In Terminal, find looks through the folders one by one:
find ~ -name ".env" 2>/dev/null
Spotlight and a normal Finder search leave hidden files out of their results. More commands are in how to find files on Mac from the Terminal.
Everything Locator (free to try) keeps its own index of file names and can include hidden ones: open Settings › Indexing, tick Index hidden files and folders and choose Apply and Rebuild Index. After that, dotfiles are found as you type:
| Search | Finds |
|---|---|
wfn:.zshrc | files named exactly .zshrc |
startwith:.zsh | .zshrc, .zprofile, .zsh_history and the rest |
startwith:. parent:~ file: | the dotfiles directly in your home folder |
wfn:.env dm:thisweek | .env files changed this week, in any project |
wfn:.gitignore | every .gitignore |
~/.ssh/ | everything in your .ssh folder |
~/.config/ dm:thismonth | settings files changed this month |
Press ⌘ Return to show a result in Finder and ⇧ ⌘ C to copy its full path. The app finds files by name and does not edit them. Its content: search asks Spotlight, which skips hidden files and system folders, so it usually won't find a word inside dotfiles or the hosts file. The hosts file is outside your home folder, so it is in the results only if you gave the app the whole disk or that folder to index. Its path never changes, so you do not need a search for it.
FAQ
Where is the hosts file on a Mac?
At /etc/hosts, which is the same file as /private/etc/hosts. In Finder, press ⇧ ⌘ G, type /etc and press Return.
Why can't I save the hosts file?
It belongs to the system, and a normal account can only read it. Edit it in Terminal with sudo nano /etc/hosts and enter an administrator's password.
Do I need to restart the Mac after editing the hosts file?
No. Save the file, then quit and reopen the browser. If the old address is still used, run sudo dscacheutil -flushcache.
How do I put the hosts file back to the default?
Open it with sudo nano /etc/hosts and delete every line you or an app added, so that only the three original entries are left: 127.0.0.1 localhost, 255.255.255.255 broadcasthost and ::1 localhost.
Where is .zshrc on a Mac?
In your home folder, at ~/.zshrc. It is hidden because its name starts with a period, and it does not exist until you or an installer creates it. Press ⌘ ⇧ . in Finder to see it, or run touch ~/.zshrc to create it.
Find any dotfile by name
Everything Locator searches your Mac by name as you type, hidden files included.