Local DNS Configuration
Before a system ever reaches out to the global DNS infrastructure, it consults local configuration files. Understanding these is essential for development, testing, and troubleshooting DNS issues.
/etc/hosts — The Local Phonebook
The hosts file is a static mapping of hostnames to IP addresses. It is consulted before any external DNS query.
Default Contents
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
How It Works
- When you type
localhostin a browser, it doesn’t query DNS. - The OS reads
/etc/hostsand finds127.0.0.1. - It behaves like a local A record.
“When you say localhost, it actually uses this file to see which IP address you should point to. There’s an A record already set for that.” — Day 30 CKA Video
Common Use Cases
| Scenario | Example Entry |
|---|---|
| Local development | 127.0.0.1 myapp.local |
| Testing before DNS propagation | 1.2.3.4 staging.example.com |
| Blocking websites | 127.0.0.1 distractions.com |
| Internal services (no public DNS) | 10.0.0.5 internal-db.company |
Editing /etc/hosts
sudo nano /etc/hosts
# Add line:
127.0.0.1 teachers-local.siteNow visiting teachers-local.site in a browser resolves to 127.0.0.1 — no domain purchase required.
/etc/resolv.conf — DNS Resolver Settings
This file tells the OS which DNS server(s) to use for queries that don’t match /etc/hosts.
Typical Contents
nameserver 192.168.1.1
search localdomain
| Field | Meaning |
|---|---|
nameserver | IP of the DNS resolver to query |
search | Default domain suffix for short names |
How It Works
- Browser asks OS for
example.com. - OS checks
/etc/hosts— no match. - OS reads
/etc/resolv.conf— findsnameserver 192.168.1.1. - OS sends DNS query to
192.168.1.1(usually your router). - Router either answers from cache or forwards upstream.
“This is basically your DNS settings. You can see that for me, the DNS server is set up to my local router. Even my router has the capability to resolve it.” — Day 30 CKA Video
Changing Your DNS Server
If your router’s DNS is slow, you can override it:
sudo nano /etc/resolv.conf
# Change to Cloudflare:
nameserver 1.1.1.1
# Or Google:
nameserver 8.8.8.8“If you think your DNS is slow, maybe your router is slow, you can edit this file and change your DNS to 1.1.1.1 because Cloudflare’s DNS is very fast.” — Day 30 CKA Video
Note: On modern systems using systemd-resolved or macOS, /etc/resolv.conf may be auto-managed. Permanent changes should be made via NetworkManager, system settings, or systemd-resolved configuration.
macOS vs. Linux Differences
| Aspect | Linux | macOS |
|---|---|---|
hosts location | /etc/hosts | /etc/hosts |
resolv.conf location | /etc/resolv.conf | /etc/resolv.conf (often symlinked) |
| DNS management | systemd-resolved, NetworkManager | mDNSResponder, System Preferences |
| Flush DNS cache | systemd-resolve --flush-caches | sudo killall -HUP mDNSResponder |
Troubleshooting DNS Issues
When a domain doesn’t resolve:
- Check
/etc/hosts— Is there a wrong static entry? - Check
/etc/resolv.conf— Is the nameserver correct and reachable? - Test with
nslookup—nslookup example.com - Test with specific resolver —
nslookup example.com 1.1.1.1 - Check if it’s a cache issue — Flush caches or wait for TTL expiration