How to Update Hosts File (Mac and PC) – A Guide for eCommerce Teams

Hello my internet friends. Has some pesky dev has once again asked you to alter your hosts file in order to view your staging site, or pre-production, or a new build for that site, etc… again? And you have plum forgotten how to do it. Well never fear, your internet wizards are here.

First off…

What is a Hosts File?

Definition:
Your computer has a file that maps special websites to IP addresses. When you visit a site, it first checks this file to see if you have an entry for that site. It is a plain text file.

Basic Explanation

Example: You want to hit www.demacmedia.com but want to see the special, not user-ready, rebuild of this site instead. Normally you would just type in the URL in your browser and be done with it, but since the new site with that URL isn’t on the “internet” proper, your browser will just go to the old one. But you don’t want this garbage old site, you want to see the new awesome site!

What changing the hosts file means is that instead of querying the “internet” for where the site is hosted (nerds, please don’t hatemail me on this, I know it queries the DNS, etc…), your computer looks at the hosts file first. It then points the browser at the specific IP address you have listed there and then sends that URL to that IP address. Assuming that everything was set up correctly your browser should now be able to see the new site that is hosted at that specific IP address with that URL, not the commonly available one on the internet.

This also holds for sites that have not been published yet, but exist only on that IP to people who know how to look for it.

Below are instructions for both Windows and Mac users on how to alter the hosts file.

Windows 8-10

  1. Navigate to your windows icon, select it and type in notepad as seen in the image below

update hosts file 1

  1. Right click notepad (or Notepad++ which I prefer over Notepad) and select “Run as administrator” as seen in the image below. This is important because the hosts file is a restricted file of sorts; its only allowed to be modified by the computer administrator.

update hosts file 2

  1. You then go to File -> Open go to the following Path
    1. c:\\Windows\System32\drivers\etc\
    2. Make sure, next to filename, that you have “All Files” selected in the drop down and not just “*.txt” otherwise you won’t be able to find the file
    3. Select the “hosts” file
    4. See image below

update hosts file 3

  1. Enter in the host entry
    1. This comes in a couple of formats but follows the same basic pattern
      1. [IP addresss] [URL minus the “http” and “www”] eg: 127.0.0.1 devsbecrazy.com
      2. [IP address] [many URLs that you want linked to this IP address that are related including the “www” depending on how the server was set up]
        1. Eg: 127.0.0.1 devsbecrazy.com www.devsbecrazy.com staging.devsbecrazy.com www.staging.devsbecrazy.com web1-staging-devs.com www.davsbecrazy.uk etc…
    2. If you don’t know what the host entry is and someone has asked you to change yours, ask them for the IP address and the URL they want linked to that IP address and use the first format
  1. Save changes to the file and exit the program

NOTE: The instructions for Windows 7 are almost identical, there is just an extra step at Step 2 where you click Continue as Windows will prompt you to be sure you know what you are doing

Mac OSX – 2 Ways:

Way 1 (Easiest and, for my money, Best):

  1. Install a program called Gas Mask (this program is awesome and will save you a lot of time and energy)
  2. Open Gas Mask
  3. You can now make new hosts files without changing the original (you can have host files for staging, for production, etc…) and you can switch between your files, not worrying about having one canonical source.
  4. Save host files and tell gas mask which file to use via the hosts file profile which is available through the icon gas mask in the menu bar
  5. For more information see http://geekussion.com/mac-os-x/gas-mask-mac-80/

Way 2

  1. Open Finder window (little smiling two-toned face in the dock)

update hosts file 4

  1. Once the window is open go to the menu option Go and select “Go To Folder…”
    update hosts file 5
  2. Type in /private/etc/hosts
    update hosts file 6
  3. It will open the finder window at that location. You then select the hosts file, right click it, and open with the text editor of your choice

update hosts file 7

  1. Change the host entry and save the file (the host entries follow the same pattern as in the windows instructions above, which I will list again below)
    1. This comes in a couple of formats but follows the same basic pattern
      1. [IP addresss] [URL minus the “http” and “www”] eg: 127.0.0.1 devsbecrazy.com
      2. [IP address] [many URLs that you want linked to this IP address that are related including the “www” depending on how the server was set up]
        1. Eg: 127.0.0.1 devsbecrazy.com www.devsbecrazy.com staging.devsbecrazy.com www.staging.devsbecrazy.com web1-staging-devs.com www.davsbecrazy.uk etc…
    2. If you don’t know what the host entry is and someone has asked you to change yours, ask them for the IP address and the URL they want linked to that IP address and use the first format