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:
- Local Development and Testing: Developers can map a
live domain name (e.g.,
example.com) directly to127.0.0.1or a private development server IP. This enables full-stack testing under actual domain conditions before updating public DNS records. - Pre-Migration Verification: When migrating a
website or service to a new server, administrators can add the new IP
address and target domain to
/etc/hoststo verify that the site works on the target infrastructure prior to cutting over global DNS. - Domain Sinkholing and Ad Blocking: By redirecting
unwanted domains, telemetry endpoints, or malicious servers to invalid
or unroutable addresses such as
0.0.0.0or127.0.0.1, users can block outgoing connections to specific targets at the OS level. - Small Network Name Resolution: On small local area networks (LANs) or home labs where running a dedicated DNS server is unnecessary, the hosts file provides a simple way to access other systems using memorable names instead of dynamic or hard-to-remember IP addresses.
- Network Redundancy: In the event of an external DNS
server outage, critical local mappings stored in
/etc/hostscontinue to function, ensuring persistent communication between local services.