The Hosts file is just a list of IP addresses and corresponding server names. The server typically checks this file before querying DNS. If a name with a corresponding IP address is found, DNS is not queried at all. Unfortunately, if the IP address of the host changes, you must also update the file. This is not a big problem for a single machine, but it's tough to update the entire company. For ease of administration, it is usually in the file to place only the loopback interface and the local machine name records, and then use the centralized http://www.aliyun.com/zixun/aggregation/33842.html ">dns server to process the rest." Sometimes you may not be able to control the DNS server, in which case it is much simpler to add a record to the hosts file until it joins the centralized DNS.
192.168.1.101 Smallfry
In the example above, the server Smallfry IP address is 192.168.1.101. You can use Smallfry to replace 192.168.1.101 in ping,telnet or other network related programs. The following example uses the ping command to see if Smallfry exists on the network:
[Root@bigboy tmp]# Ping smallfryping Zero (192.168.1.101) bytes of data.64 bytes from Smallfry (192.168.1.101): Icmp_ seq=0 ttl=64 time=0.197 ms bytes from Smallfry (192.168.1.101): icmp_seq=1 ttl=64 time=0.047 ms---smallfry Ping Statistics---2 packets transmitted, 2 received, vs packet, loss, time 2017ms Rttmin/avg/max/mdev = 0.034/0.092/0.197/0.074 MS, pipe 2[root@bigboy tmp]#
You can also add aliases to the line and associate other names with your server. Here we set up Smallfry can also be accessed using tiny and littleguy.
192.168.1.101 Smallfry Tiny Littleguy
You should never reuse the same IP address in a file because Linux uses only the first record value it finds.
192.168.1.101 Smallfry # (wrong) 192.168.1.101 Tiny # (wrong) 192.168.1.101 Littleguy # (wrong)
LocalHost inside the loopback interface
Usually the first record in hosts defines the IP address of the server virtual loopback interface. It is usually mapped to name Localhost.localdomain (instead of the server's own generic name) and localhost (short alias). By default, Fedora plugs the host name between 127.0.0.1 and localhost:
127.0.0.1 bigboy localhost.localdomain localhost
When the server is connected to the Internet, the 127.0.0.1 of the first record should be the FQDN name of the server (fully qualified domain name). For example, bigboy.mysite.com:
127.0.0.1 bigboy.my-site.com localhost.localdomain localhost
Some programs, such as SendMail, are very sensitive, and if you feel that an incorrect FQDN name is detected, it defaults to using Localhost.localdomain to contact other servers on the network. This can cause confusion because other servers also consider themselves Localhost.localdomain.
Note: You must always have a pair of localhost and localhost. Localdomain maps to records on 127.0.0.1 to make Linux work properly and safely.