How-To

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.

Windows

Editing the hosts file in Windows can all be done in Notepad.

1. Open a Run prompt

Press the Windows key + ‘R’.

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.

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

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.

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.

Mac

Use Terminal to edit the hosts file on a Mac. The commands are similar to Linux and the hosts file itself resembles a Windows hosts file.

1. Open a Terminal window

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

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.

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

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’.

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.

Linux

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

1. Open a Terminal

2. Open the hosts file

Edit the hosts file by typing the following command:

sudo nano /etc/hosts

Type your sudo password when prompted.

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

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’.

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 the 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.

Find out more about ServInt solutions

Starting at $25

Comments
  1. @Jacob - yeah, thanks. In the end, I put the password, it was much easier solution :)
  2. @Nikola, I had the same problem on my Mac. In order to use 'sudo' you must have a non-blank password for you current user. Its highly recommended for security reasons anyway. See Apple docs here: http://support.apple.com/kb/HT4103 The alternative is that you could enable the root user on your Mac. Then you could 'su' to root in order to run commands. See Apple's support on how to set that up: http://support.apple.com/kb/ht1528
    Jacob "Boom Shadow" Tirey /
  3. I don't have a password on my Mac (not saying that I don't know it - I don't have it, it's just blank space). And when in Terminal prompts the password - I just press Return and nothing happens. Not seeing any default hostname enteries or anything. Does anyone else has that kind of problem - any ideas how to solve it?
  4. @Fotis Evangelou, that is a pretty cool program for Mac. I personally don't typically need to make heavy modifications to my hosts file. But I could definitely see the advantage to being able to maintain multiple versions of the hosts file. Nice recommendation. Thanks.
    Jacob "Boom Shadow" Tirey /
  5. There is also "Gas Mask" for the Mac http://code.google.com/p/gmask/ which makes editing your hosts file a piece of cake.
    Fotis Evangelou /
  6. That's right, Marc. You can add multiples domains in your hosts file. Example: 64.64.7.186 boomshadow.net 216.87.58.131 domain1.com 199.34.59.201 domain2.com Please note that you need to delete these entries once DNS changes have been made, otherwise you'll be living in a parallel universe.
  7. Nice Trick! Thank you for sharing this very useful , specially for people non techie like me!
    Octa Rendra /
  8. [...] written for the ServInt blog as part of the ‘Tech bench’ series. You can view it on the ServInt blog here. They are using the utility with my permission. Related Posts:No Related Posts dns, linux, [...]
    Hosts file modification - Linux, Mac, or Windows | BoomShadow.net /
  9. @Marc, that's right, these directions are the same regardless of your server and web service configuration. It is actually a DNS level change. You could be running a Windows or Linux server, Apache or Nginx service, and these instructions would be the same. :)
    Jacob "Boom Shadow" Tirey /
  10. Can I assume these directions are the same if I'm running multiple sites on my server via vhosts?
  • Hosting Advice
  • The New York Times
  • The Hill
  • Bloomberg
  • The Seattle Times
  • Computer World
  • Ars Technica
  • MSNBC

To engage with the ServInt Sales Team use the following chat icon. Normal sales hours are Monday-Friday 9am-5pm EST but feel free to leave a message and we will follow up as soon as possible.

Sales Chat



To engage with the ServInt Support Team you must be logged into our Customer Portal for identity verification and have a ticket opened about your request or there will only be limited support offered.

Support Chat

CLOSE