DNS Configuration with /etc/resolv.conf
Topic Overview
The /etc/resolv.conf file configures DNS resolution for Linux systems. It tells the system which DNS servers to query when converting domain names to IP addresses, making it critical for network connectivity and internet access.
Key Concepts
- Name Resolution: Converting hostnames to IP addresses
- DNS Server: Server that translates domain names to IPs
- Search Domain: Default domain appended to hostnames
- Resolver: System component that performs DNS lookups
- Nameserver Priority: Order servers are queried
File Structure
|
|
- Each directive on separate line
- Comments start with
# - Changes take effect immediately
Common Directives
nameserver - DNS server IP address (max 3)
search - List of domains for hostname completion
domain - Local domain name (deprecated)
options - Resolver behavior modifications
Practical Examples
Example 1: Basic configuration
|
|
Uses Google DNS with company domain search
Example 2: View current config
|
|
Displays current DNS configuration
Example 3: Test DNS resolution
|
|
Verify DNS is working correctly
Example 4: Backup before editing
|
|
Always backup before making changes
Common Options
timeout:n - Query timeout in seconds
attempts:n - Number of query attempts
rotate - Round-robin through nameservers
ndots:n - Dots needed to try absolute name first
Use Cases
- Configure corporate DNS servers
- Set up local network resolution
- Troubleshoot connectivity issues
- Override DHCP-provided DNS settings
- Set up split DNS configurations
Related Commands
nslookup - Interactive DNS lookup tool
dig - Detailed DNS information utility
host - Simple DNS lookup command
systemd-resolve - Modern systemd DNS tool
resolvectl - Control systemd-resolved
Tips & Troubleshooting
Common Issues
-
File gets overwritten: Network Manager or DHCP may reset changes. Use
chattr +ito make immutable or configure through Network Manager -
DNS not working: Check if systemd-resolved is managing DNS instead of resolv.conf
-
Slow resolution: Add
options single-requestto avoid IPv6 lookup delays
Best Practices
- Use at least 2 nameservers for redundancy
- Put fastest/most reliable server first
- Keep search list short (max 6 domains)
- Monitor for automatic overwrites
Security Notes
- Avoid untrusted DNS servers
- Consider DNS over HTTPS/TLS
- Monitor for DNS hijacking attempts
- Use internal DNS for local resources