Bootstrap FreeKB - Linux Fundamentals - Understanding the /etc/hosts file in Linux
Linux Fundamentals - Understanding the /etc/hosts file in Linux

Updated:   |  Linux Fundamentals articles

The /etc/hosts file contains a listing of hostnames associated with an IP address. By default, the /etc/hosts file should have one or more hostnames associated with the IPv4 loopback address 127.0.0.1 and the IPv6 loopback address ::1.

[root@server1 ~]# cat /etc/hosts
127.0.0.1    localhost  localhost.localdomain  localhost4  localhost4.localdomain4
::1          localhost  localhost.localdomain  localhost6  localhost6.localdomain6

 

The  /etc/hosts file should be used before DNS to resolve a hostname to an IP address. Ping or ping6 can be used to ensure the  /etc/hosts file is properly resolving a hostname to an IP address. In this example, pinging localhost properly pings 127.0.0.1.

[root@server1 ~]# ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.031ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.036ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.032ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.031ms

 

For example, let's say the you have two routers, one named r1.examle.com and the other names r2.example.com. The following could be entered in /etc/hosts.

[root@server1 ~]# cat /etc/hosts
127.0.0.1    localhost  localhost.localdomain  localhost4  localhost4.localdomain4
::1          localhost  localhost.localdomain  localhost6  localhost6.localdomain6
192.168.0.1  r1.example.com
192.168.0.2  r2.example.com

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 3f6e54 in the box below so that we can be sure you are a human.