As your machine gets started, it will need to know the mapping of some hostnames to IP addresses before DNS can be referenced. This mapping is kept in the /etc/hosts file. In the absence of a name server, any network program on your system consults this file to determine the IP address that corresponds to a host name.
The hosts file is a computer file used by an operating system to map hostnames to IP addresses.
The hosts file is a plain text file, and is conventionally named hosts. Originally, a file named HOSTS.TXT was manually maintained and made available via file sharing by Stanford Research Institute for the ARPANET membership,
containing the hostnames and address of hosts as contributed for inclusion by member organizations. The Domain Name System, first described in 1983 and implemented in 1984, automated the publication process and provided instantaneous and dynamic hostname resolution in the rapidly growing network. In modern operating systems, the hosts file remains an alternative name resolution mechanism,
configurable often as part of facilities such as the Name Service Switch as either the primary method or as a fallback method.
Purpose
The hosts file is one of several system facilities that assists in addressing network nodes in a computer network.
It is a common part of an operating system's Internet Protocol (IP) implementation, and serves the function of translating human-friendly hostnames into numeric protocol addresses, called IP addresses, that identify and locate a host in an IP network.
In some operating systems, the contents of the hosts file is used preferentially to other name resolution methods, such as the Domain Name System (DNS), but many systems implement name service switches, e.g.,
nsswitch.conf for Linux and Unix, to provide customization. Unlike remote DNS resolvers, the hosts file is under the direct control of the local computer's administrator.
File content
The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more host names. Each field is separated by white space tabs are often preferred for historical reasons, but spaces are also used.
Comment lines may be included; they are indicated by a hash character (#) in the first position of such lines. Entirely blank lines in the file are ignored. For example, a typical hosts file may contain the following:
127.0.0.1 localhost loopback
::1 localhost
This example only contains entries for the loopback addresses of the system and their host names, a typical default content of the hosts file.
The example illustrates that an IP address may have multiple host names (localhost and loopback), and that a host name may be mapped to both IPv4 and IPv6 IP addresses.
etc-host Example consisting of IP Address and DNS
This is the reference to the local machine. 127.0.0.1 is always reserved for the loopback or lo interface
This line is a reference to the local machine, using its network address.
This refers to another machine on the same network. Now you refer to host2 instead of its IP address.
This is an alias for the gateway on your network.
This is a machine on a different network; note the difference in the network portion of the address.