How to Edit the Hosts File on Mac (/etc/hosts) β 2026
Edit /etc/hosts on Mac with sudo nano or a GUI. Exact path, permission denied fixes, DNS flush, and when to use a hosts file manager instead of Terminal.
Sleezr Team

Table of Contents
- How to edit the hosts file on Mac
- Why edit the hosts file on Mac?
- Terminal method: sudo nano /etc/hosts
- Step 1: Open Terminal
- Step 2: Open the file with nano
- Step 3: Edit the file
- # Host Database
- 127.0.0.1 localhost
- Step 4: Save and quit
- Step 5: Flush DNS cache (crucial)
- Sources and further reading
- Common problems with Terminal
- "Permission denied"
- Changes not taking effect
- Syntax error
- Unreadable file with many entries
- GUI method: graphical interface
- Advantages over terminal
- How it works with Sleezr
- Advanced features
- Comparison: Terminal vs GUI
- Verify that your changes work
- Test with ping
- Test in browser
- Advanced troubleshooting
- The .local domain is slow on Mac
- The browser ignores the hosts file
- Best practices for daily use
- Team workflow
- Conclusion
Editing the hosts file on Mac is a common task for developers, but it can be frustrating with permission issues, commands to memorize, and DNS cache to flush. In this guide, we'll look at the two main methods: the classic Terminal method and the modern alternative with a graphical interface.
How to edit the hosts file on Mac
To edit the hosts file on Mac, open it with administrator rights in Terminal and save your changes to /etc/hosts:
sudo nano /etc/hosts and enter your administrator password.127.0.0.1 mysite.test.sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder.The detailed Terminal and GUI methods are below.
Why edit the hosts file on Mac?
The hosts file allows you to redirect domain names to specific IP addresses, locally on your Mac. Here are the most common use cases:
Local web development
- Create custom domains like myproject.local
- Simulate multi-domain environments
- Test configurations before going to production
Testing and debugging
- Point a production domain to a staging server
- Test a server migration before changing official DNS
- Isolate services for debugging
Site blocking
- Block distracting sites
- Block ads at the system level
Terminal method: sudo nano /etc/hosts
This is the traditional method, used by developers for decades.
Step 1: Open Terminal
Several ways to open Terminal on Mac:
- Spotlight: Press Cmd + Space, type "Terminal", then Enter
- Finder: Applications > Utilities > Terminal
- Launchpad: Search for Terminal in the app grid
Step 2: Open the file with nano
Type the following command and press Enter:
sudo nano /etc/hostsThe system will ask for your administrator password. Type it (nothing appears during input, that's normal) and press Enter.
Step 3: Edit the file
You'll see the current contents of the hosts file. By default, it looks like this:
##
# Host Database
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhostUse the arrow keys to navigate to the end of the file, then add your entries:
# My local projects
127.0.0.1 myproject.local
127.0.0.1 api.myproject.localStep 4: Save and quit
- Press Ctrl + O to save
- Press Enter to confirm
- Press Ctrl + X to quit nano
Step 5: Flush DNS cache (crucial)
This step is often forgotten. Without it, your changes may not take effect.
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponderSources and further reading
- Terminal User Guide (Apple Support)
- The hosts file explained (Wikipedia)
- hosts(5) manual page (man7.org)
Common problems with Terminal
"Permission denied"
Cause: You forgot sudo before the command.
Solution: Use sudo nano /etc/hosts.
Changes not taking effect
Cause: DNS cache wasn't flushed.
Solution: Always run the DNS flush command.
Syntax error
Cause: Incorrect entry format.
Solution: Follow the format IP[TAB or SPACES]domain.
Unreadable file with many entries
Cause: With dozens of entries, finding a specific line becomes tedious.
Solution: Use comments to organize, or switch to a GUI solution.
GUI method: graphical interface
For those who prefer to avoid the terminal or manage many entries, a GUI significantly simplifies the workflow.
Advantages over terminal
- No commands to memorize
- Automatic IP address validation
- Instant search through entries
- Visual organization (drag & drop)
- Enable/disable entries without deleting them
- Automatic backup before each modification
- Built-in one-click DNS flush
How it works with Sleezr
Advanced features
Environment management
Create groups of entries (Dev, Staging, Production) and switch between them with one click.
JSON Import/Export
Share your configurations with your team.
History and backups
Each modification automatically creates a backup.
Comparison: Terminal vs GUI
| Criteria | Terminal (nano) | GUI (Sleezr) |
|---|---|---|
| Learning curve | Medium | Low |
| Managing 50+ entries | Difficult | Easy |
| Syntax validation | Manual | Automatic |
| DNS Flush | Separate command | Built-in |
| Backups | Manual | Automatic |
| Cost | Free | β¬4.99 (one-time) |
Verify that your changes work
After modifying the hosts file and flushing DNS cache:
Test with ping
ping myproject.localExpected result:
PING myproject.local (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.042 msTest in browser
Open http://myproject.local in your browser.
Advanced troubleshooting
The .local domain is slow on Mac
macOS uses Bonjour (mDNS) for .local domains, which can create delays.
Solution: Use .test or .localhost instead of .local.
The browser ignores the hosts file
Some browsers have their own DNS cache.
Solution:
- Chrome:
chrome://net-internals/#dnsthen "Clear host cache" - Firefox: about:networking#dns then "Clear DNS Cache"
Best practices for daily use
If you edit /etc/hosts regularly, treat it like project configuration rather than a random system file.
- Add comments before each group.
- Use
.testdomains for local development. - Keep production-domain overrides temporary.
- Add both
127.0.0.1and::1when needed. - Back up before bulk imports.
- Flush DNS after every change.
- Clear Chrome DNS cache when Chrome ignores the update.
Example:
# Client: Acme
127.0.0.1 app.acme.test
127.0.0.1 api.acme.test
::1 app.acme.test
::1 api.acme.test
# Temporary migration - remove after launch
203.0.113.50 www.example.comTeam workflow
For teams, avoid sending raw snippets in chat. Instead:
- Keep a documented list of required local domains.
- Export/import hosts profiles with Sleezr.
- Use the same naming convention across the team.
- Include hosts setup in onboarding docs.
- Add a rollback note for temporary migration entries.
This prevents the classic bug where one developer tests api.local, another tests api.test, and nobody is hitting the same service.
Conclusion
Editing the hosts file on Mac is an essential skill for any developer. The Terminal method remains viable for one-off changes, but for daily management, a graphical interface like Sleezr saves precious time and avoids errors.
Also on other platforms: see our main edit host file guide for Windows and Linux, or the dedicated Windows and Linux tutorials.
Whatever method you choose, never forget: use sudo, check the syntax, and flush DNS cache.
To deepen your knowledge, check our complete guide to the hosts file or learn how to block ads with the hosts file.
Frequently Asked Questions
How do I open the hosts file on Mac?
Via Terminal with the command: sudo nano /etc/hosts. You will need to enter your administrator password. Alternatively, use a GUI application like Sleezr to avoid the terminal.
Why do I need sudo to edit the hosts file?
The /etc/hosts file is a protected system file. Only the root user can modify it. The sudo command temporarily gives you root privileges.
How do I save my changes in nano?
In the nano editor, press Ctrl+O to save (Write Out), then Enter to confirm. Then press Ctrl+X to exit.
How do I flush DNS cache after modification?
Run: sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder. This command works on macOS Monterey and later.
Is there a GUI for the hosts file on Mac?
Yes, applications like Sleezr offer a visual interface to manage the hosts file without terminal, with automatic validation and built-in DNS flush.
Related Articles
Hosts File: Location, Syntax and Uses (2026)
Learn what the hosts file does, where to find it on Mac, Windows and Linux, syntax examples, local dev uses and mistakes to avoid.
Sleezr Team
Hosts File Location Windows 11: C:\Windows\System32\drivers\etc\hosts
Exact hosts file path on Windows 10/11 and 24H2: C:\Windows\System32\drivers\etc\hosts. Edit with Notepad or PowerShell, flush DNS, and fix access denied.
Sleezr Team
Terminal vs GUI for Editing Hosts Files
Compare editing hosts files in Terminal vs a GUI app: permission issues, typo risks, backups, DNS flush and developer workflow speed.
Sleezr Team
Using Hosts Files for Docker Development on Mac
Configure hosts files for Docker, docker-compose and container networking. Map services to local domains and simplify Mac development.
Sleezr Team
How to Reset the Hosts File to Default (Windows, Mac, Linux)
Reset and restore the hosts file to its default contents on Windows, macOS and Linux. Copy the exact default file, back up first, then flush DNS so the reset takes effect.
Sleezr Team
Developer tools team