<< there's no place like 127.0.0.1

When you visit a website, your computer has to look up the IP address for that site so that it knows where to go. Before your computer goes out to find the IP address, it will first check the local hosts file. The hosts file on your computer is a plain text file that contains a map of hostnames to IP addresses.

If you wanted to override the actual IP address of a site, you could ‘trick’ your computer by manually setting the IP address for a website and telling it where to go to find a website. This is extremely common and useful to do when testing a website that you are transferring to a new serer.

Imagine you are moving your website to a new, powerful server. However, you do not want the existing site to go down; you want it to stay up at all times. You can copy the site and all it’s contents to the new server, but how do you test it without changing the DNS? You want to make sure that the site is going to work on the new server without any errors before you start sending traffic over to it. The best method to do this is to change your computer’s hosts file. You can send only your computer to the new server without affecting the live site at all.

The process for modifying the hosts file varies depending on what Operating System you are running. With the help of my good buddy Colin, we have developed a handy utility that will tell you how to modify your hosts file no matter you are running Linux, Mac, or Windows. The following section of this page will detect your OS and print instruction specific to you. Hope you enjoy!

Mac

Editing the hosts file on a Mac is rather easy, especially if you already know how to use a terminal. The commands are similar to Linux and the hosts file itself resembles a Windows hosts file.

Step 1 - Open a Terminal

Either type ‘Terminal’ into Spotlight or you can navigate to Applications » Utilities » Terminal

Step 2 - Open the hosts file

Edit the hosts file by typing in the newly opened Terminal:

sudo nano /private/etc/hosts

Type your main password when it prompts for it.

Step 3 - Edit the hosts file

Your hosts file will already contain comments (lines that start with the pound # symbol), as well as some default hostname entries (e.g. 127.0.0.1 localhost).
Simply add in your custom entry for your domain onto it’s own new line at the bottom. Be sure to add both the ‘www’ version of your domain and the non-‘www’. Example:

64.64.7.186    boomshadow.net    www.boomshadow.net

Step 4 - Save the hosts file

When you are done modifying your hosts file, press Ctrl+O to save the file.
Press enter when it prompts for the filename, and then hit Ctrl+X to exit out of nano.

Step 5 - Flush the DNS cache

On a Mac, you will need to run a quick command to flush the existing DNS cache from your computer. This will allow our host file changes to take effect immediately:

dscacheutil -flushcache

Now load up your site in your browser and you’ll be hitting the new IP location!

Windows

To edit the hosts file on Windows, you will edit it with something simple like notepad. However, you will need to open the file with Administrator rights.

Step 1 - Navigate to Notepad from your ‘Start Menu’

Do no actually click on notepad just yet.

Navigate to: Start (Windows Icon) » All programs » Accessories » Notepad

Step 2 - Open Notepad with Administrator rights

RIGHT click on the Notepad icon and select: ‘Run as administrator’
Hit ‘Yes’ on the pop-up prompt to open the file with Admin priviledges.

Step 3 - Open the hosts file

Hit ‘File’ » ‘Open’, or simply Ctrl+O. Navigate to the hosts file:

c:\windows\system32\drivers\etc\hosts

NOTE: If you do not see any file inside the etc directory, make sure you have Notepad set to look for All Files (*.*). Not just simply txt files.*

Step 4 - Editing the hosts file

Your hosts file will already contain comments (lines that start with the pound # symbol), as well as some default hostname entries (e.g. 127.0.0.1 localhost).
Simply add in your custom entry for your domain onto it’s own new line at the bottom. Be sure to add both the ‘www’ version of your domain and the non-‘www’. Example:

64.64.7.186    boomshadow.net    www.boomshadow.net

Step 5 - Save the hosts file

When you are done modifying your hosts file, press Ctrl+S to save the file. Then you can close notepad.

Step 6 - Flush the DNS cache

Typically, you will want to flush the existing DNS cache from your computer so that your host file changes can take effect immediately.
You will need to get to a command prompt. Open ‘Run’ by hiting the Windows key+R. Now type:

cmd

While still inside the command prompt, flush the DNS cache:

ipconfig -flushdns

Now load up your site in your browser and you’ll be hitting the new IP location!

Linux

Editing the hosts file in Linux is a breeze. You can directly edit the file from a terminal.

Step 1 - Open a Terminal

You’re running linux, so I assume you’re somewhat tech savvy. Load up a terminal and get ready to edit.

Step 2 - Open the hosts file

Edit the hosts file by typing in the newly opened Terminal:

sudo nano /etc/hosts

Type your sudo password when it prompts for it.

Step 3 - Edit the hosts file

Your hosts file will already contain some default hostname entries (e.g. 127.0.0.1 localhost).
Simply add in your custom entry for your domain onto it’s own new line right below the default lines. Be sure to add both the ‘www’ version of your domain and the non-‘www’. Example:

64.64.7.186    boomshadow.net    www.boomshadow.net

Step 4 - Save the hosts file

When you are done modifying your hosts file, press Ctrl+O to save the file.
Press enter when it prompts for the filename, and then hit Ctrl+X to exit out of ‘nano’.

Step 5 - Flush the DNS cache

Typically, you will want to flush the existing DNS cache from your computer so that your host file changes can take effect immediately.
On Ubuntu, you can run:

sudo /etc/init.d/dns-clean start

Now load up your site in your browser and you’ll be hitting the new IP location!

Note about this article

This article is one I had written and shared with the ServInt blog as part of the ‘Tech bench’ series. You can view it on the ServInt blog here. They are using my article with my permission.

Change Log

This post has been updated since it’s original publishing. Here is a list of changes:

  • 09/21/12: Revised Windows instructions to properly open Notepad with Admin rights
  • 05/20/12: Updated OS detection interface

Running Gitit Wiki with Upstart

I know I've been rather quiet lately. I've been busy travelling for a little bit and found myself at a new job. Well, now I'm getting bac...… Continue reading

Installing ImageMagick & PHP Imagick

Published on March 26, 2015