The hosts file is useful for mapping hostname or domain names with IP locally on the system. This file is available on each operating system and useful for mapping domains with IP addresses without making any DNS entry.
Sample Hosts File:
127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6
For example, if you are running a server on the public network and configured a website on that server. Now to access the website through the domain name, you must register a valid domain and point DNS records to that server. But using hosts file we can use any test domain name like example.com, www.example.com configure on server and map with IP in the hosts file, Using this we can access the site from the server without any domain registration and pointing DNS.
Edit Hosts File on Windows:
On Windows operating systems this file is available at following location with name “hosts”
C:\Windows\System32\drivers\etc
Navigate to the above location in file explorer and edit “hosts” file in notepad and make any entry like below at end of the file.
127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 192.168.1.100 example.com 192.168.1.100 www.example.com 10.10.0.11 site1.example.com site2.example.com
Save the file and close it. You have done the mapping between a domain name and IP locally on your system.
Edit Hosts File on Linux/Unix:
On Linux/Unix operating systems this file is generally available at following location
/etc/hosts
Edit this file and make proper entry with hostname and ip address as below.
# vim /etc/hosts
127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 192.168.1.100 example.com 192.168.1.100 www.example.com 10.10.0.11 site1.example.com site2.example.com
Save the file and close it. You have done the mapping between a domain name and IP locally on your system.