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:
| 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:
| Provider | Primary DNS | Secondary DNS |
| Google DNS | 8.8.8.8 | 8.8.4.4 |
| Cloudflare DNS | 1.1.1.1 | 1.0.0.1 |
| OpenDNS | 208.67.222.222 | 208.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:
| 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:
| 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
| 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:
| 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:
| sudo systemctl status bind9 |
After running, you should see:
| sudo systemctl status bind9 |
Step 3: Configure Forwarders
➔ Open the BIND options file:
| sudo nano /etc/bind/named.conf.options |
➔ Locate the forwarders section and update it:
|
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:
| sudo nano /etc/bind/named.conf.local |
➔ You have to add:
|
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:
| 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:
| sudo cp /etc/bind/db.local /etc/bind/zones/db.example.com |
➔ You have to edit the file:
| sudo nano /etc/bind/zones/db.example.com |
Example configuration:
|
$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:
| sudo named-checkconf |
➔ Validate the zone file:
| sudo named-checkzone example.com /etc/bind/zones/db.example.com |
➔ Once done, your expected output would be:
| OK |
Step 8: Restart BIND9
Restarting BIND9 reloads all DNS zones and configuration files into memory.
➔ Now, you have to apply the new configuration:
| sudo systemctl restart bind9 |
➔ Verify service status:
| 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:
| 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:
| sudo apt install dnsutils -y |
➔ Now, test your DNS server:
| 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:
| 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.
