While both files handle name resolution in Linux systems, they serve fundamentally different purposes:
# Example /etc/hosts entry
127.0.0.1 localhost
192.168.1.5 myserver.local
The /etc/hosts
file provides static hostname-to-IP mappings that take precedence over DNS lookups. It's typically used for:
- Local development environments
- Network isolation scenarios
- Blocking specific domains
# Example /etc/resolv.conf
nameserver 8.8.8.8
nameserver 1.1.1.1
search example.com
The /etc/resolv.conf
configures the system's DNS resolver, specifying:
- DNS server IP addresses
- Search domains for hostname completion
- Resolution options like timeout and attempts
The resolution order follows the nsswitch.conf
configuration (usually files dns
), meaning:
- First check
/etc/hosts
- Then query DNS servers specified in
/etc/resolv.conf
For local development with Docker:
# /etc/hosts
127.0.0.1 myapp.local
172.17.0.2 docker-mysql
For custom DNS resolution in a corporate network:
# /etc/resolv.conf
nameserver 10.0.0.53
search internal.corp.example.com
options timeout:2 attempts:2
/etc/resolv.conf
may be automatically managed by network services (NetworkManager, systemd-resolved)- Modern systems may use
systemd-resolve --status
to view actual DNS configuration - Changes to
/etc/hosts
take effect immediately
The best references are the man pages:
man 5 hosts
man 5 resolv.conf
For deeper understanding, consult the glibc documentation on name service switch.
Both /etc/hosts
and /etc/resolv.conf
handle hostname resolution in Linux systems, but they operate at different layers of the networking stack and serve distinct purposes.
This static file contains manual IP-to-hostname mappings that bypass DNS resolution entirely. Example entry:
127.0.0.1 localhost
192.168.1.5 myserver.local
This dynamic file configures how the system interacts with DNS servers. Example:
nameserver 8.8.8.8
nameserver 1.1.1.1
search example.com
- Resolution Order:
/etc/hosts
is checked before DNS (configured in/etc/nsswitch.conf
) - Persistence:
/etc/hosts
changes remain until manually edited, while/etc/resolv.conf
may be overwritten by DHCP or network managers - Scope:
/etc/hosts
is machine-specific, while DNS affects all resolvers
For local development environments, I often use:
# In /etc/hosts
127.0.0.1 api.staging.local
127.0.0.1 web.staging.local
For custom DNS configuration in Docker containers:
# In /etc/resolv.conf
nameserver 10.0.0.2
options ndots:0
Modern systems often use systemd-resolved
or NetworkManager
which dynamically manage /etc/resolv.conf
. To check actual DNS configuration:
systemd-resolve --status
For authoritative references:
man 5 hosts
man 5 resolv.conf
- The Linux Documentation Project (tldp.org)