Full Version: the Hosts file on Linux systems
Dream.In.Code > Programming Tutorials > Linux Tutorials
no2pencil
Setting up your Hosts file:

Where this file comes from:
Back in the days of ARPANet each computer had a file which mapped out the location of the other computers. As time went on, more & more computers were added to this list. With the list growing came the possibility for other machines to become out of date. This is where DNS came in. However, we can still make use of this file & method today!

On your Linux system, you'll find this file in /etc/hosts. The basic hosts file contain at least the following:
CODE

127.0.0.1       localhost


This allows you to communicate with 127.0.0.1 with the word 'localhost'. Go ahead & try it out for yourself!
$ping localhost

Any entry within the hosts file with a number sign (#) is considered a comment.

CODE

# Local Host
127.0.0.1       localhost


The order to the hosts file is :
QUOTE

<IP Address> <host name> <alias>


So if you wanted to setup an internal webserver, & you wanted to access it using example, you could enter the following:


CODE

# Local Host
127.0.0.1       localhost

# example web server
192.168.0.101 example


This will allow you to resolve example, to the given ip address.

CODE

# nslookup example
Server:         192.168.0.2
Address:        192.168.0.2#53

Name:   example
Address: 192.168.0.101


The main drawback to using this method is you must 1st setup the ip addresses on each of the machines. Since DHCP stands the possibility of reassigning a new ip, this would cause your hosts file to become out of date.

For more information :
http://en.wikipedia.org/wiki/Hosts_file
http://www.linuxfromscratch.org/lfs/view/s...er07/hosts.html
Tom9729
You can also use your hosts file to block bad domains.

For example:
CODE

127.0.0.1 microsoft.com
no2pencil
But before that can block the domains, you will have to clear your current DNS cache list.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.