How to Set Up BIND DNS on Ubuntu
How to Set Up BIND DNS on Ubuntu
Introduction
DNS (Domain Name System) is an important part of the internet. It translates human-readable domain names, such as example.com, into IP addresses that computers use to communicate with each other.
BIND (Berkeley Internet Name Domain) is one of the most widely used DNS server software packages. It allows administrators to manage DNS records, configure DNS zones, and provide domain name resolution.
In this guide, we will install BIND on Ubuntu, configure a DNS zone, understand common DNS records, and explore how primary and secondary DNS servers keep their information synchronized.
Understanding BIND DNS
BIND is open-source DNS server software commonly used on Linux servers.
It can perform several DNS-related functions:
- Authoritative DNS server: Stores DNS records for a domain and provides official answers to queries about that domain.
- Caching DNS server: Stores previously received DNS answers temporarily to reduce repeated queries.
- Primary DNS server: Maintains the main copy of a DNS zone.
- Secondary DNS server: Receives and maintains a copy of a zone from a primary server.
BIND can be configured to perform more than one of these roles, depending on the requirements.
Installing BIND on Ubuntu
Before installing BIND, update the Ubuntu package list.
sudo apt update
Install BIND and the DNS utilities package:
sudo apt install bind9 bind9-utils dnsutils -y
Check the BIND service status:
sudo systemctl status bind9
If the service is not running, start and enable it:
sudo systemctl enable --now bind9
Check whether BIND is listening on DNS port 53:
sudo ss -tulpn | grep :53
DNS normally uses both UDP and TCP on port 53.
If UFW is enabled, allow DNS traffic:
sudo ufw allow 53/udp
sudo ufw allow 53/tcp
Only expose DNS to the networks that need to access your server. A public authoritative DNS server should not accidentally become an open recursive resolver.
Understanding DNS Records
DNS records contain information about a domain, such as its IP address, mail server, and authoritative name servers.
Some commonly used DNS record types are:
| Record | Purpose | Example |
|---|---|---|
| A | Maps a domain to an IPv4 address | example.com → 192.0.2.10 |
| AAAA | Maps a domain to an IPv6 address | example.com → IPv6 address |
| CNAME | Creates an alias for another hostname | www → example.com |
| MX | Specifies mail servers for a domain | mail.example.com |
| NS | Identifies authoritative name servers | ns1.example.com |
| PTR | Maps an IP address back to a hostname | Reverse DNS |
| TXT | Stores text information, often used for verification and email security | SPF records |
| SOA | Contains important information about a DNS zone | Primary server and serial number |
These records are stored in DNS zone files and are used by DNS servers to answer queries.
Understanding DNS Zones
A DNS zone is a portion of the DNS namespace managed by a particular DNS administrator or organization.
For example, a DNS zone for example.com can contain records for:
example.comwww.example.commail.example.comns1.example.com
A zone file contains the DNS records for that zone.
BIND uses zone configuration entries to identify the zone and its corresponding file.
There are two common types of DNS zones:
Forward Lookup Zone
A forward lookup zone resolves domain names into IP addresses.
For example:
www.example.com → 192.0.2.10
Reverse Lookup Zone
A reverse lookup zone resolves IP addresses into hostnames using PTR records.
For example:
192.0.2.10 → www.example.com
Both types are useful when managing DNS infrastructure.
Configuring a Primary DNS Server
A primary DNS server maintains the original zone data and provides authoritative DNS answers for its configured domain.
For this example, we will use:
| Configuration | Example |
|---|---|
| Domain | example.com |
| Primary DNS server | ns1.example.com |
| Server IP address | 192.0.2.10 |
| Zone file | /etc/bind/db.example.com |
The IP address used here is reserved for documentation. Replace it with the actual IP address of your server.
Step 1: Configure the DNS Zone
Open the local BIND configuration file:
sudo nano /etc/bind/named.conf.local
Add the following configuration:
zone "example.com" {
type primary;
file "/etc/bind/db.example.com";
allow-transfer { 192.0.2.11; };
also-notify { 192.0.2.11; };
};
Here:
example.comis the domain being managed.type primaryidentifies this server as the primary DNS server for the zone.filespecifies the location of the zone file.allow-transferpermits the specified secondary server to receive zone data.also-notifytells the secondary server when zone data changes.
The secondary server IP address in this example is 192.0.2.11.
Step 2: Create the Zone File
Create the zone file:
sudo nano /etc/bind/db.example.com
Add the following records:
$TTL 86400
@ IN SOA ns1.example.com. hostmaster.example.com. (
2026111801 ; Serial
3600 ; Refresh
900 ; Retry
604800 ; Expire
86400 ; Negative Cache TTL
)
@ IN NS ns1.example.com.
@ IN NS ns2.example.com.
@ IN A 192.0.2.10
ns1 IN A 192.0.2.10
ns2 IN A 192.0.2.11
www IN CNAME example.com.
mail IN A 192.0.2.10
@ IN MX 10 mail.example.com.
This zone file defines the domain’s DNS information.
The SOA record contains important zone information, including the primary server, administrator contact, and serial number.
Important: Increase the serial number whenever you update the zone file. Secondary DNS servers use this number to identify changes.
Step 3: Validate the Configuration
Before restarting BIND, check the configuration:
sudo named-checkconf
Check the zone file:
sudo named-checkzone example.com /etc/bind/db.example.com
If the configuration is correct, the zone check should return an OK message.
Restart BIND to apply the changes:
sudo systemctl restart bind9
Check the service:
sudo systemctl status bind9
Understanding DNS Replication
DNS replication is the process of keeping DNS zone information synchronized between primary and secondary DNS servers.
A primary server maintains the original zone data, while a secondary server receives and stores a copy of that data.
This helps improve DNS availability and provides another authoritative server if the primary becomes unavailable.
DNS replication commonly uses zone transfers:
- AXFR: Transfers the complete DNS zone.
- IXFR: Transfers only the changes made since a previous zone version, when supported.
The SOA serial number helps secondary servers determine whether the zone has changed.
How DNS Replication Works
- The administrator updates a DNS record on the primary server.
- The administrator increases the zone’s SOA serial number.
- The primary server notifies the secondary server about the change.
- The secondary server checks the SOA serial number.
- If the zone has a newer version, the secondary server requests a zone transfer.
- The secondary server updates its copy of the zone.
This process keeps both DNS servers synchronized.
Configuring a Secondary DNS Server
A secondary DNS server maintains a copy of the zone data received from the primary server.
For this example, we will use:
| Configuration | Example |
|---|---|
| Primary DNS server | 192.0.2.10 |
| Secondary DNS server | 192.0.2.11 |
| Domain | example.com |
First, install BIND on the secondary Ubuntu server:
sudo apt update
sudo apt install bind9 bind9-utils dnsutils -y
Open the local configuration file:
sudo nano /etc/bind/named.conf.local
Add the following configuration:
zone "example.com" {
type secondary;
file "/var/cache/bind/db.example.com";
primaries { 192.0.2.10; };
};
Here:
type secondaryidentifies the server as a secondary DNS server.filespecifies where the transferred zone data will be stored.primariesidentifies the primary DNS server from which zone data is received.
The secondary server will request the zone data from the primary server.
Restart BIND:
sudo systemctl restart bind9
Check the service:
sudo systemctl status bind9
Make sure DNS traffic on TCP and UDP port 53 is allowed between the two servers.
You can also check the BIND logs to confirm that the zone transfer completed successfully.
How DNS Resolution Works
DNS resolution is the process of finding the IP address associated with a domain name.
For example, when someone enters www.example.com into a browser, the system needs to find the corresponding IP address before connecting to the website.
The resolution process generally works as follows:
- Browser cache: The browser checks whether it already has a saved DNS result.
- Operating system cache: If the browser does not have the answer, the operating system checks its DNS cache.
- Recursive DNS resolver: The query is sent to a recursive resolver, usually provided by an ISP or a public DNS service.
- Root DNS server: If the resolver does not have the answer cached, it queries a root DNS server to find the relevant top-level domain servers.
- TLD DNS server: The TLD server, such as the
.comDNS server, provides information about the domain’s authoritative name servers. - Authoritative DNS server: The resolver queries the authoritative server for the domain.
- IP address returned: The authoritative server returns the requested DNS record, and the resolver sends the answer back to the client.
The resolver can cache the answer for the record’s TTL (Time to Live), which helps reduce repeated DNS queries.
Testing DNS Configuration
After configuring BIND, you can use dig to test DNS records and verify that the server is responding correctly.
Check an A record:
dig @127.0.0.1 example.com A
Check the name server records:
dig @127.0.0.1 example.com NS
Check the mail server record:
dig @127.0.0.1 example.com MX
Query the primary DNS server directly:
dig @192.0.2.10 example.com
Trace the DNS resolution process:
dig +trace example.com
These commands help verify DNS responses and identify configuration problems.
For a public domain, also make sure the domain registrar’s nameserver settings point to your authoritative DNS servers. If you use nameservers such as ns1.example.com and ns2.example.com, the required glue records may also need to be configured at the registrar.
Managing the BIND Service
You can use the following commands to manage the BIND service on Ubuntu.
| Command | Description |
|---|---|
sudo systemctl start bind9 |
Starts BIND |
sudo systemctl stop bind9 |
Stops BIND |
sudo systemctl restart bind9 |
Restarts BIND |
sudo systemctl reload bind9 |
Reloads BIND configuration |
sudo systemctl status bind9 |
Checks service status |
sudo named-checkconf |
Validates BIND configuration |