DEV Community

David Cao
David Cao

Posted on

how /etc/hosts file works

In the Linux operating system, the /etc/hosts file is a simple text file that maps hostnames to IP addresses. It is used to resolve hostnames to IP addresses without the need for a DNS (Domain Name System) server.

The /etc/hosts file has the following format:

IP_address hostname aliases

For example:

127.0.0.1 localhost
192.168.0.100 server1
Enter fullscreen mode Exit fullscreen mode

In this example, the /etc/hosts file maps the hostname localhost to the IP address 127.0.0.1, and the hostname server1 to the IP address 192.168.0.100.

You can also specify multiple aliases for a hostname by separating them with spaces. For example:

192.168.0.100 server1 server1.example.com

In this case, both server1 and server1.example.com will be resolved to the IP address 192.168.0.100.

Note: In some cases, one server can have multiple IP addresses. Be sure to get the correct IP address and add it to the file.

The /etc/hosts file is usually the first place the operating system looks for hostname-to-IP address mappings. If the hostname is not found in the /etc/hosts file, the operating system will consult a DNS server to resolve the hostname to an IP address.

You can edit the /etc/hosts file to add your own hostname-to-IP address mappings. This can be useful for testing purposes, or for setting up a small network without the need for a DNS server.

It is important to note that the /etc/hosts file does not support wildcards or regular expressions, and each entry must be specified individually.

reference: https://manpages.ubuntu.com/manpages/xenial/man5/hosts.5.html

Top comments (0)