Problem Statement
Explain DNS configuration in Linux including /etc/hosts, /etc/resolv.conf, and DNS troubleshooting commands.
Explanation
The /etc/hosts file provides static hostname-to-IP mappings checked before DNS queries. Format: IP_address hostname [aliases]. Example: 127.0.0.1 localhost, 192.168.1.10 server.local server. Changes take effect immediately. Use hosts file for local development, overriding DNS for specific hosts, or improving resolution speed for frequently accessed hosts.
The /etc/resolv.conf file configures DNS servers for system-wide name resolution. Format: nameserver IP lists DNS servers (queries in order listed), search domain sets search domains for short hostnames, domain sets local domain. Example: nameserver 8.8.8.8, nameserver 8.8.4.4 uses Google DNS. Modern systems often have resolv.conf managed by systemd-resolved or NetworkManager - check for symlinks.
Systemd-resolved manages DNS in modern systems. Configuration in /etc/systemd/resolved.conf. Check status: systemd-resolve --status or resolvectl status. Set DNS servers: resolvectl dns interface 8.8.8.8. Flush cache: resolvectl flush-caches. Resolution order: /etc/hosts → systemd-resolved cache → configured DNS servers → /etc/nsswitch.conf defines this order.
DNS troubleshooting tools: nslookup domain shows DNS query result and server used. Dig (domain information groper) provides detailed DNS query information: dig domain shows A record, dig domain MX shows mail servers, dig @8.8.8.8 domain queries specific DNS server, dig +trace domain shows full DNS resolution path from root servers. Host command: host domain simpler output than dig.
Common DNS issues: wrong DNS server (check resolv.conf), DNS server not responding (test with dig @server), caching issues (flush cache), local hosts file overriding DNS (check /etc/hosts), NetworkManager overwriting resolv.conf (make immutable with chattr +i or configure NetworkManager). Understanding DNS configuration is crucial for network connectivity and troubleshooting name resolution issues.
Practice Sets
This question appears in the following practice sets: