If you’re running a local development server (which you should if you are developing for WordPress) and you are on a Mac, it can be a very useful skill to know how to edit your hosts file on a Mac. I don’t do it all that often, so I thought that I’d share it here so that when I inevitably forget how to do it again, I will be able to find the steps again quickly. Also, if you are looking for how to do it, you can be helped as well.
(If you’re just looking for the steps and don’t need an explanation, feel free to skip to the bottom.)
Why Do You Need to Know How to Edit Your Hosts File on a Mac?
If I’m testing some development stuff on a website that is also in production, I want to make a local version in case I mess something up. That way, my site won’t be down until I get it fixed. Also, some changes can take some time to implement fully, so instead of having a public site in various states of disrepair until I get the change fully implemented, I can make the changes on a local site and then move everything up to the public server.
The issue is that when I’m working on the local site, I have to either change the URL of the local site to something like http://localhost/ or change my hosts file so that my computer looks at a different address for my website than the address of the production server. If I just change the URL, that can cause problems when I go to move the site back to production server (also some plugins or other site features might not work correctly if I’m not using the correct site name). So, updating the hosts file will point my computer to the address I specify when I put in a certain URL in my browser instead of the address it would normally get from any other DNS server.
Can You Do This on a Windows/Linux/Something Else Computer?
Yes, but the way you make the change will be different. I develop mostly from a Mac, so I’m writing this article about How to Edit Your Hosts File on a Mac. If you have a different OS, please find the appropriate instructions somewhere else.
Steps to Editing Your Hosts File
- Open the Mac’s Terminal
You may either type Terminal in the Spotlight, or go to Applications -> Utilities -> Terminal. - Open the hosts file for editing
In the terminal window you just opened copy/paste the command string below, and press return.
sudo nano /private/etc/hosts
You’ll be prompted to enter your Mac user’s password. NOTE: you won’t see the cursor move. This is normal, so press return when done. - Edit the hosts file
Add these two lines to the bottom of the file:
SERVER_IP_ADDRESS domain.com
SERVER_IP_ADDRESS www.domain.com
For example, it should look like:
67.199.146.25 domain.com
67.199.146.25 www.domain.com
Make sure that there is a space between the IP and the URL! Otherwise, the rule will not work.
Be sure to place domain.com with your actual domain name. The IP address will be the localhost address (usually something like 127.0.0.1) or the address of the virtual machine if you are running your local server that way. - Save your changes
Presscontrol-o
on your keyboard, then return to accept the filename. Exit the editor by pressingcontrol-x
. This takes you back to the terminal screen. - Flush your Mac’s DNS cache
If you’re on macOS Sierra (10.12.x), you can flush your DNS cache like this:
sudo killall -HUP mDNSResponder
If you’re on a different version, the command will be different.Congratulations! You now have a modified hosts file that will point your browser to the IP address of your choosing when you put in the specified domain name.
How to Change Back
When you are done working on the site you might want to change this setting back so that you can see the actual site. To do this, simply reopen the hosts file from the terminal, like you did to edit it in the first place. Then either delete the lines you added (you can do this quickly by moving the cursor to the line and pressing
control+k
) or simply insert a#
at the beginning. (For those of you who don’t know, this is called “commenting out” the line.) Once you’ve edited the file to your liking, follow steps 4 and 5 above to finish up.
Leave a Reply