We're currently building
Online shop for animal feed brandWordPress migration for law firmWooCommerce integration with Exact OnlineElementor website for architecture practiceMultilingual website for export companyWordPress maintenance for hospitality chainBespoke plugin for property platformOnline shop with stock integration for retailerOnline shop for animal feed brandWordPress migration for law firmWooCommerce integration with Exact OnlineElementor website for architecture practiceMultilingual website for export companyWordPress maintenance for hospitality chainBespoke plugin for property platformOnline shop with stock integration for retailer
WP Agency

Technical WordPress agency for websites, WooCommerce shops and integrations. A dedicated team for new projects and ongoing development, based in the Netherlands.

Guide: How to edit the Hosts file on a Mac

Written by Leon on 24 March 2025

The Hosts file is a text file on a Mac computer used to manage the translation of domain names to IP addresses. It is a handy tool that can be used to block access to certain websites, redirect websites to a different IP address or set up local development servers. Editing the Hosts file can be useful in various situations, such as blocking adverts, bypassing DNS caching or testing a website before it goes live.

Why would you want to edit the Hosts file?

Editing the Hosts file offers several advantages. Firstly, it can help block adverts and reduce unwanted pop-ups whilst browsing the internet. By adding the domain names of advert servers to the Hosts file and directing them to a non-existent IP address, adverts can be effectively blocked.

Additionally, editing the Hosts file can be useful for bypassing DNS caching. When you visit a website, the IP address of that website is stored in your computer’s DNS cache. If the website subsequently moves to a different IP address, it can take a while before your computer has updated to the new location. By manually adding the website’s IP address to the Hosts file, you can immediately access the new location.

Finally, editing the Hosts file can be useful for setting up local development servers. If, for example, you are developing and testing a website locally before taking it live, you can add the website’s domain to the Hosts file and direct it to your local server’s IP address. This way, you can test the website as if it were already live, without actually having to put it online.

Step 1: Open the Terminal on your Mac

To edit the Hosts file on a Mac, you need to open the Terminal. The Terminal is a command-line interface that allows you to execute various commands on your computer. To open the Terminal, go to the ‘Utilities’ folder within the ‘Applications’ folder and double-click the Terminal icon.

Step 2: Navigate to the Hosts file location

The Hosts file is located in the /etc directory. To navigate to this location, type the following in the Terminal and press Enter:

cd /etc

Then type ls to display a list of files and folders in the /etc directory. You should see the hosts file.

Step 3: Make a backup of the current Hosts file

Before making changes to the Hosts file, it is important to create a backup of the current file. This way, you can always revert to the original state if something goes wrong. To create a backup, type the following in the Terminal:

sudo cp hosts hosts.bak

This creates a copy of the hosts file with the name hosts.bak.

Step 4: Open the Hosts file in a text editor

To edit the Hosts file, you need to open it in a text editor. You can use any text editor you prefer, such as TextEdit, Sublime Text or Atom. To open the Hosts file in TextEdit, type the following in the Terminal:

sudo open -e hosts

This opens the hosts file in TextEdit.

Step 5: Add new entries to the Hosts file

Now you can add new entries to the Hosts file. Each line consists of an IP address followed by one or more domain names, separated by spaces or tabs. For example:

127.0.0.1 localhost
127.0.0.1 example.com
127.0.0.1 www.example.com

In this example, the domain names example.com and www.example.com are directed to the IP address 127.0.0.1, which is the IP address of the local host.

Step 6: Save the changes and close the text editor

After making the desired changes to the Hosts file, you need to save it and close the text editor. In TextEdit, you can save the changes by pressing Command + S or by clicking ‘File’ and then ‘Save’. Then close the text editor.

Step 7: Verify that the changes have taken effect

To verify that the changes to the Hosts file have taken effect, you can try visiting the domain names you added. If the changes have been applied correctly, you should be directed to the IP address you specified in the Hosts file. If the changes do not appear to work, it may help to restart your computer or flush the DNS cache.

Troubleshooting when editing the Hosts file

Various problems can occur when editing the Hosts file. A common issue is that the changes do not appear to work. This can be caused by a typo in the Hosts file, incorrect syntax or a caching problem. To resolve this, you can check whether the entries have been entered correctly, verify the syntax and, if necessary, restart your computer or flush the DNS cache.

Another problem can occur if you do not have permission to edit the hosts file. In this case, you need to ensure that you are logged in as an administrator on your Mac and that you enter the correct password when prompted.

Conclusion

Editing the Hosts file on a Mac can be useful in various situations. It offers advantages such as blocking adverts, bypassing DNS caching and setting up local development servers.

Leon

About the author

Leon

Founder and WordPress Developer

Leon Katz founded WP Agency driven by his passion for WordPress and web development. With over 20 years of experience in the technical sector, he helps…

Learn more about this author →