The DNS resolver on your Linux machine is failing to translate hostnames into IP addresses because it’s either not receiving valid responses from DNS servers or it’s not configured to ask the right ones.
Common Causes and Fixes
1. Network Interface Not Up
-
Diagnosis: Check if your network interface is active.
ip addr showLook for your primary interface (e.g.,
eth0,enp3s0) and ensure it has anUPstate and an IP address assigned. If it’s down, you’ll seeDOWNand no IP. -
Fix: Bring the interface up.
sudo ip link set eth0 up # Replace eth0 with your interface nameThis command directly instructs the kernel to activate the specified network interface, allowing it to send and receive network traffic, including DNS queries.
-
Why it works: The DNS client needs a functional network interface to send requests to DNS servers and receive their replies. If the interface is down, no network communication is possible.
2. Incorrect DNS Server Configuration in /etc/resolv.conf
-
Diagnosis: Examine your DNS resolver configuration file.
cat /etc/resolv.confVerify that the
nameserverentries point to valid, reachable IP addresses of DNS servers. Common public DNS servers are8.8.8.8(Google) and1.1.1.1(Cloudflare). If this file is empty, missing, or contains invalid IPs, it’s the problem. -
Fix: Manually edit or regenerate
/etc/resolv.conf. To manually edit:sudo nano /etc/resolv.confAdd lines like:
nameserver 8.8.8.8 nameserver 1.1.1.1Save the file. For systems using
systemd-resolvedorNetworkManager, it’s better to configure DNS through their respective tools to prevent overwrites. Forsystemd-resolved:sudo systemd-resolve --set-dns=8.8.8.8 --interface=eth0 # Replace eth0 sudo systemd-resolve --set-dns=1.1.1.1 --interface=eth0 sudo systemd-resolve --flush-cachesFor
NetworkManager:sudo nmcli connection modify eth0 ipv4.dns "8.8.8.8,1.1.1.1" # Replace eth0 sudo nmcli connection up eth0 -
Why it works:
/etc/resolv.confis the primary configuration file read by the C library’s DNS resolver. It tells your system which DNS servers to query. Correcting these entries ensures your system asks the actual DNS servers that can provide IP address translations.
3. Firewall Blocking DNS Traffic (UDP/TCP Port 53)
-
Diagnosis: Check your local firewall rules. If you’re using
ufw:sudo ufw statusIf you’re using
firewalld:sudo firewall-cmd --list-allIf port 53 (for UDP and TCP) is explicitly denied or not allowed for outgoing connections, this can be the cause.
-
Fix: Allow outgoing DNS traffic. For
ufw:sudo ufw allow out 53/udp sudo ufw allow out 53/tcpFor
firewalld:sudo firewall-cmd --zone=public --add-port=53/udp --permanent sudo firewall-cmd --zone=public --add-port=53/tcp --permanent sudo firewall-cmd --reload -
Why it works: DNS queries and responses are transmitted over UDP and sometimes TCP on port 53. A firewall blocking these packets prevents your machine from communicating with DNS servers.
4. DNS Server Unreachable or Unresponsive
-
Diagnosis: Manually test connectivity to the configured DNS servers.
ping 8.8.8.8 # Or your configured DNS server IPIf ping fails, the DNS server is not reachable from your network. Also, try a direct DNS query using
digornslookupagainst the specific server:dig @8.8.8.8 google.comIf this command times out or returns errors, the server itself is the issue.
-
Fix:
- Check your network connectivity: Ensure your router is online and your local network is functioning.
- Try different DNS servers: If
8.8.8.8is unresponsive, try1.1.1.1or your ISP’s provided DNS servers. Update/etc/resolv.confwith the new IPs. - Restart your router/modem: Sometimes network hardware needs a refresh.
-
Why it works: The DNS resolver relies on external DNS servers to perform lookups. If these servers are down, overloaded, or unreachable due to network issues beyond your machine, resolution will fail.
5. /etc/nsswitch.conf Misconfiguration
-
Diagnosis: Inspect the Name Service Switch configuration.
cat /etc/nsswitch.confLook for the
hosts:line. It should typically look like:hosts: files dnsIf
dnsis missing or commented out, or iffilesis missing and you expect to use/etc/hostsfor lookups, it can cause issues. -
Fix: Edit
/etc/nsswitch.confto ensurednsis present and in the correct order for hostname resolution.sudo nano /etc/nsswitch.confEnsure the
hosts:line includesdns. For example:hosts: files dns myhostname(The
myhostnameentry is specific to systemd and often included by default on modern systems.) -
Why it works: The
nsswitch.conffile dictates the order in which the system consults different sources for information, including hostnames. Thednsentry tells the system to use DNS servers (as configured in/etc/resolv.conf) for name resolution.
6. DNS Caching Issues (e.g., systemd-resolved or dnsmasq)
-
Diagnosis: If you recently changed DNS settings or suspect stale data, check your caching service status. For
systemd-resolved:systemd-resolve --statisticsFor
dnsmasq:sudo systemctl status dnsmasqIf the service is running but not responding, or if you suspect cached bad data, it might be the cause.
-
Fix: Flush the DNS cache. For
systemd-resolved:sudo systemd-resolve --flush-cachesFor
dnsmasq:sudo systemctl restart dnsmasq -
Why it works: Caching DNS resolvers store recent lookups to speed up subsequent requests. If the cache contains incorrect or outdated information (e.g., a hostname was temporarily unavailable), it will continue to serve that bad data until cleared.
After resolving these, the next error you might encounter is a Connection refused if the service you’re trying to reach isn’t actually running on the resolved IP address.