Understanding the Linux Hosts File for Local DNS

The /etc/hosts file in Linux is an operating-system-level text file responsible for mapping human-readable domain names directly to numerical IP addresses on a local machine. Acting as a static, local precursor to the global Domain Name System (DNS), this file allows system administrators and developers to override public DNS records, test domains locally, or navigate internal networks without relying on external resolvers. This article explains the primary function of the Linux hosts file, how it integrates into the operating system's name-resolution workflow, and its most practical use cases.

What is the Linux Hosts File?

Located at /etc/hosts, the hosts file is a plain text file that contains static IP-to-hostname mappings. Long before centralized DNS servers were introduced to handle the vast scale of the internet, the hosts file was the sole mechanism used for translating network names to addresses.

Each entry in the file resides on its own line and follows a straightforward syntax: an IP address followed by one or more hostnames or aliases separated by spaces or tabs:

127.0.0.1   localhost
::1         localhost ip6-localhost ip6-loopback
192.168.1.50 fileserver.local fileserver

How It Works in Local DNS Resolution

When an application on a Linux system—such as a web browser or a command-line tool like curl—attempts to connect to a domain name, the operating system must translate that domain into an IP address.

Linux determines the order of resolution using the Name Service Switch configuration file, located at /etc/nsswitch.conf. By default, the hosts entry in this configuration typically reads:

hosts: files dns

This directive instructs the system to consult local files (specifically /etc/hosts) before sending queries over the network to external DNS servers configured in /etc/resolv.conf. If a matching entry exists in /etc/hosts, the operating system immediately returns the specified IP address, entirely bypassing the global DNS lookup process.

Primary Purposes and Use Cases

The hosts file remains a standard utility in modern Linux environments due to several practical capabilities: