Web

Moving web servers and hosts file

For whatever reason there may come a time when you need to change web hosts for a site. Its not a good look for the world see a broken site due to differences between the old and new. So how do you test everything is set up correctly at the new host before updating nameservers?
When you type a site into the browser location bar, a DNS lookup goes on and the typed in domain name is resolved to an IP address and a known location. the requested site is returned to you. So how do you test a domain that is to be hosted at a new location? In the scenario where you do not have a dedicated IP address, the answer is the hosts file on your local PC.
Your local hosts file is checked before any DNS lookup, so adding a domain and the new IP will cause your browser to go to the new location not the old.
So what does a hosts file look like? In the case of Windows XP, something like below, ignoring the comment lines which start with a #. In this case localhost will resolve to the IP address of 127.0.1 which is your local machine.

127.0.0.1       localhost

Hosts files for other OS are much the same. Ubuntu has entries for the local machine name and IPv6 setup in its default hosts file.

127.0.0.1 localhost
127.0.1.1 mymachine.local mymachine

# The following lines are desirable for IPv6 capable hosts'
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Now if you wished to have www.mysite.com resolve to IP address 192.168.1.2 you would add the following line into the hosts file. As simple as that. It’s always a good idea to back up the existing hosts file.

192.168.1.2     mysite.com

So where is your hosts file? Obviously this varies by operating system. The table below will cover the standard location for common operating systems.

OS Location
Win XP, Vista, 7 & 2003 (32 bit) C:WINDOWSsystem32driversetchosts
Windows 64 bit C:WINDOWSSysWOW64driversetchosts
Mac OS X /private/etc/hosts
Linux distributions /etc/hosts

These lookup entries are often cached, so you will need to either restart networking or even reboot your machine for the new IP address to be used.
Once you have finished your move & testing, and updated the real nameservers for your new location, its always best to remove these temporary lines from the hosts file. That way you find the site, the same way everybody else does, and you may find out early if there are any problems.

Standard