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:
- web site testing during domain name delegation process;
- checking a proper work after migrating to a new host.
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 :
- find the hosts file on your computer. Normally it is located in Windows drivers folder. You can press a Start button and type in a search box C:\Windows\System32\drivers\etc or just use a File Explorer navigation;
- when prompted for program to open a file with choose Notepad or Wordpad (using an option “Run as Administrator” may be an answer, if there are any problems with opening a file). You can see a resulting screen on the Picture #1;
- to edit the hosts file you just have to write down the pairs of names and IP-addresses below the existing text. Example at the Picture #2;
- replace the IP-address from the example above with the one you can find in your control panel or emails from ISP. And give your domain name instead of the “example.com”.
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.