Sales:  800-573-7846 or Live Chat

The ServInt Source

The Tech Bench: Hosts File Modification

How do I view my site on a new server without letting the public see it?

When migrating your website to a new web host, it can be helpful to see the site up on the new server before it goes live. 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. You can copy the site and all your content to the new server, but how do you test the new setup without changing the DNS (and having the whole world see your site on the new server before it’s ready for prime time)?

The best way to do this is to change your personal computer’s hosts file. 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 onto the Internet to find the IP address, it will first check its own local hosts file. The hosts file on your computer is a plain text file that contains a map of hostnames to IP addresses.

By changing the hosts file on your personal computer, you can send only your computer to the new server without affecting the live site at all. In essence, you are “tricking” your computer by manually setting the IP address for a particular website and telling it where to go instead.

The process for modifying the hosts file varies depending on what operating system you are running. The following utility has already checked which OS you are running (Mac, Windows or Linux) and has given you instructions on how to change your hosts file. Note that if you are using an older Mac, the instructions are good for OSX 10.5 and higher.

I am unsure of what OS you are using.

I'll simply print the three instructions I have for Windows, Mac and Linux.

Editing the hosts file on in Windows is easy. It can all be done in Notepad.

Step 1 - Open a Run prompt

Press the Windows key + 'R'.

Step 2 - Open the hosts file with Notepad

In your newly opened Run prompt, type the following to have notepad open the system's hosts file:

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

Hit 'OK' to open the file with Administrative priviledges.

Step 3 - 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 its own new line at the bottom. For example:

64.64.7.186    boomshadow.net

Step 4 - 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 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.

You will need to get to a command prompt. Open 'Run' as in Step 1 and type:

cmd

Now, inside the command prompt, flush the DNS cache by typing:

ipconfig -flushdns

Click "OK"

Now load your site in your browser and you'll be hitting the new IP location.

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

Step 1 - Open a Terminal window

Either type 'Terminal' into Spotlight or you can navigate to Applications >> Utilities >> Terminal.

Step 2 - Open the hosts file

Open the hosts file in an editor by typing in the following in the newly opened Terminal window:

sudo nano /private/etc/hosts

Type your Mac computer password when prompted.

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 its own new line at the bottom. For example:

64.64.7.186    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 prompted 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. In the same Terminal window type:

dscacheutil -flushcache

Simply quit Terminal to exit.

Now load your site in your browser and you'll be hitting the new IP location.

Editing the hosts file in Linux is easy. You can edit the file directly on the command line.

Step 1 - Open a Terminal

Step 2 - Open the hosts file

Edit the hosts file by typing the following command:

sudo nano /etc/hosts

Type your sudo password when prompted.

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 its own new line right below the default lines. For example:

64.64.7.186    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 prompted 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 your site in your browser and you'll be hitting the new IP location.

To reverse this process, simply follow the instructions again and remove the line you added to the hosts file.

Important Note: While these directions are reliable, a little knowledge can be a dangerous thing. Do your research and get answers to any questions before editing any program files on your computer. The suits upstairs want me to remind you that we are not responsible any misuse of these instructions.

And as always, if you’re a ServInt customer and have any questions or are unsure of what you are doing, don’t hesitate to open up a support ticket in your customer portal. We’re happy to help.


The Tech Bench is an ongoing blog series featuring the answers to common questions the ServInt MST fields everyday. You can also find more great tech tips in the ServInt KnowledgeBase.

About Jacob Tirey

Jacob Tirey is an IT professional and former member of ServInt's Managed Services team.


The ServInt Source | Web Hosting Blog