Edit the hosts file on Mac: Terminal vs GUI (2026)
How to edit /etc/hosts on macOS without errors? Terminal (sudo nano) vs GUI comparison. Fix permission denied and DNS cache issues in 2 minutes.
Locahl 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 Locahl
- 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:
1. Open Terminal (Cmd + Space, type Terminal, press Enter). 2. Run sudo nano /etc/hosts and enter your administrator password. 3. Add a new line with the IP address, a space, then the domain β for example 127.0.0.1 mysite.test. 4. Save with Ctrl+O then Enter, and exit with Ctrl+X. 5. Flush DNS with 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 Locahl
1. Launch the app - The interface displays all your current hosts entries 2. Add an entry - Fill in the IP, domain, and optionally a comment 3. Validate - The app checks the syntax and applies the change 4. Flush DNS - A button lets you clear the cache instantly
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 (Locahl) |
|---|---|---|
| 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 Locahl.
- 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 Locahl 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.
Ready to simplify your workflow?
Stop wasting time with the terminal. Locahl lets you manage your hosts file in a few clicks, with automatic validation and no risk of errors.
- Intuitive visual interface
- Automatic DNS flush
- Multi-environment management
- Automatic backups
- JSON Import/Export
Reader Reviews
"Very practical article! The Terminal vs GUI comparison convinced me to switch to a visual solution."
November 15, 2025
"Finally a guide that clearly explains why my changes weren't working. The DNS flush was the issue!"
December 28, 2025
"Good article, I would have liked more details on Apache Virtual Hosts but otherwise very complete."
January 12, 2026
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 Locahl 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 Locahl 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.
Locahl Team
How to Edit the Host File (Windows, Mac & Linux)
Edit the host file on Windows, macOS and Linux: file location, admin rights, syntax, DNS flush, and troubleshooting when changes do not apply.
Locahl Team
How to Edit the Hosts File on Windows 10 (2026)
Edit the Windows 10 hosts file step by step: Notepad as administrator, file location, UAC, save errors, DNS flush, and troubleshooting access denied.
Locahl Team
How to Edit the Hosts File on Windows 11 (2026)
Three methods to edit the Windows 11 hosts file: Notepad as admin, PowerShell, and VS Code. Includes UAC tips, DNS flush, and troubleshooting for 24H2.
Locahl 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.
Locahl Team