From time to time you may find that you need to view a website that is hosted locally.
For instance, when conducting page load speed tests for a client, we first created a copy of their website on our servers to test the differences in the hosting resources between the two servers to determine if changing hosting providers might positively increase their site’s overall speed.
The steps below outline how to change your hosts file in order to view a website located on a different server than the live version.
Below is a quick reference on how to go about editing your ETC / Hosts file in Terminal (Mac):
1. Cmd+spacebar to open Spotlight and search for Terminal
2. Open Terminal and to open the hosts file, enter the following command:
sudo nano /private/etc/hosts
3. Terminal will now ask you to enter your Administrator Password:
Enter Password and then press Return
4. The hosts file will display in the Terminal window:
To move throughout the hosts file, use your arrow keys to move up and down. The lines starting with the hash (#) symbol are comments and are not to be edited. A couple of default mappings will also appear in the window, such as the entry 127.0.0.1 for localhost.
5. Use your Arrow Keys to move the cursor to a blank space at the bottom of the hosts file, and enter your new mapping here.
In the following example, I am adding an entry to display a copy of a website residing on my local server, rather than where the DNS is pointing.
6. To save the hosts file after editing the entries, press Ctrl-o. This will bring up a prompt for the file name to write to:
7. Save the hosts file by pressing Return. Then exit the hosts file in Terminal by pressing Ctrl-x. You will now return to the main Terminal prompt.
8. For the new settings to take effect you will have to flush your DNS cache. To accomplish this, enter the following command at the Terminal prompt:
Press Return. You can now close the Terminal window and your edits to the host mappings will take effect.