You try to connect to a server by hostname, but get "hostname not found" or "temporary failure in name resolution." SSH connections fail, wget can't download files, and even ping returns unknown host errors. Hostname resolution is fundamental to networking, and when it breaks, many services fail.

Understanding Hostname Resolution

  1. 1.Linux resolves hostnames through multiple mechanisms:
  2. 2.DNS servers - Query external nameservers
  3. 3./etc/hosts - Local hostname mappings
  4. 4.mDNS/Avahi - Local network discovery (.local domains)
  5. 5.NSS (Name Service Switch) - Controls resolution order

Typical Error Messages

bash
ping: google.com: Name or service not known
curl: (6) Could not resolve host: example.com
ssh: Could not resolve hostname server.local: Name or service not known
Temporary failure in name resolution
getaddrinfo failed: Name or service not known

Common symptoms: - ping fails with unknown host - curl and wget can't connect to URLs - SSH by hostname fails - Package managers can't reach repositories - Applications report DNS failures

Diagnosing Hostname Resolution Issues

Step 1: Basic Connectivity Test

```bash # Test if you can reach any external host ping -c 3 8.8.8.8

# If IP works but hostname doesn't, it's a DNS issue ping -c 3 google.com

# Test specific DNS resolution nslookup google.com dig google.com host google.com

# Test with specific DNS server nslookup google.com 8.8.8.8 dig @8.8.8.8 google.com ```

Step 2: Check DNS Configuration

```bash # View DNS configuration cat /etc/resolv.conf

# Check if it's a symlink (systemd-resolved) ls -la /etc/resolv.conf

# Check systemd-resolved status systemctl status systemd-resolved

# View resolved DNS settings resolvectl status

# Check DNS server connectivity ping -c 3 8.8.8.8 nc -vz 8.8.8.8 53 ```

Step 3: Check Hosts File

```bash # View hosts file cat /etc/hosts

# Check for specific entry grep hostname /etc/hosts

# Verify hosts file syntax # Format: IP_address hostname [aliases] # Example: 192.168.1.10 server.local server ```

Step 4: Check NSS Configuration

```bash # View NSS configuration cat /etc/nsswitch.conf

# Check hosts resolution order grep hosts /etc/nsswitch.conf

# Typical output: # hosts: files dns # (checks /etc/hosts first, then DNS)

# Test NSS resolution getent hosts google.com getent hosts localhost ```

Step 5: Check Local Hostname

```bash # View current hostname hostname hostnamectl

# View all hostnames hostnamectl status

# Check if hostname resolves ping -c 1 $(hostname) getent hosts $(hostname) ```

Solutions

Solution 1: Fix /etc/resolv.conf

If DNS servers are missing or incorrect:

```bash # View current DNS servers cat /etc/resolv.conf

# Check if using systemd-resolved ls -la /etc/resolv.conf

# If resolv.conf is managed by systemd-resolved sudo systemctl restart systemd-resolved

# View actual DNS servers in use resolvectl status

# Manually set DNS servers (temporary) sudo nano /etc/resolv.conf

# Add DNS servers: nameserver 8.8.8.8 nameserver 8.8.4.4 nameserver 2001:4860:4860::8888

# Or use your ISP's DNS servers # Or local DNS server if available nameserver 192.168.1.1 ```

Solution 2: Configure DNS with systemd-resolved

```bash # Edit resolved configuration sudo nano /etc/systemd/resolved.conf

[Resolve] DNS=8.8.8.8 8.8.4.4 FallbackDNS=1.1.1.1 1.0.0.1 Domains=~local # Disable stub listener if not needed # DNSStubListener=no

# Restart systemd-resolved sudo systemctl restart systemd-resolved

# Verify DNS servers resolvectl status

# If /etc/resolv.conf is not a symlink to systemd-resolved sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf ```

Solution 3: Configure DNS with NetworkManager

```bash # Check NetworkManager status systemctl status NetworkManager

# View current DNS settings nmcli dev show | grep DNS

# Set DNS servers for a connection nmcli con mod <connection-name> ipv4.dns "8.8.8.8 8.8.4.4" nmcli con up <connection-name>

# Or use nm-connection-editor for GUI nm-connection-editor

# Set DNS globally sudo nmcli general reload

# Ignore automatic DNS and use manual nmcli con mod <connection-name> ipv4.ignore-auto-dns yes nmcli con mod <connection-name> ipv4.dns "8.8.8.8 8.8.4.4" nmcli con up <connection-name> ```

Solution 4: Fix /etc/hosts

```bash # Edit hosts file sudo nano /etc/hosts

# Ensure localhost entries exist: 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback

# Add your hostname if needed: 127.0.1.1 myserver myserver.local

# Add other hosts as needed: 192.168.1.10 server.local server 192.168.1.20 database.local database

# Verify syntax # Each line should be: IP hostname [aliases] # No duplicate IPs for the same hostname ```

Solution 5: Fix Hostname Configuration

```bash # Set hostname sudo hostnamectl set-hostname myserver.local

# Verify hostnamectl

# Edit /etc/hosts to include your hostname sudo nano /etc/hosts # Add: 127.0.1.1 myserver.local myserver

# Some distributions use /etc/hostname cat /etc/hostname echo "myserver.local" | sudo tee /etc/hostname

# Apply immediately sudo hostname -F /etc/hostname ```

Solution 6: Fix NSS Configuration

```bash # Edit nsswitch.conf sudo nano /etc/nsswitch.conf

# Ensure hosts line includes dns: hosts: files dns myhostname

# Alternative orders: # files dns - Check hosts file first, then DNS # dns files - Check DNS first, then hosts file # files mdns4_minimal [NOTFOUND=return] dns mdns4 - Include mDNS

# Clear NSS cache sudo systemctl restart nscd # If nscd is installed sudo systemctl restart systemd-resolved ```

Solution 7: Disable IPv6 (If Causing Issues)

IPv6 can sometimes cause resolution delays:

```bash # Check if IPv6 is enabled cat /proc/sys/net/ipv6/conf/all/disable_ipv6 # 0 = enabled, 1 = disabled

# Disable IPv6 temporarily sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1 sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1 sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1

# Disable IPv6 permanently sudo nano /etc/sysctl.conf

# Add: net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv6.conf.lo.disable_ipv6 = 1

# Apply sudo sysctl -p ```

Solution 8: Fix DNS Port Blocking

```bash # Test if DNS port (53) is reachable nc -vz 8.8.8.8 53 telnet 8.8.8.8 53

# Check firewall rules sudo iptables -L -n | grep 53 sudo nft list ruleset | grep 53

# Allow DNS traffic sudo iptables -A INPUT -p udp --dport 53 -j ACCEPT sudo iptables -A OUTPUT -p udp --dport 53 -j ACCEPT

# For firewalld sudo firewall-cmd --add-service=dns --permanent sudo firewall-cmd --reload

# For ufw sudo ufw allow dns ```

Solution 9: Fix systemd-resolved Issues

```bash # Check if resolved is running systemctl status systemd-resolved

# Start if not running sudo systemctl start systemd-resolved sudo systemctl enable systemd-resolved

# Check if resolv.conf is linked correctly ls -la /etc/resolv.conf

# Should point to: # /run/systemd/resolve/stub-resolv.conf (recommended) # or /run/systemd/resolve/resolv.conf

# Fix symlink sudo ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf

# Restart resolved sudo systemctl restart systemd-resolved

# Flush DNS cache sudo resolvectl flush-caches ```

Solution 10: Fix Chroot/Container DNS

DNS issues in containers or chroot environments:

```bash # For chroot, copy resolv.conf sudo cp /etc/resolv.conf /path/to/chroot/etc/resolv.conf

# For Docker, use host DNS docker run --dns 8.8.8.8 myimage

# Or in docker-compose.yml dns: - 8.8.8.8 - 8.8.4.4

# For containers with systemd-resolved on host docker run -v /run/systemd/resolve:/run/systemd/resolve:ro myimage

# Bind mount resolv.conf docker run -v /etc/resolv.conf:/etc/resolv.conf:ro myimage ```

Testing DNS Resolution

```bash # Test basic resolution nslookup google.com dig google.com host google.com

# Test with specific DNS server dig @8.8.8.8 google.com nslookup google.com 8.8.8.8

# Test reverse DNS dig -x 8.8.8.8 nslookup 8.8.8.8

# Test specific record types dig google.com A dig google.com AAAA dig google.com MX dig google.com TXT

# Test DNS resolution time dig google.com | grep "Query time"

# Test with getent getent hosts google.com getent ahosts google.com ```

Debugging DNS Issues

```bash # Enable debug logging for dig dig +debug google.com

# Enable debug in resolvectl RESOLVCTL_DEBUG=1 resolvectl query google.com

# Check for DNSSEC issues dig +dnssec google.com

# Test EDNS dig +edns=0 google.com

# Test with TCP (normally DNS uses UDP) dig +tcp google.com

# Check DNS cache sudo resolvectl statistics ```

Verification

After making changes:

```bash # Test DNS resolution nslookup google.com dig google.com

# Test connectivity ping -c 3 google.com curl -I https://google.com

# Verify DNS configuration cat /etc/resolv.conf resolvectl status

# Check hosts file cat /etc/hosts

# Test local hostname hostname hostnamectl getent hosts $(hostname)

# Check nsswitch grep hosts /etc/nsswitch.conf ```

Quick Reference

```bash # View DNS servers cat /etc/resolv.conf resolvectl status

# Set DNS temporarily echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf

# Set DNS with systemd-resolved sudo nano /etc/systemd/resolved.conf sudo systemctl restart systemd-resolved

# Test DNS dig google.com nslookup google.com host google.com

# Flush DNS cache sudo resolvectl flush-caches

# Check hosts file cat /etc/hosts

# Set hostname sudo hostnamectl set-hostname myserver.local ```