“`html
Modifying the Hosts file on a Mac can be useful for redirecting domains, blocking websites, or testing a web server locally. The process requires accessing system files and making manual changes, but with the right steps, it becomes a straightforward task.
Why Modify the Hosts File?
The Hosts file is a plain-text file that maps domain names to IP addresses. By editing this file, users can:
- Redirect websites to different IP addresses.
- Block access to certain sites.
- Test website changes before switching DNS settings.
Follow this five-step guide to change the Hosts file on a Mac safely and correctly.
Step 1: Open the Terminal
Since the Hosts file is a system file, it cannot be edited through the Finder. Instead, it requires using the Terminal application.
- Open Terminal from Applications > Utilities.
- Type the following command and press Enter:
sudo nano /etc/hosts
Using sudo gives administrative privileges, allowing the file to be modified.

Step 2: Enter the Administrator Password
Upon running the command, the system will prompt for the administrator password. Type the password and press Enter. The characters will not appear as they are typed for security reasons.
Step 3: Edit the Hosts File
Once inside the nano text editor, the default Hosts file content will be displayed. Here’s how to add or change entries:
- Navigate using arrow keys.
- Add a new mapping by entering the IP address, followed by a space, then the domain name. Example:
127.0.0.1 example.com
This will redirect example.com to the local machine.

Step 4: Save and Exit
After making changes, save the file by following these steps:
- Press Control + X to exit.
- Press Y to confirm saving the changes.
- Press Enter to finalize modifications.
Step 5: Flush the DNS Cache
For changes to take effect, it is necessary to flush the DNS cache. Run the following command in Terminal:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
After executing this command, the Mac will apply the new Hosts file settings.
Conclusion
Editing the Hosts file is a powerful way to control domain traffic on a Mac. By following this simple five-step guide, users can redirect URLs, block sites, or test new server settings without modifying global DNS records.
FAQ
What is the Hosts file on Mac?
The Hosts file is a local configuration file that maps domain names to IP addresses, overriding the DNS lookup process.
Why do I need to use Terminal to edit the Hosts file?
Since the Hosts file is a system-protected file, it requires administrative access via Terminal to make changes.
Will modifying the Hosts file affect my internet connection?
No, it only affects how the system resolves specific domains. General internet access remains unaffected.
Why do I need to flush the DNS cache?
Flushing the DNS cache ensures that the system recognizes the newly modified Hosts file immediately, preventing conflicts with cached information.
Can I restore the original Hosts file?
Yes, by simply removing any added entries and saving the file, the default settings are restored.
“`