The /etc/hosts file contains one line for each hostname-to-IP address mapping, with the fields being separated by white space. The fields in the /etc/hosts file are as follows:

IP address: The IP address of the host. Hostname: The hostname of the host.

Here is an example of a /etc/hosts file entry:

In this example, the IP address is “127.0.0.1” and the hostname is “localhost”. This entry maps the hostname “localhost” to the IP address “127.0.0.1”, which is the loopback address for the local system. The /etc/hosts file is used by the system’s networking tools and services to resolve hostnames to IP addresses. When a hostname is entered into a web browser or other networking tool, the system first checks the /etc/hosts file to see if there is an entry for the hostname. If there is an entry, the system uses the corresponding IP address instead of performing an external DNS lookup.

Adding New Entry in /etc/hosts

You can also use /etc/hosts to resolve domain names that are not in the global DNS, such as local development servers or intranet servers. This can be done by adding entries for those servers in /etc/hosts with their corresponding IP addresses. For example:

Here demo.example.com is mapped to 192.168.1.210 IP address local.example.com is mapped with 127.0.0.1 (localhost).

Blocking Website with /etc/hosts

We can also use this file to prevent access to any domain. That can be helpful for parents to control website access to their loved ones. For example, If someone wants to block all traffic to facebook.com, they can add an entry in /etc/hosts that points facebook.com to 127.0.0.1, which is the loopback address and will not resolve to any actual website. The /etc/hosts file can be edited by the root user to add, modify, or delete hostname-to-IP address mappings. This can be useful for testing purposes, or to override the DNS resolution of specific hostnames. However, the /etc/hosts file should not be used as the primary source of hostname-to-IP address mappings, as it can become difficult to manage as the number of entries grows. Instead, it is generally recommended to use a centralized DNS server for hostname resolution.

Understanding the  etc hosts File   TecAdmin - 6Understanding the  etc hosts File   TecAdmin - 57Understanding the  etc hosts File   TecAdmin - 29