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 show
    

    Look for your primary interface (e.g., eth0, enp3s0) and ensure it has an UP state and an IP address assigned. If it’s down, you’ll see DOWN and no IP.

  • Fix: Bring the interface up.

    sudo ip link set eth0 up # Replace eth0 with your interface name
    

    This 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.conf
    

    Verify that the nameserver entries point to valid, reachable IP addresses of DNS servers. Common public DNS servers are 8.8.8.8 (Google) and 1.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.conf
    

    Add lines like:

    nameserver 8.8.8.8
    nameserver 1.1.1.1
    

    Save the file. For systems using systemd-resolved or NetworkManager, it’s better to configure DNS through their respective tools to prevent overwrites. For systemd-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-caches
    

    For 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.conf is 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 status
    

    If you’re using firewalld:

    sudo firewall-cmd --list-all
    

    If 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/tcp
    

    For 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 IP
    

    If ping fails, the DNS server is not reachable from your network. Also, try a direct DNS query using dig or nslookup against the specific server:

    dig @8.8.8.8 google.com
    

    If 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.8 is unresponsive, try 1.1.1.1 or your ISP’s provided DNS servers. Update /etc/resolv.conf with 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.conf
    

    Look for the hosts: line. It should typically look like:

    hosts:          files dns
    

    If dns is missing or commented out, or if files is missing and you expect to use /etc/hosts for lookups, it can cause issues.

  • Fix: Edit /etc/nsswitch.conf to ensure dns is present and in the correct order for hostname resolution.

    sudo nano /etc/nsswitch.conf
    

    Ensure the hosts: line includes dns. For example:

    hosts:          files dns myhostname
    

    (The myhostname entry is specific to systemd and often included by default on modern systems.)

  • Why it works: The nsswitch.conf file dictates the order in which the system consults different sources for information, including hostnames. The dns entry 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 --statistics
    

    For dnsmasq:

    sudo systemctl status dnsmasq
    

    If 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-caches
    

    For 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.

Want structured learning?

Take the full Dns course →