Where apps hide your files
The hosts file is /etc/hosts, a small text file that maps names to addresses before DNS is asked. It needs administrator rights to change, so back it up first, and a changed entry may not apply until you flush the DNS cache.
/etc/hosts
/etc is a link to /private/etc, so /private/etc/hosts is the same file. On the Mac used for this guide it is owned by root, readable by everyone and writable only by root.
Finder hides /etc. To open its folder, choose Go > Go to Folder (⇧⌘G), type /etc and press Return, or reveal the file from Terminal:
open -R /etc/hosts
To read it, which changes nothing:
cat /etc/hosts
On a Mac that nobody has edited, the file is a short comment header (it starts # Host Database and warns not to change the localhost entry) followed by three working lines:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Leave those three lines alone; software on your Mac relies on localhost reaching the loopback address. Each line is an address, whitespace, then one or more names. A # starts a comment.
Before any edit, copy the current file into your home folder. This needs no administrator rights and changes nothing in /etc:
cp /etc/hosts ~/hosts.backup
TextEdit cannot save to /etc directly, so use Terminal and sudo:
sudo nano /etc/hosts
Enter your Mac password (nothing appears as you type). On recent macOS nano is a link to the pico editor that ships with the system, which uses the same keys. Use the arrow keys to go to the bottom, add your line, press ⌃O then Return to save, and ⌃X to exit. Examples:
127.0.0.1 example-site.test
10.0.0.25 staging.internal
0.0.0.0 ads.example.com
The first points a name at your own Mac for local development, the second maps a name to a server, and the third blocks a domain by sending it to an address that goes nowhere. Use your own names, one entry per line, and put a comment above each block you add (# dev sites) so you can find it later.
macOS caches name lookups, so an edit may seem to do nothing at first. The reset used across recent macOS versions is:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
It prints nothing when it works. Apple does not publish this as an official command for each release, but the pair is the standard answer for Sonoma and Sequoia, and running it is harmless. Browsers keep their own cache too: Chrome's can be cleared from its chrome://net-internals/#dns page, and quitting and reopening a browser clears the rest. A VPN, or DNS over HTTPS in a browser's settings, can bypass the hosts file entirely.
To list only the active lines, skipping comments and blank lines:
grep -v '^#' /etc/hosts | grep .
To see how your Mac now resolves a name, ask the system resolver, which reads the hosts file:
dscacheutil -q host -a name example-site.test
ping also goes through the system resolver. dig and nslookup query a DNS server directly and ignore the hosts file, so they will not show your entry even when it works.
sudo. Exit and use the sudo nano command above.*.example.com has no effect, so list each subdomain, including www. if the site uses it.127.0.0.1 myapp.test still needs the port in the URL.To go back to your backup:
sudo cp ~/hosts.backup /etc/hosts
That overwrites the current file, so run it only when you mean to, then flush the cache. With no backup, remove your own lines with sudo nano /etc/hosts until only the comment header and the three stock lines above remain.
The hosts file suits a handful of fixed entries: pointing a development domain at your own Mac, testing a site on a new server before DNS changes, or blocking a few known domains. For many entries, or for blocking across a whole network, a DNS-level or router setting is easier to maintain than a long hand-edited file.
The file belongs to the Mac, not to your user account. After moving to a new Mac or reinstalling macOS, check /etc/hosts and add back any lines you rely on. Keeping them in a note, or in the hosts.backup copy in your home folder, makes that a two-minute job.
/etc is a hidden system folder, and the file has no extension. Finder searches skip it, and Spotlight does not show system files in normal results.
Type hosts into Everywhere to find the file in /private/etc, the real folder behind /etc, next to copies such as hosts.backup. The word hosts also matches plenty of unrelated names, so check the folder column before you choose, then press ⌘T to open a Terminal there. It searches file names only, not the lines inside the file.
At /etc/hosts, which is the same file as /private/etc/hosts. Reveal it in Finder with open -R /etc/hosts.
Back it up with cp /etc/hosts ~/hosts.backup, then run sudo nano /etc/hosts, add a line such as 127.0.0.1 example.test, save with ⌃O and exit with ⌃X.
Run sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder in Terminal. It prints nothing on success.
The DNS cache, a browser's own cache, a VPN or browser DNS over HTTPS can all get in the way. Flush the cache, check for typos, and test in a private window.
Everywhere keeps track of every file on your Mac — including the folders Spotlight hides and drives you have unplugged. Free for a day, then $19 once.
Version 2.20.3 · macOS 13+ · 4.7 MB · notarised