Let's start from the very beginning – what is hosts file at all? I'm sure there is no need to tell you in details what Internet Protocol addresses are (IP-addresses or IP's for short) and why do we need Domain Name System (DNS). Information on this subject is easy to find and you have already heard something about it. Let's only highlight the main points.
Basically IP-address points to the precise location of a particular computer in web. And a domain name is kind of alias (convenient for memorizing) for the IP-address.
While numeric IP is hard to remember we need to have some table, where mapping between domain names and IP-addresses is stored. Actually DNS is such a worldwide table or better to say a huge distributed database managing system. And every time you go on line your browser first asks DNS for a desired IP-address.
Before DNS has appeared every computer linked to the global web had a text file, where IP's and corresponding hostnames were written. This file is actually the hosts file itself. You can still find it among your OS' files. And furthermore, hosts file has a higher priority for your computer than DNS, that means your browser checks the hosts file first for IP-address of the computer you need.
There are several situations, when changing the hosts file can be useful:
The thing is DNS acts like a really huge database with lots of nodes, so it takes some considerable amount of time (normally up to 48 hours) to update information about your host, so every user in the world could reach it.
But there has to be the way to use your website even during this period. When you are dealing with web hosting migration some providers can give you a possibility to use temporary URL to access your host. But this option is insecure and what else is important – some features on your site may work not exactly the way you’ve planed (e.g. there can be some problems with htaccess configuration file).
On the other hand, editing the hosts file is an easy and reliable option. Here is an example for Windows OS, just follow the steps described below :
If you have Mac or Unix OS all the actions should be almost the same. You can find the hosts file in "/etc/hosts". To edit the file open your terminal and input "sudo nano /etc/hosts" with administrator password. Go through the steps described above to edit the file and press Ctrl+O to save it.
Now you’re all set and able to connect to your webserver and test it! One more thing – don’t forget to delete the lines you have added (or just comment them out by placing # at the beginning) after some time.