Use it for local overrides and tests, or when a name resolves to an unexpected address.
/etc/hosts
/etc/hosts maps names to IP addresses locally, consulted before DNS when nsswitch.conf says hosts: files dns.
1. grep <name> /etc/hosts — override present?
2. grep hosts /etc/nsswitch.conf — files before dns?
3. getent hosts <name> vs dig +short <name> — different?
4. Remove or correct the entry.
5. Test again.
lookup → nsswitch "files" → /etc/hosts match returns first → otherwise DNS
For DNS → /etc/hosts, continue with the first unproven Topic check: Capture the exact name spelling/FQDN and inspect all matching hosts lines including IPv4/IPv6 duplicates and aliases. If that boundary is healthy, advance to the adjacent check: Read hosts ordering in nsswitch and identify whether the process runs in a container/chroot or uses a custom resolver. Preserve the same host/time/identity scope and rerun the original workload before accepting recovery.
Continue only after interpreting the current observation.Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Operational evidence path Verify boundary
application name → NSS hosts ordering → namespace-specific /etc/hosts match → address selection → route/socket → service/application result
Healthy evidence: the exact intended line is unambiguous, NSS/application lookup returns it in the relevant namespace and the mapped endpoint matches the intended target.
Step 2 Verification and output interpretation Verify boundary
Existing fast checks for this Topic. - Capture the exact name spelling/FQDN and inspect all matching hosts lines including IPv4/IPv6 duplicates and aliases.
- Read hosts ordering in nsswitch and identify whether the process runs in a container/chroot or uses a custom resolver.
- Use getent/app-path lookup to show effective resolution, then compare with DNS only if precedence/override behavior is relevant.
- After change, repeat original lookup and connection, and verify no unintended alias/family regression.
Verification chain. application name → NSS hosts ordering → namespace-specific /etc/hosts match → address selection → route/socket → service/application result
Step 3 Commands Verify boundary
getent hosts myhost
grep myhost /etc/hosts
Step 4 Failure modes and next checks Narrow the fault
Suspicious evidence: duplicate/conflicting entries, wrong address family, container-specific generated hosts data, or direct DNS differs while the application unexpectedly prefers local files.
/etc/hosts evidence requires that the original resolver path selects the intended local entry for the exact name/family; the file line itself does not show effective resolution or connectivity.
Step 5 Common mistakes Narrow the fault
- Stale
/etc/hostsentries overriding DNS.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
resolvectl status— DNS servers per link (systemd-resolved)cat /etc/resolv.conf— configured nameserversgetent hosts <name>— resolution through the system path (nsswitch)dig <name> @<server>— query a specific server
ReferenceWhat it is1 section · depth on demand
Core model
Read hosts data together with /etc/nsswitch.conf and the exact queried name. Distinguish canonical name and aliases, IPv4/IPv6 entries, duplicate/conflicting lines and container/chroot namespaces. Verify through getent or the application resolver API instead of assuming a file line wins.
ReferenceImportant terms & boundaries5 sections · depth on demand
What this shows and what it does not
/etc/hosts evidence requires that the original resolver path selects the intended local entry for the exact name/family; the file line itself does not show effective resolution or connectivity.
In depth
/etc/hosts: static name resolution
/etc/hosts maps names to addresses without any network lookup. The hosts(5) manual page defines the format: one IP address per line, followed by the canonical host name and optional aliases. Text after # is a comment. The page notes that DNS has largely replaced the file. It's still used for bootstrapping (resolving important hosts when DNS isn't available, for example during boot), and for isolated networks.
Standard entries (manual)
| Line | Purpose |
|---|---|
127.0.0.1 localhost | IPv4 loopback |
::1 localhost ip6-localhost ip6-loopback | IPv6 loopback |
127.0.1.1 thishost.example.org thishost | The machine's own name (a Debian convention) |
ff02::1 ip6-allnodes, ff02::2 ip6-allrouters | IPv6 multicast names |
Legitimate uses
| Use | Example |
|---|---|
| The host's own name | Resolves without DNS, so sudo and services don't hang |
| Cluster nodes that must resolve without DNS | 10.30.0.11 node1.cluster.example.net node1 |
| Testing a migration before the DNS change | Point the name at the new server on one client |
| Blocking a host | 0.0.0.0 tracking.example.com |
Risks
| Risk | Consequence |
|---|---|
| Stale entries | DNS changes are ignored; the "it works everywhere except on this host" problem |
| Inconsistent copies across hosts | Hard-to-find differences |
Not checked by dig | Troubleshooting shows the DNS answer, not what applications use |
If the hosts file must be used on many hosts, manage it centrally with configuration management. Comment each entry with its purpose and an expiry date.
Lookup order
Whether /etc/hosts is read before or after DNS depends on /etc/nsswitch.conf. The usual order is files before dns, so the hosts file wins.
host$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 srv1.example.net srv1
10.30.0.11 node1.cluster.example.net node1 # cluster interconnect, no DNS
203.0.113.50 www.example.net # migration test, remove after 2026-12-05
::1 localhost ip6-localhost ip6-loopback
host$ getent hosts www.example.net
203.0.113.50 www.example.net
host$ dig +short www.example.net
198.51.100.10
host$ grep '^hosts' /etc/nsswitch.conf
host$ hostname -f
srv1.example.net
Reading the example
Applications on this host reach www.example.net at the test address 203.0.113.50, while DNS still says 198.51.100.10. That's exactly what the migration test intends. The comment with a removal date makes sure the override doesn't stay forever.
Documented example, not captured output.
How it works at runtime
/etc/hosts is part of DNS → /etc/hosts. /etc/hosts is a local static hostname-to-address source consulted according to NSS policy and can intentionally override DNS for selected names, but its presence, ordering and exact name forms determine whether an application actually receives the entry.
Topic-specific state model. /etc/hosts is a local static hostname-to-address source consulted according to NSS policy and can intentionally override DNS for selected names, but its presence, ordering and exact name forms determine whether an application actually receives the entry.
Interpretation rule. - Entry in /etc/hosts ≠ application uses it if NSS ordering or an embedded resolver bypasses files.
- Short-name entry ≠ FQDN match in every application; exact labels and search expansion matter.
- Correct address in hosts ≠ service reachable at that address.
- Editing host /etc/hosts ≠ editing a container/chroot namespace copy.
Quick reference
/etc/hosts provides static name mappings consulted before DNS (with hosts: files dns).
127.0.0.1 localhost
::1 localhost ip6-localhost
10.0.10.21 web01.corp.example.com web01
getent hosts web01
Useful for bootstrapping, cluster nodes that must resolve each other without DNS, and testing a migration (10.0.30.5 app.example.com). Keep entries minimal and documented — forgotten entries cause confusing behaviour later. cloud-init may regenerate it (manage_etc_hosts).
Limits and how it fails
Primary failure point. /etc/hosts evidence requires that the original resolver path selects the intended local entry for the exact name/family; the file line itself does not show effective resolution or connectivity.
ReferenceConfiguration & state1 section · depth on demand
Configuration, persistence and lifecycle
Distribution/platform context. /etc/hosts owns local static name mappings. DNS owns external resolution; containers may own generated namespace-specific hosts files; Networking owns reachability to the mapped address.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in DNS → /etc/hosts. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.freedesktop.org/software/systemd/man/latest/systemd-resolved.service.html · confidence 94%What changed and what may be affected
Recent technical edits are separated from ordinary page timestamps. Relationship impact is advisory: it identifies nearby canonical references to review, not automatic breakage.
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
/etc/hosts operational reference
Topic-specific lookup boundaries, proof limits and next-step Evidence for /etc/hosts.
| Lookup boundary | Topic-specific reference | Operational use |
|---|---|---|
| Scope / effective state | /etc/hosts is a local static hostname-to-address source consulted according to NSS policy and can intentionally override DNS for selected names, but its presence, ordering and exact name forms determine whether an application actually receives the entry. | Establish exactly what this Topic owns before crossing into another subsystem. |
| Fast verification | Pin exact host/namespace + original name or endpoint → capture link/address/route/resolver state → reproduce original failure → localize first failed resolver/network boundary → collect bounded packet/socket or DNS evidence → change one demonstrated cause → read back effective state → repeat original application transaction and an adjacent regression check. | Use the shortest evidence path first; a fast check is a boundary test, not blanket proof. |
| Verification chain | application name → NSS hosts ordering → namespace-specific /etc/hosts match → address selection → route/socket → service/application result | Trace requested/configured state through effective state to the observed workload result. |
| Failure boundary | /etc/hosts proof requires that the original resolver path selects the intended local entry for the exact name/family; the file line itself does not prove effective resolution or connectivity. | Stop broad troubleshooting at the first boundary that is not supported by evidence. |
| Positive evidence | Correlated Linux DNS/network evidence can establish this proof path: application name → NSS hosts ordering → namespace-specific /etc/hosts match → address selection → route/socket → service/application result | Use this observation only for the specific claim it actually supports. |
| Evidence limit | It does not independently prove behavior outside the captured host/network namespace, resolver path or DNS view, interface/source, destination/protocol/port, capture point, time or workload scope. /etc/hosts proof requires that the original resolver path selects the intended local entry for the exact name/family; the file line itself does not prove effective resolution or connectivity. | Do not promote a local success signal into end-to-end or adjacent-dependency proof. |
| Next troubleshooting check | Continue from the first unproven DNS/network boundary: preserve the original source and failure conditions, capture the adjacent resolver/socket/packet state, change only the demonstrated cause, then repeat the original application lookup or transaction and adjacent regression checks. | Continue to the next unresolved boundary while preserving the original test condition. |
/etc/hosts · operational reference
Compact Linux state, persistence, verification and boundary reference for fast lookup.
| Reference boundary | /etc/hosts |
|---|---|
| Topic path | DNS → /etc/hosts |
| Runtime state | /etc/hosts is a local static hostname-to-address source consulted according to NSS policy and can intentionally override DNS for selected names, but its presence, ordering and exact name forms determine whether an application actually receives the entry. |
| Fast checks | - Capture the exact name spelling/FQDN and inspect all matching hosts lines including IPv4/IPv6 duplicates and aliases. - Read hosts ordering in nsswitch and identify whether the process runs in a container/chroot or uses a custom resolver. - Use getent/app-path lookup to prove effective resolution, then compare with DNS only if precedence/override behavior is relevant. - After change, repeat original lookup and connection, and verify no unintended alias/family regression. |
| Verification chain | application name → NSS hosts ordering → namespace-specific /etc/hosts match → address selection → route/socket → service/application result Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return/regression → explicit Evidence boundary. Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return path or regression → explicit Evidence boundary. |
| Failure boundary | /etc/hosts proof requires that the original resolver path selects the intended local entry for the exact name/family; the file line itself does not prove effective resolution or connectivity. Boundary chain: process/socket namespace → source/rule/FIB → neighbor → netfilter/eBPF/qdisc → virtual/physical device → peer → return → application. |
| Persistence rule | Prove which configuration source owns the state, then prove that the running consumer loaded or generated the intended state. |