Configuring DNS on Linux is crucial for network connectivity, security, and performance. Whether you are setting up a new server, troubleshooting network issues, or optimising your connection, it’s important to know how to modify DNS settings.
By default, Linux systems use DNS servers provided by the network or ISP. However, in many cases, you may need to change some settings to use a faster or more secure DNS provider like Google, Cloudflare, or OpenDNS.
This guide will explore the different ways to configure DNS on Linux, ensuring smooth and efficient network communication.
Also Read: How to Create a New Group in Linux?
Steps to Configure DNS on Linux
➢ Configure DNS on Linux by Setting Up the Named Configuration Files.
This method involves setting up a DNS server using BIND (Berkeley Internet Name Domain). BIND is one of the most widely used DNS software in Linux, allowing administrators to manage domain resolution effectively.
Configuring named files lets you control how your DNS server handles requests and forwards queries.
Step 1: Install BIND
You have to run the following command to install BIND on your system:
- For Ubuntu/Debian
sudo apt update && sudo apt install bind9 -y |
- For CentOS/RHEL
sudo yum install bind bind-utils -y |
This command updates the package list and installs BIND, the primary DNS software for Linux.
Step 2: Configure Named Files
➔ You have to edit the main BIND configuration file:
- For Ubuntu/Debian
sudo nano /etc/bind/named.conf.options |
- For CentOS/RHEL
sudo nano /etc/named.conf |
➔ After this, Add or modify the following section:
options { directory “/var/cache/bind”; recursion yes; allow-query { any; }; forwarders { 8.8.8.8; 8.8.4.4; }; }; |
This configuration allows DNS queries from any source to be forwarded to Google’s public DNS servers.
➔ Once done, Just save and exit.
Step 3: Restart BIND Service
➔ For Ubuntu/Debian
sudo systemctl restart bind9 |
➔ For CentOS/RHEL
sudo systemctl restart named |
Also Read: How to Check Whether Port is Open or Not in Linux?
➢ Configure DNS on Linux by Adding Forward and Reverse Lookup.
DNS resolution works through forward and reverse lookups. A forward lookup resolves a domain name to an IP address, while a reverse lookup resolves an IP address to a domain name. Configuring both ensures proper DNS functionality and helps with troubleshooting network issues.
Step 1: Configure Forward Lookup Zone
➔ Firstly, you have to edit the zone file:
➔ For Ubuntu/Debian
sudo nano /etc/bind/named.conf.local |
➔ CentOS/RHEL
sudo nano /etc/named.conf.local |
➔ You have to add the following lines to define a forward lookup:
zone “example.com” IN { type master; file “/etc/bind/db.example.com”; }; |
*Note: You have to replace your domain with an example.com
➔ Once done, Just save and exit.
➔ Now, you have to create the zone file:
sudo nano /etc/bind/db.example.com |
*Note: You have to replace your domain with an example.com
➔ Now, you have to add the following content. This file defines the authoritative name server and maps domain names to IP addresses.
$TTL 86400 @ IN SOA ns1.example.com. admin.example.com. ( 20240321 ; Serial 3600 ; Refresh 1800 ; Retry 604800 ; Expire 86400 ) ; Minimum TTL @ IN NS ns1.example.com. ns1 IN A 192.168.1.1 www IN A 192.168.1.2 |
*Note: You have to replace your domain with an example.com
➔ Once done, Just save and exit.
Step 2: Configure Reverse Lookup Zone
➔ Edit the zone file:
sudo nano /etc/bind/named.conf.local |
➔ You have to add the following content to define a reverse lookup zone for the 192.168.1.x network.
zone “1.168.192.in-addr.arpa” IN { type master; file “/etc/bind/db.192”; }; |
➔ Once done, Just save and exit.
➔ Create the reverse lookup file:
sudo nano /etc/bind/db.192 |
➔ You have to add the following to map IP addresses back to domain names.
$TTL 86400 @ IN SOA ns1.example.com. admin.example.com. ( 20240321 ; Serial 3600 ; Refresh 1800 ; Retry 604800 ; Expire 86400 ) ; Minimum TTL @ IN NS ns1.example.com. 1 IN PTR ns1.example.com. 2 IN PTR www.example.com. |
*Note: You have to replace your domain with an example.com
➔ Once done, Just save and exit.
Step 3: Restart BIND to Apply the Changes
➔ For Ubuntu/Debian
sudo systemctl restart bind9 |
➔ For CentOS/RHEL
sudo systemctl restart named |
Also Read: Know About the ls Command in Linux With Examples
➢ Configure DNS on Linux by Restarting the BIND Server and Checking Status.
Once you have configured DNS settings, it is essential to restart the BIND service to apply the changes. Additionally, checking the status ensures that BIND is running correctly and debugging any issues if required.
Step 1: Check BIND Status
➔ Run the following command to check the status, which helps confirm whether the service is running properly.
➔ For Ubuntu/Debian
sudo systemctl status bind9 |
➔ For CentOS/RHEL
sudo systemctl status named |
Step 3: Test DNS Resolution
➔ You have to use dig to check if your DNS is working:
dig example.com |
Or use nslookup:
nslookup example.com |
Also Read: How to Install RPM files on Different Linux Distributions?
➢ Configure DNS on Linux by Binding with the UFW Firewall.
The firewall must allow DNS queries for proper communication. UFW (Uncomplicated Firewall) is a simple and effective tool for managing firewall rules. Allowing DNS traffic ensures that your server can resolve and process domain requests.
Step 1: Allow DNS Traffic in UFW Firewall
Run the following commands to allow DNS through the firewall:
sudo ufw allow 53/tcp sudo ufw allow 53/udp |
Port 53 is used for DNS queries; It allows both TCP and UDP to ensure proper functionality.
Step 2: Reload UFW Rules
sudo ufw reload |
Reloading applies the new firewall rules.
Step 3: Verify UFW Rules
sudo ufw status |
This command checks if DNS traffic is allowed through the firewall.
Also Read: Create Zip File With Command in Linux With Examples
Conclusion
Mastering DNS configuration on Linux is like unlocking the map to seamless network communication! From setting up BIND to ensuring smooth lookups and securing traffic through the firewall, you have a solid foundation to manage DNS effectively.
By following these steps, your Linux system will resolve domains like a pro, making your network faster, more secure, and well-optimized. Whether setting up a new server or troubleshooting DNS issues, you’re now equipped to handle it confidently.
