Accidentally deleted your hosts file? You can get it back
iBoysoft Data Recovery restores lost or overwritten system files in a few clicks — works in read-only mode so nothing else is touched, and runs in macOS recovery mode too. Available on Setapp.
Editing the Mac hosts file takes five steps. Locate the file at /private/etc/hosts, back it up, open it in Terminal with nano, add or edit your entries, then flush the DNS cache with sudo killall -HUP mDNSResponder. No reboot needed.
Back up before you touch anything. Mistakes in the hosts file can break domain routing. Use Get Backup Pro to create encrypted, scheduled backups of the file before every edit.
Terminal is built-in, but there are better options.Core Shell makes regular editing easier with session recovery and built-in SFTP. SSH Config Editor gives you a visual interface for managing multiple host configurations.
If something goes wrong, recovery is possible. A corrupted or accidentally deleted hosts file can be restored from a backup or recovered with iBoysoft Data Recovery, which works in read-only mode so it won't touch existing data.
All the tools mentioned are available onSetapp. Core Shell, SSH Config Editor, Get Backup Pro, CleanMyMac, and iBoysoft Data Recovery — one subscription, 7-day free trial, no separate purchases needed. Start your risk-free Setapp trial.
Editing the Mac hosts file is simpler than it sounds — open Terminal, make your changes in nano, flush the DNS cache, and you're done.
If you're testing a new website, blocking distracting sites, or strengthening your Mac's security, you'll need to edit your hosts file. It’s a plain-text mapping file located at /private/etc/hosts that tells your Mac which IP address to connect to when you type in a domain name.
Don't let the file path intimidate you. I'll walk you through five steps that make finding and editing the hosts file on your Mac pretty straightforward. Here’s a quick overview of the main methods and tools for that. Detailed instructions follow below:
The hosts file lives in the /etc/hosts folder on your Mac. Despite the technical-sounding path, accessing it takes just a few clicks.
To locate the hosts file on a Mac:
Open Finder.
Click Go in the top menu bar > Go to Folder from the menu bar (or press Shift + Command + G).
Type /private/etc/hosts in the dialog box.
Click Go.
You'll land in the "etc" folder with the hosts file highlighted. Now you're ready to edit it.
Before you edit the hosts file
Step 1: Back up your hosts file
Before editing, I always create a backup. If something goes wrong, you can restore the previous version without losing anything. Copy the file to an external drive or use a backup tool.
I use Get Backup Pro for this. It creates encrypted backups of specific files or your entire system. If something happens to your device, you can recover your files on any Mac with the app installed.
If you're working on a public network, consider setting up a VPN on your Mac for extra security. I'd recommend ClearVPN to keep your connection secure while you're making changes.
Step 2: Open Terminal on macOS
You'll need Terminal to edit the hosts file. To open it:
Press Command + Space to open Spotlight.
Type "Terminal" and press Return.
The built-in Terminal works just fine, but if you edit hosts files regularly, Core Shell might be a better option.
Core Shell includes built-in SFTP capabilities for managing remote hosts. It also restores your session if your connection fails. All the saved host files appear right at your fingertips — you just have to select the one you want to edit.
Manage all your hosts from one clean list — no re-entering passwords, no lost sessions.
Core Shell brings tabs, iCloud sync, and automatic reconnect to every SSH workflow. Rated 97% by Setapp users. Get Core Shell on Setapp.
Step 3: Add a hosts file entry
Here's how to access the hosts file for editing:
In Terminal, type sudo nano /etc/hosts and press Return.
Enter your admin password when prompted.
The nano text editor opens with your hosts file.
You're now ready to make changes to the file.
Step 4: Edit the hosts file
The next steps depend on what you're trying to accomplish. To add a new domain mapping:
Use the arrow keys to navigate below the existing entries.
Press Return to create a new line.
Type the IP address (like 127.0.0.1).
Press Tab once.
Type the domain name (like example.com).
Press Control + O to save the file.
Press Return to confirm the filename.
Press Control + X to exit nano.
Changes take effect immediately. With the hosts file, you can redirect any domain to a different IP. Use 127.0.0.1 to point domains to your local development server. You can also block malicious sites by assigning them a 0.0.0.0 address.
For a more complete walkthrough of website blocking beyond the hosts file, see a guide on how to block websites on Mac.
In Terminal, type sudo killall -HUP mDNSResponder.
Press Return.
Enter your admin password if prompted.
Your DNS cache is now cleared, and the new hosts file entries are active.
Fix a malfunctioning Mac hosts file
Your Mac hosts file can malfunction for several reasons — wrong permissions, formatting errors, or conflicting entries. Here's how to fix the most common issues.
Adding too many entries can cause your Mac to misprocess the hosts file. Duplicate domain mappings or accidentally deleted entries are common culprits.
To troubleshoot:
Check file permissions with ls -la /etc/hosts in Terminal. The file should show -rw-r--r-- permissions.
Verify each entry follows the format: IP_address hostname.
Remove duplicate entries for the same domain.
Ensure no extra spaces or tabs exist at the ends of lines.
Reset the hosts file to default if needed.
The default hosts file contains:
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost
If your hosts file is missing entirely, restore it from a backup or use recovery software.
Recover lost hosts file or data on Mac with iBoysoft Data Recovery
Your Mac's hosts file might be tiny, but it plays a surprisingly important role. If you accidentally delete or overwrite it, you can experience unexpected connectivity issues.
Unlike command-line recovery tools,iBoysoft Data Recovery has a clean, intuitive interface. What I like most is that no technical expertise is needed — just a few clicks to get your files back. Recovery is possible as long as the disk space hasn't been overwritten, so the sooner you act, the better.
Here's how to recover your files using iBoysoft Data Recovery:
Install and open the app.
Choose the drive where your hosts file was stored.
Click Search for Lost Data.
Select the file and click Preview in the right pane.
Click Recover.
The app works in read-only mode, so it won't modify any existing data during recovery.
Tips to protect your hosts file on Mac
Protecting your Mac's hosts file from unauthorized changes reduces the risk of phishing attacks or malware infections that could redirect you to malicious sites.
I also recommend running regular scans with CleanMyMac. It's great for detecting and removing any malicious software that could tamper with your hosts file. Just run a Protection scan from the sidebar, and the app detects and removes any threats on your machine. Personally, I run it any time I make a system-level edit — it takes under a minute and tells me nothing unwanted crept in.
If you need to manage multiple host configurations, SSH Config Editor provides a visual interface. You can browse through all host entries, edit revocation states, and manage known-hosts files with ease. The app even generates RSA key pairs for secure access.
Master your Mac's hosts file with the right tools
Editing your Mac's hosts file gives you powerful control over domain routing and security. Whether you're blocking distractions, testing websites, or enhancing protection, the process takes just minutes with the right approach.
The hosts file is located at /private/etc/hosts on your Mac. To find it quickly, open Finder, press Command + Shift + G, type /private/etc/hosts, and click Go. The file opens in the etc folder.
How do I edit the hosts file on a Mac?
Open Terminal and type sudo nano /etc/hosts, then press Return. Enter your admin password. Add new entries in the format IP_address domain.com. Save with Control + O, then exit with Control + X. Flush the DNS cache with sudo killall -HUP mDNSResponder.
How do I reset the hosts file to its default state?
Open the hosts file in nano, delete all custom entries below the localhost lines. Keep only the default entries: 127.0.0.1 localhost, 255.255.255.255 broadcasthost, and ::1 localhost. Save the file and flush your DNS cache to apply changes.
Do I need to flush DNS after changing the hosts file?
Yes, flush the DNS cache to ensure changes take effect immediately. Run sudo killall -HUP mDNSResponder in Terminal. This clears cached DNS records and forces your Mac to read the updated hosts file.
Is it dangerous to edit your hosts file?
Editing your hosts file is generally safe — any changes can be reversed by restoring the file. That said, mistakes happen. You might accidentally run test scripts against production. Always back up before editing.
Do you need to reboot after changing the hosts file?
No, you don't need to reboot. Once you save the changes and clear your DNS cache, the updates take effect immediately. Your Mac reads the hosts file in real time for domain resolution.
How do I clear the cache in the hosts file?
Open Terminal and type sudo killall -HUP mDNSResponder, then press Return. This command flushes your Mac's DNS cache, ensuring the system recognizes your hosts file updates without needing a restart.
What should I do if I accidentally delete my Mac's hosts file?
If you delete your hosts file, restore it from a backup first. If no backup exists, use iBoysoft Data Recovery to scan your drive and recover the deleted file. The app works in read-only mode to protect existing data.
Does iBoysoft work with an encrypted Mac?
Yes, iBoysoft Data Recovery recovers directly from FileVault-encrypted drives. Supply the proper password or recovery key to unlock the disk. The app handles the rest of the recovery process automatically.
Will iBoysoft work if my Mac won't start?
iBoysoft Data Recovery works in macOS recovery mode. If your Mac won't boot properly, launch the tool in recovery mode to access your drive and recover your data, including system files like the hosts file.
Is iBoysoft safe to use for system files?
Yes, iBoysoft Data Recovery is completely safe for system files. The tool operates in read-only mode and recovers files, including the hosts file, without changing any existing data on your drive.