Curl Works with IP Not Domain: Complete DNS Troubleshooting Guide
Learn why curl works with IP addresses but fails with domain names. Master DNS troubleshooting techniques and common DNS resolution issues.
Curl Works with IP Not Domain: Complete DNS Troubleshooting Guide
When you can successfully make requests using an IP address but fail when using a domain name, you're dealing with a DNS resolution problem. This is a common issue that can occur in various scenarios - from local development environments to production systems. Understanding how to diagnose and fix DNS resolution issues is crucial for any developer or system administrator.
Understanding DNS Resolution
How DNS Works
DNS (Domain Name System) is like the internet's phonebook. When you type a domain name like example.com, your system needs to look up the corresponding IP address before it can make the actual connection.
DNS Resolution Process
- Local cache check - Check if the domain is cached locally
- DNS server query - Ask configured DNS servers for the IP
- Recursive resolution - DNS servers query other servers if needed
- Response - Return the IP address to your application
Why IP Addresses Work
IP addresses are the actual network addresses that computers use to communicate. When you use an IP address directly, you bypass the DNS resolution process entirely:
# This works because it bypasses DNS
curl http://93.184.216.34
# This fails because DNS resolution is broken
curl http://example.com
Common DNS Resolution Issues
1. DNS Server Configuration Problems
Missing or Incorrect DNS Servers
# Check current DNS configuration
cat /etc/resolv.conf
# Expected output should show nameservers:
# nameserver 8.8.8.8
# nameserver 1.1.1.1
Fix DNS Configuration
# Add Google DNS servers
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null
echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf > /dev/null
# Or add Cloudflare DNS
echo "nameserver 1.1.1.1" | sudo tee /etc/resolv.conf > /dev/null
echo "nameserver 1.0.0.1" | sudo tee -a /etc/resolv.conf > /dev/null
2. DNS Server Unreachable
Test DNS Server Connectivity
# Test if DNS servers are reachable
ping -c 3 8.8.8.8
ping -c 3 1.1.1.1
# Test DNS resolution with specific server
dig @8.8.8.8 example.com
nslookup example.com 8.8.8.8
Check Network Connectivity
# Check if port 53 (DNS) is blocked
telnet 8.8.8.8 53
nc -zv 8.8.8.8 53
# Test with different DNS servers
for server in 8.8.8.8 1.1.1.1 9.9.9.9; do
echo "Testing $server"
dig @$server example.com +short
done
3. Firewall Blocking DNS
Check Firewall Rules
# Check iptables rules
sudo iptables -L -n | grep 53
# Check UFW status
sudo ufw status
# Check systemd firewall
sudo firewall-cmd --list-all
Allow DNS Traffic
# Allow DNS traffic through firewall
sudo ufw allow 53
sudo firewall-cmd --add-service=dns --permanent
sudo firewall-cmd --reload
4. Domain Name Issues
Verify Domain Exists
# Check if domain is registered
whois example.com
# Check domain from different network
# Try from your phone's hotspot or different network
Check for Typos
# Common typos to check:
# - Missing www prefix
# - Incorrect TLD (.com vs .org)
# - Extra characters or spaces
# - Case sensitivity (though DNS is case-insensitive)
5. Hosts File Override
Check Hosts File
# Check for conflicting entries
cat /etc/hosts
# Look for entries like:
# 127.0.0.1 example.com
# 0.0.0.0 example.com
Fix Hosts File
# Remove or comment out problematic entries
sudo nano /etc/hosts
# Or remove specific entries
sudo sed -i '/example.com/d' /etc/hosts
6. Internal DNS Issues
Internal Domain Resolution
# For internal domains, check internal DNS servers
nslookup internal.example.com
# Check if you're connected to the right network
ip route show
VPN and Network Issues
# Check VPN connection
ip addr show
route -n
# Test internal DNS servers
dig @internal-dns-server internal.example.com
DNS Troubleshooting Commands
Basic DNS Testing
nslookup
# Basic domain lookup
nslookup example.com
# Query specific DNS server
nslookup example.com 8.8.8.8
# Reverse DNS lookup
nslookup 93.184.216.34
dig (Domain Information Groper)
# Basic DNS query
dig example.com
# Query specific record type
dig MX example.com
dig AAAA example.com
# Query specific DNS server
dig @8.8.8.8 example.com
# Trace DNS resolution
dig +trace example.com
# Short output
dig +short example.com
host
# Simple DNS lookup
host example.com
# Query specific record type
host -t MX example.com
# Reverse DNS lookup
host 93.184.216.34
Advanced DNS Troubleshooting
DNS Cache Analysis
# Check local DNS cache (Windows)
ipconfig /displaydns
# Flush DNS cache (Windows)
ipconfig /flushdns
# Check systemd-resolved cache (Linux)
systemctl status systemd-resolved
sudo systemctl flush-dns
DNS Performance Testing
# Measure DNS resolution time
dig +stats example.com
# Test multiple DNS servers
for server in 8.8.8.8 1.1.1.1 9.9.9.9; do
echo "Testing $server"
time dig @$server example.com +short
done
Temporary Workarounds
Using Curl with Resolve
Bypass DNS with --resolve
# Manually resolve domain to IP
curl --resolve example.com:80:93.184.216.34 http://example.com
# For HTTPS
curl --resolve example.com:443:93.184.216.34 https://example.com
# Multiple resolves
curl --resolve example.com:80:93.184.216.34 \
--resolve api.example.com:80:93.184.216.35 \
http://example.com
Using Hosts File Temporarily
Add Temporary Entry
# Add temporary entry to hosts file
echo "93.184.216.34 example.com" | sudo tee -a /etc/hosts
# Test the connection
curl http://example.com
# Remove the entry when done
sudo sed -i '/example.com/d' /etc/hosts
DNS Configuration Best Practices
1. Use Multiple DNS Servers
# Configure primary and secondary DNS
nameserver 8.8.8.8
nameserver 1.1.1.1
nameserver 9.9.9.9
2. Use Reliable DNS Providers
# Popular public DNS servers:
# Google DNS: 8.8.8.8, 8.8.4.4
# Cloudflare: 1.1.1.1, 1.0.0.1
# Quad9: 9.9.9.9, 149.112.112.112
# OpenDNS: 208.67.222.222, 208.67.220.220
3. Monitor DNS Performance
# Regular DNS health checks
dig +stats example.com
# Monitor DNS resolution times
ping -c 10 example.com
4. Implement DNS Caching
# Enable DNS caching
systemctl enable systemd-resolved
systemctl start systemd-resolved
Common Scenarios and Solutions
Scenario 1: Development Environment
Problem: Local development domain not resolving
# Add to /etc/hosts
echo "127.0.0.1 localhost" | sudo tee -a /etc/hosts
echo "127.0.0.1 myapp.local" | sudo tee -a /etc/hosts
Scenario 2: Corporate Network
Problem: Internal domains not resolving
# Check corporate DNS servers
nslookup internal.company.com
# Connect to VPN if needed
# Check network configuration
Scenario 3: Docker Containers
Problem: Container cannot resolve external domains
# Check Docker DNS configuration
docker run --dns=8.8.8.8 nginx
# Or modify Docker daemon configuration
echo '{"dns": ["8.8.8.8", "1.1.1.1"]}' | sudo tee /etc/docker/daemon.json
Scenario 4: Kubernetes
Problem: Pods cannot resolve external domains
# Configure DNS in pod spec
apiVersion: v1
kind: Pod
spec:
dnsPolicy: "None"
dnsConfig:
nameservers:
- 8.8.8.8
- 1.1.1.1
DNS Security Considerations
DNS over HTTPS (DoH)
# Configure DoH in browsers
# Firefox: about:preferences#privacy
# Chrome: chrome://settings/security
DNS over TLS (DoT)
# Configure DoT in systemd-resolved
[Resolve]
DNS=1.1.1.1#cloudflare-dns.com
DNSOverTLS=yes
DNSSEC
# Check DNSSEC status
dig +dnssec example.com
# Verify DNSSEC chain
dig +sigchase example.com
Conclusion
When curl works with an IP address but fails with a domain name, you're dealing with a DNS resolution problem. The key steps to resolve this are:
- Check DNS configuration - Verify
/etc/resolv.confhas valid nameservers - Test DNS connectivity - Ensure DNS servers are reachable
- Check firewall rules - Make sure port 53 is not blocked
- Verify domain existence - Confirm the domain is registered and correct
- Check hosts file - Look for conflicting entries
- Test with different DNS servers - Use public DNS servers like 8.8.8.8
Key takeaways:
- DNS is essential - Domain names must be resolved to IP addresses
- Multiple DNS servers - Always configure backup DNS servers
- Test systematically - Use dig, nslookup, and host commands
- Check network connectivity - Ensure DNS servers are reachable
- Use temporary workarounds - --resolve flag or hosts file for quick fixes