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.

@Jacob – yeah, thanks. In the end, I put the password, it was much easier solution :)
@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
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?
@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.
There is also “Gas Mask” for the Mac http://code.google.com/p/gmask/ which makes editing your hosts file a piece of cake.
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.
Nice Trick!
Thank you for sharing this very useful , specially for people non techie like me!
[...] 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, [...]
@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. :)
Can I assume these directions are the same if I’m running multiple sites on my server via vhosts?