Desktop | Mobile
Sunday, August 10, 2008, 05:21 AM JST
Posted by Yanto Suryono
When you access a host on the internet, usually you access them using its human friendly name, such as tech.yanto-flora.net. However, the actual access is done using what is called the host's IP address. Normally, a DNS will be responsible to translate the human friendly name into its corresponding IP address, but most OS have a local file for name translation which has precedence over DNS.

For windows OS, the local file is located at %WINDOWS%system32\drivers\etc\hosts while for UNIX (including Linux), the file is usually located at /etc/hosts.

The file listed IP addresses and its corresponding human friendly host name, one host per line, and at least the following line will be present:

127.0.0.1 localhost

This line allows you to access your local host using the name "localhost" which points to the IP address 127.0.0.1 (self loopback).

Now, what we can do with this file ? Usually we do not need to edit this file, as normally we use DNS to do name translations but below are three cases which I find this file to be quite useful:


  1. Local Web Development

  2. If you are a web developer, quite often you want to test your pages on your local machine first before making it available to public on the internet. If your page is quite complex one which dynamically checks for the host name from which it is being accessed, then you can not verify its output without really accessing it using the method it is expecting. This check is quite common if you host a lot of subdomains which actually point to the same server, but you want the page to be accessed from a particular subdomain only. In this case, we can add a line in the host file like below:

    127.0.0.1 subdomain.localhost

    Now, if you access http://subdomain.localhost you will be accessing your local host but of course you should configure your local web server to be able to receive requests to subdomain.localhost. If you want you even can put your real hostname too:

    127.0.0.1 subdomain.domain

    Then all accesses to subdomain.domain will be directed to your local host since this file will be consulted first before the DNS. Be careful though to remove this line once you are finished so that you will be accessing the actual server after that

  3. Preventing Accesses to Certain Sites

  4. This trick is used by many anti spyware tools to forcefully direct accesses to dangerous/phishing/fraud sites to a certain host, normally 127.0.0.1, by putting:

    127.0.0.1 dangerous.site.hostname

    This way, you are protected from mistakenly accessing that dangerous site and if there is any spyware accessing that site, it is blocked too.

  5. Quick Check when You Changed Your Hosting Company

  6. Suppose you have a web site hosted at a certain web hosting company and you decided to change the hosting company for any reason. Beside moving the web content to the new hosting company, you normally will have to change the DNS to point to the new hosting company too. However, the new DNS settings need time to propagate and it will take from several hours to days until the whole world is fully synchronized to the new DNS.

    Then, how are you supposed to check if your site is up and running without problems on the new hosting company if your DNS has not been synchronized yet ? Every attempt to access the site will end up at site on the old hosting company. Certainly you can not wait several hours or days to see that your site actually appears crippled. Most hosting company will provide you with a temporary access URL for this checking purpose,
    but once again, if your site relies on checks on how it is being accessed, then you have a problem here.

    You can then temporarily put the following in your host file:

    <new hosting IP> yoursitesubdomain.yoursitedomain

    Of course you need to know the IP address of the new server your site is being hosted though (either ask your hosting company or find out yourself).


Leave a Comment

Fields with * are required.