C.W.K.
Stream
Lesson 05 of 12 · published

DNS

~15 min · dns, name-resolution, hosts-file, mdns

Level 0Pinger
0 XP0/101 lessons0/12 achievements
0/150 XP to next level150 XP to go0% complete

The internet's phone book

DNS (Domain Name System) translates human-readable names like github.com into IP addresses like 140.82.121.3. Without DNS, every URL would be a string of numbers. With it, you only have to remember names.

What happens when you type a domain

When your browser opens github.com, a chain of lookups fires:

  1. OS cache — Did this machine resolve it recently?
  2. Router cache — Did your router resolve it recently?
  3. Recursive resolver — Your ISP's DNS, or a public one (1.1.1.1, 8.8.8.8). It does the heavy lifting.
  4. Root nameservers — "For .com, ask the .com TLD server."
  5. TLD nameserver — "For github.com, ask GitHub's authoritative nameserver."
  6. Authoritative nameserver — Returns the actual IP.

The whole dance takes milliseconds and is cached at every layer. Your browser typically only hits step 3 (the recursive resolver), because the recursive resolver caches everything below.

/etc/hosts — the override that predates DNS

Before DNS, every machine had a /etc/hosts file mapping names to IPs by hand. It still exists and is checked before DNS, which makes it incredibly useful for local overrides — pointing a domain to a different IP for testing, blocking ad servers, naming devices on your LAN.

mDNS / .local addresses

mDNS (Multicast DNS) lets devices on the same LAN discover each other by name with no DNS server involved. On macOS this is Bonjour. Any Mac on your network is reachable as hostname.local. ssh office.local works without a single line of DNS or SSH config — that's mDNS.

Code

Edit /etc/hosts safely·bash
# View the file
cat /etc/hosts

# Typical content:
#   127.0.0.1    localhost
#   ::1          localhost
#   192.168.1.100  office.local

# Add a local override (requires sudo)
sudo nano /etc/hosts

# After saving, flush macOS DNS cache so changes take effect:
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
Diagnose with dig and nslookup·bash
# Detailed query
dig github.com

# Just the answer
dig +short github.com

# Use a specific resolver (Cloudflare)
dig @1.1.1.1 +short github.com

# Reverse lookup (IP → name)
dig -x 140.82.121.3

# Which DNS server is your machine using? (macOS)
scutil --dns | head -20

External links

Exercise

Run dig +short github.com and note the IP. Then run dig +short @1.1.1.1 github.com — same answer? Edit /etc/hosts and add 127.0.0.1 testblock.example (sudo required), flush DNS with the macOS commands above, and confirm ping testblock.example resolves to localhost. Remove the line when done.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.