/etc/hosts file

If you don’t want to use a DNS server for name resolution, you can use the /etc/hosts file for the purpose of name resolution. This is a simple text file that contains IP addresses to hostnames mappings. Each line consists of an IP address, followed by one or more hostnames:

etc hosts file

In the picture above you can see the typical default content of the hosts file that contains entries for the loopback addresses. To set up your own mappings, add the entries in the form of IP_ADDRESS HOSTNAME:

etc hosts mappings

The line 192.168.198.140 webserver will map the IP address of 192.168.198.140 to the webserver hostname . We can now use the webserver hostname to communicate with the remote machine:

ping hostname

Note that you can use both the /etc/hosts file and a DNS server for name resolution. The content of the hosts file will usually be used for lookups before DNS. If there is no match in the hosts file, then the DNS server will be used. Note that, however, the hosts files are rarely used today, since you need to edit each file individually on every machine.

Geek University 2022