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
- 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)
- 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
- 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.
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
Simplify your hosts file management
Locahl lets you manage your hosts file visually, without touching the terminal. Automatic DNS flush, multiple environments, and backups included.
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 mDNSResponderCommon 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 | €9.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"
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.
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: definition, location, and syntax (2026 guide)
Master the hosts file in 5 minutes: location on Mac/Windows/Linux, syntax, use cases (local dev, ad blocking) and common mistakes to avoid. Updated January 2026.
Locahl Team
Terminal vs GUI: Why Developers Are Switching to Hosts File Apps
Compare editing hosts file via Terminal vs using Locahl GUI. Learn about permission issues, typo risks, backups, DNS flush, and why GUI hosts file managers are better for developers.
Locahl Team
Using Hosts Files for Docker Development on Mac
Learn how to configure hosts files for Docker, docker-compose, and container networking. Map container services to local domains and streamline your Docker development workflow.
Locahl Team