Hosts file modification

April 5, 2012 — 3 Comments


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 system. 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!

Unfortunately, we can't detect your OS.



Perhaps you came here to lookup something for your computer real quick.
Simply click on the OS you need below.

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!

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 NOT 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!

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!

Note about this article

This utility is one I had written for 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:

  • 09/21/12 | Revised Windows instructions to properly open Notepad with Admin rights

  • 05/20/12 | Updated OS detection interface

Jacob "Boom Shadow" Tirey

Posts Twitter

A linux web hosting administrator, a professional production sound man, and a renegade cop without nothing left to lose.... Ok, that last part is made up. In all seriousness, my passion in life is to help people; whether that be with help running their sites or with their productions. The name 'Boom Shadow' was given to me by a great group of filmmakers called Star Wipe Films. back in 2005 and has been with me since. I hope my site is helpful to you, and if there's something you need, drop me a line!
  • http://www.facebook.com/jennifer.lubyhoward Jennifer Luby Howard

    Very helpful, thank you!  How do you reverse this once you don’t need it anymore?

    • http://boomshadow.net Jacob (Boom Shadow)

      I’m glad to help Jennifer. To reverse the changes, simply re-edit your hosts file, remove the lines you had added in (or you can comment them out with a ‘#’), and then re-save the file. Finally, flush your DNS cache again (Step 5).

  • veeresha

    Thank you ………………………….usefull