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 localhost in a browser, it doesn’t query DNS.
  • The OS reads /etc/hosts and finds 127.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

ScenarioExample Entry
Local development127.0.0.1 myapp.local
Testing before DNS propagation1.2.3.4 staging.example.com
Blocking websites127.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.site

Now 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
FieldMeaning
nameserverIP of the DNS resolver to query
searchDefault domain suffix for short names

How It Works

  1. Browser asks OS for example.com.
  2. OS checks /etc/hosts — no match.
  3. OS reads /etc/resolv.conf — finds nameserver 192.168.1.1.
  4. OS sends DNS query to 192.168.1.1 (usually your router).
  5. 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

AspectLinuxmacOS
hosts location/etc/hosts/etc/hosts
resolv.conf location/etc/resolv.conf/etc/resolv.conf (often symlinked)
DNS managementsystemd-resolved, NetworkManagermDNSResponder, System Preferences
Flush DNS cachesystemd-resolve --flush-cachessudo killall -HUP mDNSResponder

Troubleshooting DNS Issues

When a domain doesn’t resolve:

  1. Check /etc/hosts — Is there a wrong static entry?
  2. Check /etc/resolv.conf — Is the nameserver correct and reachable?
  3. Test with nslookupnslookup example.com
  4. Test with specific resolvernslookup example.com 1.1.1.1
  5. Check if it’s a cache issue — Flush caches or wait for TTL expiration

Source