Saturday, February 6, 2010

DNS Host Files

One of the most powerful tools available to somebody working on their site during a migration is their computer’s ‘hosts’ file. This file is especially useful when migrating a site between two servers, or any other time when DNS might not be working properly.

Lets start with an example of the the information that would be found in a typical hosts file:

127.0.0.1 localhost

That is all you will generally see in a clean hosts file. Its layout is tab or space delimited. The first field in the line is the IP address that you would like the site to resolve to, and any additional fields after that should be domain names. For example, if I wanted to reroute example.com to some other location I might put a line in my hosts file that looks something like this:

123.123.123.123 example.com www.example.com

As you can see, this will redirect both example.com and www.example.com to the IP address of 123.123.123.123. When moving a site to a new server, you can use this file to test out your sites to ensure that they work after the move, while still leaving your original site online. Simply point the desired IP address at the domain name in question and you computer will connect to the new IP whenever you type in the domain name.

This file is located in a few different places depending on your operating system. The file name is ‘hosts’ no matter what operating system you have.
Windows

On Windows XP/2003/Vista/7 this file is located in:

%SystemRoot%\system32\drivers\etc\
(Typically C:\Windows\System32\drivers\etc)

With Vista/Windows 7 you will need to edit this file with administrative privileges. You can do this by following one of these two sets of intructions:

1. Click on the Windows logo.
2. Search for ‘notepad’
3. Right click on the notepad launcher and select ‘Run As Administrator’
4. Click File, then Open
5. Browse to the folder mentioned above and select the hosts file

OR

1. Click on the Windows logo.
2. Click on Search.
3. Search for the word “hosts” (Please be advised this search can take several minutes to complete)
4. Double click on the hosts file and choose notepad as the program to open the file once prompted

MacOS

On Mac OS X, your hosts file is located here:

/private/etc/hosts

You will need administrative privileges to edit this file.
Linux

On Linux, you can find the hosts file here:

/etc/hosts

Depending on the distribution you may need administrative privileges to edit this file.
Please remember to revert the hosts file to its original state after the migration is complete.

Feel free to link to this post or write a comment if it has solved your problem. This way other people are more likely to find it :)  

No comments:

Post a Comment