/etc/hosts is a plain-text file that maps hostnames to IP addresses, consulted before DNS by default on most Linux systems. It predates DNS by decades: in the early ARPANET, a single HOSTS.TXT file distributed from SRI listed every host on the network. Today its role is smaller but still essential, primarily for local overrides and hostname self-identification.
127.0.0.1 localhost
::1 localhost ip6-localhost
127.0.1.1 myhostname.localdomain myhostname
192.168.1.50 nas.local nas
10.0.0.5 test.example.com
Common uses include defining local hostnames for convenience, overriding public DNS to test a new server, and blocking unwanted domains by mapping them to 127.0.0.1 (the basis of many ad-blocking host files). The lookup order between /etc/hosts and DNS is controlled by /etc/nsswitch.conf with the line hosts: files dns.
Changes to /etc/hosts take effect immediately—no daemon restart required. On containerised systems, each container has its own /etc/hosts, often auto-populated with links to other containers in the same network. Keeping this file small and organised is a small act of Linux hygiene that pays off.
Related terms: DNS, /etc/resolv.conf
Discussed in:
- Chapter 12: Networking — DNS: dig and nslookup
Also defined in: Textbook of Linux