Skip to content

DNS Resolution Failed Error Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about DNS Resolution Failed Error Fix. We cover key concepts, practical examples, and best practices.

DNS resolution translates domain names to IP addresses. When resolution fails, applications cannot connect to remote servers. The error typically appears as Name or service not known or Temporary failure in name resolution in network tools and applications.

The Wrong Way

import socket

try:
    ip = socket.gethostbyname("example.nonexistent")
    print(f"IP: {ip}")
except Exception as e:
    print(f"Error: {e}")

Output:

Error: [Errno -2] Name or service not known

The Right Way

Handle DNS errors gracefully and verify DNS configuration:

import socket

def resolve_hostname(hostname):
    try:
        ip = socket.gethostbyname(hostname)
        return ip
    except socket.gaierror as e:
        print(f"DNS resolution failed for {hostname}: {e}")
        return None

ip = resolve_hostname("example.com")
if ip:
    print(f"Resolved: {ip}")
else:
    print("Using fallback IP")

Output:

Resolved: 93.184.216.34

Step-by-Step Fix

1. Test DNS resolution from the command line

# Test system DNS
nslookup example.com
dig example.com
host example.com

2. Check DNS server configuration

cat /etc/resolv.conf
# Should show:
# nameserver 8.8.8.8
# nameserver 1.1.1.1

3. Test with an explicit DNS server

import dns.resolver

resolver = dns.resolver.Resolver()
resolver.nameservers = ["8.8.8.8", "1.1.1.1"]

try:
    answers = resolver.resolve("example.com", "A")
    for rdata in answers:
        print(f"IP: {rdata}")
except dns.resolver.NXDOMAIN:
    print("Domain does not exist")
except dns.resolver.NoAnswer:
    print("No A record found")
except dns.resolver.Timeout:
    print("DNS query timed out")

4. Fall back to IP if DNS fails

import socket

def connect_with_fallback(hostname, port):
    try:
        ip = socket.gethostbyname(hostname)
    except socket.gaierror:
        ip = "8.8.8.8"  # Fallback IP
    print(f"Connecting to {ip}:{port}")

5. Flush DNS cache

# Linux
sudo systemd-resolve --flush-caches

# macOS
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder

Prevention Tips

  • Configure at least two DNS servers in /etc/resolv.conf.
  • Use a local DNS cache like systemd-resolved or dnsmasq.
  • Monitor DNS query times with dig +stats.
  • Set reasonable DNS timeouts in application code.
  • Test DNS failover by simulating a primary DNS failure.

Common Mistakes with resolution failed

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world DNS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What causes DNS resolution failure?

DNS failure is caused by misconfigured DNS servers, network connectivity issues, expired domain names, or firewall blocks on port 53. The resolver cannot reach any DNS server or the server does not have the requested record.

How do I flush my DNS cache?

Linux: sudo systemd-resolve --flush-caches. macOS: sudo dscacheutil -flushcache. Windows: ipconfig /flushdns. This clears stale DNS entries that may cause resolution failures.

What is the difference between A and AAAA records?

A records map hostnames to IPv4 addresses. AAAA records map hostnames to IPv6 addresses. A domain can have both, and the resolver chooses based on the system's network configuration.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro