Search This Blog

Showing posts with label Hosts File. Show all posts
Showing posts with label Hosts File. Show all posts

Wednesday, 5 December 2012

Hosts File:: Using Port Numbers

Today, I just tried adding a host entry, with a custom port number.
Well to my surprise it did not translate it.

If you can ping an ip from the command line, then it can definitely be added as a host entry.
I mean, it can be an existing IP or a non-existent one, but not an invalid IP.

So, I tried to ping the ip with the port number. Gives an error. I tried to ping the IP without the port number.
I could ping it. So it was obvious, that the port number was the probem.


DNS does not understand port numbers. Host entries include DNS entry. Hence, the hosts file would not be able to translate it.
Hosts file translates host names to IP addresses and vice versa. It cannot understand port numbers.

There could be many ways of handling it, as hosts file does not understand port numbers. This depends on the context of the issue.


  1. You can use a router for port forwarding to a new site, then add the router address as a host entry. I'm not sure this is the best way or not.
  2. While deploying your database, you could set up an alias, if the port number change is because of migrating your website and setting up DB on a new server. Setting Up Alias for MS SQL Server. It allows you to specify optional parameters in the Protocol Box, like port number, pipename, connection string.
  3. I'm not sure about above cases would work in all scenarios. They are very specific. We would normally have such cases for our test or development environments. Whats the matter, if you simply add an entry like the one below.
 xxx.xxx.xxx.xxx     YourSite
          When you access it, you type the url 
YourSite/portNumber.


Tip: Hosts File Error:: "Please check if this file is opened in another program"

Editing a host file entry required administrator rights.
The host file can be opened in notepad as a plain text file.

If you directly open the hosts file in notepad, (Without using Run As Administrator), you will encounter the following error.


Run Notepad As Administrator, then open the hosts file in notepad

You can then edit and save your host entries.

Tuesday, 4 December 2012

Hosts File? What is it? Why is it used?

In simple words, 
a hosts file contains mappings of IP addresses to host names.
For example, I would like to say, IP xxx.xxx.xxx.xx should be mapping to http://inspireinnovativelearning.blogspot.com

I keep adding new functionalities to my site. Later, I decide to host my web app on a different server, with a different IP. All I would do, is change my IP address in the hosts file and my web app would be up and running.

Most of the OS's have a hosts file. There's one for Linux, iOS, Unix, Windows Mobile, Android and so on. Complete list can be found in Wikipedia

This is how a host file looks
------------------------------------------------------------------------------------------------------------

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
127.0.0.1       localhost
# 127.0.0.1       ad.doubleclick.net #The IP is non-existent. It will block the site
------------------------------------------------------------------------------------------------------------


The hosts file is loaded into memory at startup. It would first check the hosts file entries before querying the DNS servers.

Now if there is a DNS server with the hostname in the hosts file, the host file entry would override it. So you can as well have a host file entry with the name www.google.com and point it to your custom search app ;)

This of course has ample implications.

  1. For development environments, this could be ideal, where you are restricting the access to a set of users and accessing the site with a custom hostname as specified in the hosts file.
  2. Taken from http://winhelp2002.mvps.org/hosts.htm
  3. You could use it as a tool to block unwanted sites. Eg. Ad sites which slow down the loading of a webpage. A list of such sites can be added into the hosts file. So you don't always need an anti virus to do all the job. A simple entry in the hosts file to a non-existent IP can save you from unwanted sites. There are fully designed hosts files containing entries to block most of the malicious sites. You could google around if you are interested.
There's one thing you can't rule out. If a malicious software, gets hold of the hosts file, it could make changes to your hosts file and point you to a dup site containing malicious content and you would actually not come to know of it.