How to Configure a DNS Server in Linux​?

A properly configured DNS server is one of the most important yet often overlooked parts of a Linux system. Whether you are managing a web server, VPS, dedicated server, or internal network, DNS settings directly impact domain resolution, application connectivity, software updates, email delivery, and overall system reliability.

If DNS is configured incorrectly, even a perfectly functioning server can experience connectivity issues, package installation failures, and service disruptions. 

In this guide, we will walk through configuring DNS servers on Linux using two different methods, understand when each method should be used, and verify that everything is working correctly after configuration.


Also Read: How to Clear DNS Cache?


Understanding DNS Configuration Files in Linux

Depending on your Linux distribution, DNS settings are generally stored in:

Copied!
AddType text/html .shtml
AddHandler server-parsed.shtml
Options +Includes

This file contains the DNS servers your system uses for domain name resolution.

A typical entry looks like:

nameserver 8.8.8.8

nameserver 8.8.4.4

Here:

  • 8.8.8.8 = Google Public DNS
  • 8.8.4.4 = Secondary Google DNS

★ Quick Tip

Always configure at least two DNS servers.

If the primary DNS server becomes unavailable, your system can automatically use the secondary server, improving reliability.


Also Read: How to Fix Slow DNS Lookup?


Before You Start

Before configuring DNS settings:

  • Ensure you have root or sudo access.
  • Verify your internet connection.
  • Identify the DNS servers you want to use.

Common public DNS servers include:

ProviderPrimary DNSSecondary DNS
Google DNS8.8.8.88.8.4.4
Cloudflare DNS1.1.1.11.0.0.1
OpenDNS208.67.222.222208.67.220.220

Also Read: How to Change SSH Port in Linux?


Methods to Configure DNS Server in Linux

➢ Method 1: Configure DNS Using /etc/resolv.conf

This is the simplest and most direct method.

➔ Open the DNS Configuration File & run:

Copied!
sudo nano /etc/resolv.conf

This file tells Linux which DNS servers to use for domain name lookups, and opening the file allows us to add or modify DNS entries.

➔ Now, you have to add DNS server entries by adding the following lines:

Copied!
nameserver 8.8.8.8
nameserver 8.8.4.4

➔ After adding, just save the file and exit.

This instructs Linux to query Google’s DNS servers whenever a domain name needs to be resolved.

➔ Now, it’s time to test DNS resolution by running the following command

Copied!
ping google.com

This confirms that your system can successfully convert domain names into IP addresses using the configured DNS server.

★ Quick Tip

If domain names do not resolve but IP addresses work, DNS configuration is often the first thing to check.


Also Read: How to Set Up FTP on Linux​?


➢ Method 2: Configure a DNS Server in Linux Using BIND9

BIND9 (Berkeley Internet Name Domain 9) is one of the most widely used DNS server software solutions for Linux, trusted by organizations and hosting providers worldwide. 

It allows you to host your own DNS zones, manage domain records, and control how DNS queries are resolved within your network or on the internet.

Step 1: Install BIND9

➔ First, update your package repository and install BIND9:

Copied!
sudo apt update
sudo apt install bind9 bind9utils bind9-doc -y

★ Quick Tip

Before proceeding, ensure your server has a static IP address. DNS servers should ideally use a fixed IP so clients can always reach them.

Step 2: Verify BIND9 Installation

Forwarders allow your DNS server to send unresolved queries to upstream DNS providers such as Google DNS instead of performing full recursive lookups itself.

➔ Now, you have to check whether the BIND9 service is running:

Copied!
sudo systemctl status bind9

After running, you should see:

Copied!
sudo systemctl status bind9

Step 3: Configure Forwarders

➔ Open the BIND options file:

Copied!
sudo nano /etc/bind/named.conf.options

➔ Locate the forwarders section and update it:

Copied!
options {
    directory “/var/cache/bind”;

    forwarders {
        8.8.8.8;
        8.8.4.4;
    };

    dnssec-validation auto;
    listen-on { any; };
};

Once done, save and close the file.

Step 4: Create a DNS Zone

A DNS zone tells BIND which domain it is responsible for managing.

➔ You have to edit the local zones configuration file:

Copied!
sudo nano /etc/bind/named.conf.local

➔ You have to add:

Copied!
zone “example.com” {
    type master;
    file “/etc/bind/zones/db.example.com”;
};

eplace example.com with your actual domain.

Step 5: Create a Zone Directory

Keeping zone files in a separate directory improves organization and simplifies management as additional domains are added.

Create a dedicated directory for zone files:

Copied!
sudo mkdir /etc/bind/zones

Step 6: Create the Forward Zone File

The zone file contains the actual DNS records that clients will query, such as A records, NS records, and mail server entries.

➔ You have to copy the default template:

Copied!
sudo cp /etc/bind/db.local /etc/bind/zones/db.example.com

➔ You have to edit the file:

Copied!
sudo nano /etc/bind/zones/db.example.com

Example configuration:

Copied!
$TTL 86400

@ IN SOA ns1.example.com. admin.example.com. (
    2026060101
    3600
    1800
    604800
    86400 )

@      IN NS ns1.example.com.
ns1   IN A 192.168.1.10
@      IN A 192.168.1.10
www   IN A 192.168.1.10
mail  IN A 192.168.1.20

★ Quick Tip

Whenever you make changes to a zone file, increase the Serial Number. This helps secondary DNS servers recognize that updates are available.

Step 7: Validate Configuration Files

Validation helps identify syntax errors before restarting the DNS service, preventing unnecessary downtime.

➔ You have to check the main BIND configuration:

Copied!
sudo named-checkconf

➔ Validate the zone file:

Copied!
sudo named-checkzone example.com /etc/bind/zones/db.example.com

➔ Once done, your expected output would be:

Copied!
OK

Step 8: Restart BIND9

Restarting BIND9 reloads all DNS zones and configuration files into memory.

➔ Now, you have to apply the new configuration:

Copied!
sudo systemctl restart bind9

➔ Verify service status:

Copied!
sudo systemctl status bind9

Step 9: Allow DNS Through Firewall

If your Linux server has a firewall enabled (such as UFW), you must allow DNS traffic so that other devices can send DNS requests to your BIND9 server.

Run the following commands:

Copied!
sudo ufw allow 53/tcp
sudo ufw allow 53/udp

Step 10: Test DNS Resolution

Testing confirms that BIND9 is responding correctly and serving records from your zone file.

➔ Install DNS utilities if required:

Copied!
sudo apt install dnsutils -y

➔ Now, test your DNS server:

Copied!
nslookup example.com localhost

Also Read: How to Change Root Password in Linux?


Small Administrator Tips

➔ Always keep a backup of your zone files before making changes.

➔ Use meaningful hostnames such as ns1, mail, and www.

➔ Regularly monitor logs:

Copied!
sudo journalctl -u bind9

➔ Keep BIND9 up to date to receive security patches and performance improvements.

➔ If hosting public DNS, use proper DNSSEC and access controls for enhanced security.


Also Read: How to Kill a Process in Linux?


Conclusion

Configuring a DNS server in Linux is more than just pointing a system to a nameserver; it is about ensuring reliable communication between users, applications, and services. In this guide, we have covered both effective approaches: configuring DNS resolution via /etc/resolv.conf and setting up a dedicated DNS server using BIND9 to host and manage your own DNS records.

While updating /etc/resolv.conf helps a Linux system resolve domain names using external DNS servers, and BIND9 gives you complete control over DNS management, allowing you to create zones, manage records, and serve DNS requests for your domains. Together, these methods provide the foundation needed to build a stable and efficient DNS environment.

As you continue managing your Linux server, remember to validate configuration changes, test DNS resolution after every update, and keep your DNS records organized and up to date. A properly configured DNS server not only improves network reliability but also ensures that websites, applications, and email services operate smoothly without interruption.