Hosting + Ai Website Builder + Free Domain (3 Month Free Credit)
Shop Today

How to Fix DNS on Linux Server: Complete Troubleshooting Guide

DNS (Domain Name System) is a crucial part of any network infrastructure. Administrators often need to fix DNS issues to ensure smooth translation of human-readable domain names (like example.com) into IP addresses (like 192.168.1.1) that computers use to communicate. If DNS is not functioning correctly on your Linux server, it can cause problems with website access, email delivery, and other internet services.

This guide will walk you through the common DNS-related issues on Linux servers and provide step-by-step solutions for fixing them, ensuring that your server resolves domain names correctly and remains accessible.

Preliminary Steps Before Fixing DNS

Fix DNS on Linux

Before diving into troubleshooting, ensure that the DNS configuration files and services are correctly set up.

Check DNS Resolver Configuration

The DNS resolver configuration is typically stored in a /etc/resolv.conf file on most Linux distributions. This file should contain at least one DNS server to resolve domain names.

To check the current DNS resolver configuration:

cat /etc/resolv.conf

A typical resolv.conf file should look like this:

nameserver 8.8.8.8
nameserver 8.8.4.4

If the file is empty or contains invalid DNS entries, you may experience DNS resolution issues.

Check if the DNS Service is Running

On Linux, DNS is typically handled by a service like systemd-resolved (for systemd-based systems) or dnsmasq. Check the status of these services.

For systemd-resolved:

sudo systemctl status systemd-resolved

For dnsmasq:

sudo systemctl status dnsmasq

If the service is not running, start it:

For systemd-resolved:

sudo systemctl start systemd-resolved

For dnsmasq:

sudo systemctl start dnsmasq

Test DNS Resolution

Use the dig or nslookup tool to test DNS resolution:

dig google.com

Or:

nslookup google.com

These commands should return the IP address of the domain (google.com) if DNS is working properly. If you don’t get any results, there may be a problem with DNS configuration or your DNS server.

Identifying Common DNS Issues

Here are some common DNS issues that may arise on a Linux server:

  • DNS Resolution Not Working

This can occur if DNS servers are not set correctly, the DNS server is down, or there’s an issue with the DNS service.

  • DNS Server Not Responding

Sometimes, the DNS server may be unreachable, either due to network problems or an incorrect DNS configuration.

  • Incorrect DNS Configuration in /etc/resolv.conf

A misconfigured /etc/resolv.conf file can prevent DNS resolution from working correctly.

  • DNS Caching Issues

Caching problems can cause outdated or incorrect DNS resolution results. For example, after updating DNS records, your server may still try to resolve old addresses due to caching.

  • Firewall Blocking DNS Traffic

A misconfigured firewall may block outbound DNS queries or responses, preventing DNS from working correctly.

Fixing DNS on Linux Server: Step-by-Step Solutions

Let’s go through step-by-step solutions to fix DNS problems on a Linux server.

Verify DNS Configuration in /etc/resolv.conf

  • Check DNS Entries:

Open the /etc/resolv.conf file:

sudo nano /etc/resolv.conf

Ensure that it contains valid DNS server entries. For example, you can use Google’s public DNS servers:

nameserver 8.8.8.8 nameserver 8.8.4.4

If you’re using a custom DNS server, replace 8.8.8.8 and 8.8.4.4 with the IP address of your preferred DNS server.

  • Check for search and domain Options:

If your server is part of a local network, you might also need the search or domain options in resolv.conf:

search example.com

This allows the server to resolve domain names like host to host.example.com.

  • Persist DNS Changes:

On systems with systemd-resolved, changes to /etc/resolv.conf might not persist across reboots. You may need to configure the DNS servers via systemd or update network configuration files. For systemd-resolved, you can modify /etc/systemd/resolved.conf:

sudo nano /etc/systemd/resolved.conf

In the [Resolve] section, add:

DNS=8.8.8.8 8.8.4.4

Restart the systemd-resolved service:

sudo systemctl restart systemd-resolved
  • Restart Networking Services:

After making changes to the resolv.conf file, restart networking services to apply the new DNS settings:

sudo systemctl restart networking # For Debian/Ubuntu-based systems sudo systemctl restart network # For RHEL/CentOS-based systems

Test DNS Resolution

Once you’ve updated the /etc/resolv.conf file, test DNS resolution to see if it is working properly:

dig google.com

If the command returns the correct IP address for google.com, DNS is functioning properly. If not, try using nslookup:

nslookup google.com

Flush DNS Cache

If you’re encountering issues with outdated or incorrect DNS information, flushing the DNS cache can help. Depending on your setup, you can flush DNS with the following commands.

  • For systemd-based systems (systemd-resolved):
sudo systemd-resolve --flush-caches
  • For dnsmasq:
sudo systemctl restart dnsmasq
  • For BIND (if running as a local DNS resolver):
sudo rndc flush

After flushing the DNS cache, test DNS resolution again with dig or nslookup.

Check for Firewall Issues

A firewall misconfiguration can block DNS traffic. Verify that your firewall is allowing DNS traffic on port 53 (both UDP and TCP).

Check Firewall Status:

  • For UFW (on Ubuntu/Debian):
sudo ufw status
  • For firewalld (on CentOS/RHEL):
sudo firewall-cmd --list-all

Allow DNS through the Firewall: If DNS traffic is blocked, open port 53 for both UDP and TCP.

  • For UFW:
sudo ufw allow 53
  • 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
  • Restart the Firewall:

After modifying the firewall rules, restart the firewall service:

sudo systemctl restart ufw # For UFW sudo systemctl restart firewalld # For firewalld

Check DNS Server Availability

If your DNS server is unreachable or experiencing issues, your server will not be able to resolve domain names.

  • Test DNS Server Reachability:

Check if the DNS server you configured is reachable using ping:

ping 8.8.8.8

If the ping fails, the DNS server may be down, or there may be a network issue.

  • Test with Different DNS Servers:

If the DNS server is down, try switching to a different DNS provider, such as Google’s DNS servers (8.8.8.8 and 8.8.4.4), Cloudflare (1.1.1.1), or OpenDNS (208.67.222.222).

Restart the DNS Service

If you’re using a local DNS server (such as dnsmasq or bind), restarting the service can help resolve DNS issues:

  • For dnsmasq:
sudo systemctl restart dnsmasq
  • For BIND:
sudo systemctl restart named
  • For systemd-resolved (if using systemd):
sudo systemctl restart systemd-resolved

Review Logs for DNS Issues

If DNS is still not functioning properly, checking the system logs can provide more information about what might be going wrong. You can review the following logs to fix DNS issue:

  • System Logs (for DNS-related issues):
sudo tail -f /var/log/syslog # For Debian/Ubuntu-based systems sudo tail -f /var/log/messages # For RHEL/CentOS-based systems
  • DNS Logs (for BIND or dnsmasq):

For dnsmasq, check /var/log/syslog or /var/log/daemon.log. For BIND, check /var/log/named.log.

Look for any errors or warnings that indicate issues with DNS resolution.

Conclusion

Fixing DNS on a Linux server involves ensuring that the DNS resolver configuration is correct, checking for network issues, verifying firewall settings, and reviewing system logs for errors. By following the troubleshooting steps outlined in this guide, you can identify and resolve common DNS problems on your Linux server. Once DNS is functioning properly, your server will be able to resolve domain names and provide seamless access to websites and services.

Himanshu Joshi

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top