DNS vs Hosts File: How Name Resolution Works
The hosts file and DNS both map hostnames to IP addresses, but the hosts file is a local, manually edited text file that takes priority, while DNS is a global, automated system of distributed servers. For most users, DNS is the practical choice, but the hosts file remains useful for testing, blocking, and offline resolution.
Background: How Name Resolution Started
Before DNS existed, a single file called HOSTS.TXT was maintained by the Network Information Center (NIC) and copied to every computer on the ARPANET. As the network grew, this approach became unsustainable—updates were slow, and the file grew too large. In 1983, Paul Mockapetris designed the Domain Name System (DNS) to replace it, creating a hierarchical, distributed database that could scale to millions of hosts. The hosts file never disappeared, though; it remains built into every major operating system as a local override mechanism.
How the Hosts File Works
The hosts file is a plain-text file that maps hostnames to IP addresses. When you type a URL, the OS checks this file before querying any DNS server. If it finds a match, it uses that IP address immediately; if not, it proceeds to DNS. The file is typically located at:
- Windows: C:\Windows\System32\drivers\etc\hosts
- macOS/Linux: /etc/hosts
Each line contains an IP address followed by one or more hostnames, separated by spaces or tabs. For example:
127.0.0.1 localhost
192.168.1.10 my-server.local
Lines starting with # are comments. On Windows, you need administrator privileges to edit it; on macOS/Linux, you need sudo. Changes take effect immediately—no reboot or service restart is required.
How DNS Works
DNS is a global, hierarchical system of servers that translate human-readable names into IP addresses. The process involves several steps:
- Local cache: Your OS and browser check their own cached DNS results.
- Recursive resolver: If not cached, your device queries a recursive resolver (usually provided by your ISP or a public DNS like 8.8.8.8).
- Root servers: The resolver asks a root server where to find the TLD (e.g., .com) server.
- TLD servers: The resolver then asks the TLD server where to find the authoritative name server for the domain.
- Authoritative server: Finally, the resolver queries the authoritative server, which returns the IP address.
This entire process typically takes milliseconds because of caching at each level. DNS records have a TTL (Time to Live) value that tells resolvers how long to cache the result.
Which Should You Use?
For everyday browsing, DNS is the clear winner—it's automated, scalable, and requires no maintenance. However, the hosts file is useful in specific scenarios:
- Development and testing: Point a domain to a local server (e.g., 127.0.0.1) to test a website before going live.
- Blocking unwanted sites: Map a domain to 127.0.0.1 or 0.0.0.0 to prevent access—a common ad-blocking technique.
- Offline name resolution: Use hosts entries for local network devices when DNS is unavailable.
- Overriding DNS for troubleshooting: Bypass a broken DNS record to test a different server.
For general use, avoid editing the hosts file unless you have a specific need. It's easy to make mistakes, and entries can conflict with DNS updates, causing unexpected behavior.
Common Problems and Pitfalls
Hosts file issues are usually straightforward but can be frustrating:
- Typos: A single wrong character breaks the mapping. Always double-check the IP and hostname.
- File format: Windows requires the file to be saved with ANSI encoding, not UTF-8 with BOM, or it may fail.
- Cache interference: Even after editing, your OS or browser may cache old DNS results. Flush the DNS cache using
ipconfig /flushdns(Windows) orsudo dscacheutil -flushcache(macOS). - Malware: Malicious software often modifies the hosts file to redirect traffic. Check it if you notice unexpected redirects.
- IPv6 vs IPv4: If a hostname resolves to an IPv6 address (::1) but your server only listens on IPv4, connection fails. Use
127.0.0.1for localhost to avoid this.
Security and Safety Considerations
Both systems have security implications. DNS is vulnerable to cache poisoning, where an attacker injects false records into a resolver, redirecting users to malicious sites. DNSSEC (DNS Security Extensions) helps mitigate this by digitally signing records, but adoption is incomplete. The hosts file is also a target: malware often adds entries to block security updates or redirect banking sites. Always protect the file with proper permissions and monitor it for unauthorized changes. When editing, avoid using public IP addresses for private resources unless you're certain of the security implications.
Alternatives and Related Tools
Beyond the hosts file and traditional DNS, there are other name-resolution options:
- mDNS (Multicast DNS): Used by Bonjour and Avahi to resolve .local hostnames on a local network without a DNS server.
- DNS over HTTPS (DoH) / DNS over TLS (DoT): Encrypt DNS queries to prevent eavesdropping and tampering.
- Local DNS servers: Tools like Pi-hole or dnsmasq can act as a network-wide hosts file, blocking ads and providing custom resolution.
- VPN and split DNS: Enterprise networks often use split DNS to resolve internal names differently for remote users.
These alternatives offer more flexibility or security but require additional setup. For most individuals, the default DNS configuration is sufficient.
Frequently Asked Questions
Does the hosts file override DNS?
Yes, on most operating systems, the hosts file is checked before DNS. If a hostname exists in the hosts file, the OS uses that IP address without querying DNS. This is why it's effective for blocking or redirecting domains.
Can I use wildcards in the hosts file?
No, the hosts file does not support wildcards. You must list each hostname explicitly. For example, 127.0.0.1 example.com only affects example.com, not sub.example.com. To block all subdomains, you'd need to add each one individually or use a DNS-based solution.
Why is my hosts file edit not working?
Common reasons include: incorrect file path, typos, not flushing the DNS cache, or the browser using its own DNS (e.g., DNS-over-HTTPS). Check that the file is saved correctly and that you're testing the right hostname. Also, some browsers like Chrome have a built-in DNS cache that may need clearing.
Is it safe to delete the hosts file?
It's not recommended. The hosts file contains essential entries like localhost and may be used by applications. Deleting it can cause issues with local name resolution. Instead, you can clear its contents (except comments) or restore the default entries for your OS.
How do I flush the DNS cache?
On Windows, open Command Prompt as administrator and run ipconfig /flushdns. On macOS, run sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder. On Linux, use sudo systemd-resolve --flush-caches or restart the network service, depending on your distribution.