Wednesday, November 11, 2009

/etc/hosts file

File /etc/hosts is a configuration file you will find in almost any operating system including for example Windows. It is a very basic file for configuring the network. This one is used for locally mapping hostnames to IP addresses. The system will always prefer to use /etc/hosts file, rather than any DNS server. So if you have a record with an IP address in your /etc/hosts the system will use this one, if you try to access the hostname you specify.

The syntax is really easy. First you specify an IP address of the host and then all aliases divided by a space. You may specify unlimited number of aliases. So your /etc/host file may look like this:

127.0.0.1 localhost my-computer
10.0.0.1 server second-computer

 Once you save this file you may try a ping to second-computer or server and it will go to 10.0.0.1. The changes you made will be applied immediately once you save the file. There's no need to restart any service or run some program.

There are some limitations for the alias format. It may contain any alphanumeric character,  minus sign ("-") and a dot (".") but these two must not be located on the beginning or at the end of the alias.

You may also find this file useful, when you would like to block to some server with for example fraudulent content. If you'd like to block access to for example badsite.com you can add this line:

0.0.0.0 badsite.com

This will resolve badsite.com as 0.0.0.0, which doesn't lead anywhere.

No comments:

Post a Comment