Scritto da Arduino Di Giosia il 23/08/2008

Il file hosts è il file che viene usato per configurare l’interfaccia di loopback, il cui scopo configurare l’indirizzo ip della macchina stessa e per risolvere gli indirizzi interni. É presente in tutti (o almeno dovrebbe) i sistemi operativi che fanno uso di protocollo tcp/ip e potete utilizzarlo per risolvere (senza passare dal DNS) anche indirizzi esterni.

Ma in pratica come funziona? Preso per assunto che ogni computer in rete deve avere un indirizzo IP univioco, quando scrivete “www.pippo.it” nel vostro browser, parte una richiesta al sistema operativo che deve tradurre il nome in un indirizzo ip che corrisponde al server su cui si trova il sito che cercate. Per farlo il Sistema Operativo per prima cosa esamina il file hosts appunto, se non trova il nome nella sua lista (e questo succede nel 99,99% dei casi) passa la richiesta al server DNS. Noi interveniamo proprio qui, prima della richiesta al server DNS e traduciamo il nome in un indirizzo.

Per modificarlo per prima cosa dobbiamo trovarlo… Negli ambienti unix (quindi anche linux/bsd/osx) lo trovate in “/etc/hosts”, windows invece, lo custodisce in “c:/windows/sistem32/drivers/etc/hosts”

Cominciamo da Windows, il file originale dovrebbe essete più o meno questo:

# Copyright (c) 1993-2006 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

127.0.0.1       localhost
::1             localhost

Se l’indirizzo ip del server web dove si trova www.pippo.it è 192.168.0.3 basta modificare

127.0.0.1       localhost
::1             localhost

in

127.0.0.1       localhost
192.168.0.3 www.pippo.it www.altro.it
192.168.0.4 www.altroserver.it
123.45.67.89 www.ancheesterni.it
::1             localhost

Potete aggiungere tutti gli ip che volete andando a capo, e ad ogni ip in nomi quanti e quali volete, separati da uno spazio

In osX (10.5-leopard) invece il file hosts è più o meno questo:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost

::1             localhost
fe80::1%lo0     localhost

Potete modificarlo così:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost

192.168.0.3 www.pippo.it www.altro.it
192.168.0.4 www.altroserver.it
123.45.67.89 www.ancheesterni.it

::1             localhost
fe80::1%lo0     localhost

In fine un esempio di file hosts per linux Ubuntu originale:

127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

e modificato:

127.0.0.1 localhost

192.168.0.3 www.pippo.it www.altro.it
192.168.0.4 www.altroserver.it
123.45.67.89 www.ancheesterni.it

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts