Skip to main content

Chapter 7.3 - DNS and Name Resolution

Learning Objectives​

By the end of this chapter, you will be able to:

  • Understand the DNS system
  • Configure DNS servers
  • Use /etc/hosts
  • Configure /etc/resolv.conf
  • Test DNS resolution
  • Use DNS tools (dig, nslookup, host)

What is DNS?​

DNS (Domain Name System) translates domain names into IP addresses.

Function:

  • Resolve names → IPs
  • Resolve IPs → names (reverse DNS)
  • Cache for performance

/etc/hosts​

Format​

# /etc/hosts
127.0.0.1 localhost
127.0.1.1 hostname.localdomain hostname
192.168.1.100 server.local
10.0.0.50 database.internal

Format:

IP_ADDRESS    HOSTNAME [ALIASES...]

Usage​

# Add entry
echo "192.168.1.100 my-server.local" | sudo tee -a /etc/hosts

# Verify
cat /etc/hosts

Priority:

  • /etc/hosts is checked before DNS
  • Useful for local override
  • Testing and development

/etc/resolv.conf​

Configuration​

# /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1
search example.com localdomain
options timeout:2 attempts:3

Directives:

  • nameserver: DNS servers (up to 3)
  • search: Search domains
  • options: Timeout, attempts, etc.
# Google DNS
nameserver 8.8.8.8
nameserver 8.8.4.4

# Cloudflare DNS
nameserver 1.1.1.1
nameserver 1.0.0.1

# OpenDNS
nameserver 208.67.222.222
nameserver 208.67.220.220

Note: On modern systems, /etc/resolv.conf may be managed by NetworkManager or systemd-resolved.


systemd-resolved​

Configuration​

# /etc/systemd/resolved.conf
[Resolve]
DNS=8.8.8.8 1.1.1.1
FallbackDNS=8.8.4.4 1.0.0.1
Domains=example.com

Restart:

sudo systemctl restart systemd-resolved

Verify​

# Status
systemd-resolve --status

# Resolve
systemd-resolve google.com

DNS Tools​

dig - DNS Lookup​

# Simple resolution
dig google.com

# Specific DNS server
dig @8.8.8.8 google.com

# Record type
dig google.com A # A record
dig google.com MX # MX record
dig google.com NS # NS record
dig google.com TXT # TXT record

# Reverse DNS
dig -x 8.8.8.8

# Short format
dig +short google.com

nslookup​

# Resolution
nslookup google.com

# Specific server
nslookup google.com 8.8.8.8

# Interactive mode
nslookup
> google.com
> server 8.8.8.8
> exit

host​

# Simple resolution
host google.com

# IP only
host -t A google.com

# Reverse DNS
host 8.8.8.8

# Specific server
host google.com 8.8.8.8

getent​

# Resolution via system
getent hosts google.com

# Uses /etc/hosts and DNS

Resolution Testing​

Verify Configuration​

# View configured DNS servers
cat /etc/resolv.conf
systemd-resolve --status

# Test resolution
ping -c 2 google.com
host google.com
dig google.com

Check Cache​

# Clear systemd-resolved cache
sudo systemd-resolve --flush-caches

# Check cache
systemd-resolve --statistics

Permanent Configuration​

Ubuntu/Debian (Netplan)​

# /etc/netplan/01-netcfg.yaml
network:
version: 2
ethernets:
eth0:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
search:
- example.com

Red Hat/CentOS​

# /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=8.8.8.8
DNS2=8.8.4.4
DOMAIN=example.com

DNS Troubleshooting​

Common Issues​

# 1. Check resolution
host google.com
dig google.com

# 2. Check DNS servers
cat /etc/resolv.conf

# 3. Test DNS server directly
dig @8.8.8.8 google.com

# 4. Check connectivity
ping -c 2 8.8.8.8

# 5. Check /etc/hosts
cat /etc/hosts

# 6. Clear cache
sudo systemd-resolve --flush-caches

Diagnostic Script​

#!/bin/bash
# DNS Diagnostic

echo "=== DNS Configuration ==="
cat /etc/resolv.conf

echo "=== Resolution Test ==="
host google.com
dig +short google.com

echo "=== DNS Server Test ==="
dig @8.8.8.8 google.com +short

echo "=== Cache ==="
systemd-resolve --statistics 2>/dev/null || echo "systemd-resolved not active"

Practical Examples​

Custom DNS Configuration​

#!/bin/bash
# Configure DNS

# Backup
sudo cp /etc/resolv.conf /etc/resolv.conf.bak

# New configuration
cat <<EOF | sudo tee /etc/resolv.conf
nameserver 8.8.8.8
nameserver 1.1.1.1
search localdomain
EOF

# Verify
cat /etc/resolv.conf

Local Override​

#!/bin/bash
# Add entry to /etc/hosts

HOST="dev-server.local"
IP="192.168.1.50"

# Check if exists
if ! grep -q "$HOST" /etc/hosts; then
echo "$IP $HOST" | sudo tee -a /etc/hosts
echo "Added: $HOST -> $IP"
else
echo "$HOST already exists"
fi

Summary​

In this chapter, you learned:

DNS: Name resolution → IPs
/etc/hosts: Local resolution, priority over DNS
/etc/resolv.conf: DNS server configuration
systemd-resolved: Modern DNS service
Tools: dig, nslookup, host for testing
Configuration: Netplan, ifcfg-* files
Troubleshooting: Resolution tests, cache


Next Steps​

Chapter 7.4: Network Tools
Module 8: Services and Daemons


Chapter created on: December 2024