> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/servers-configurations-debian/domain-name-system-dns.md).

# Domain Name System (DNS)

### Cache-Only DNS

A cache-only DNS server stores DNS query results temporarily to improve resolution speed and reduce network traffic.

#### Configuring a Cache Server

**1. Check Hostname**

```sh
hostname
```

**2. Change Hostname and Domain Name**

Edit the hostname file:

```sh
vim /etc/hostname  # hostname.domain-name.top_level_domain_name
```

**3. Modify DNS Configuration**

Edit the network interfaces file:

```sh
vim /etc/network/interfaces
```

Search for the DNS setting and enter the local IP:

```sh
DNS1=127.0.0.1
```

**4. Install Cache Tools**

```sh
apt install bind9
```

**5. Edit Configuration File**

```sh
vim /etc/bind/named.conf.options
```

Modify the following settings:

```sh
listen-on port 53 { 127.0.0.1; Your-IP; };
allow-query { localhost; query-resolve-ip; };
```

**6. Start the Cache Service**

```sh
systemctl enable bind9.service
systemctl restart bind9.service
```

**7. Enable DNS Port in Firewall**

```sh
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables-save > /etc/iptables/rules.v4
systemctl restart iptables
```

***

### Primary DNS (Master DNS)

A primary DNS server translates domain names into IP addresses, allowing users to access websites using human-readable addresses.

#### Configuration

**1. Edit Configuration File**

```sh
vim /etc/bind/named.conf.default-zones
```

**2. Define the Zone**

Refer to the image below for zone structure:&#x20;

<figure><img src="https://github.com/riteshs4hu/Linux-Servers/blob/main/Debian/DNS/images/Zone.png?raw=true" alt=""><figcaption></figcaption></figure>

Details to configure:

1. Zone Name
2. Zone Type
3. Zone File Name (Ensure the file extension matches the zone name)

**3. Create a Zone File**

```sh
cp -v /etc/bind/db.local /etc/bind/zone_file_name
```

**4. Edit the Zone File**

```sh
vim /etc/bind/zone_file_name
```

![Zone File](https://github.com/riteshs4hu/Linux-Servers/blob/main/Debian/DNS/images/ZoneFile.png?raw=true)

In this file:

* Define the primary name server.
* Define secondary name servers.
* Create DNS records.

**5. Start the DNS Service**

```sh
systemctl restart bind9
```

**6. Validate Configuration**

```sh
named-checkconf
named-checkzone zonename /etc/bind/zone_file_name
```

***

### Forwarders DNS Server

A forwarder DNS server redirects queries to an upstream DNS server to improve resolution efficiency.

**1. Edit Configuration File**

```sh
vim /etc/bind/named.conf.options
```

**2. Enable Forwarders**

Locate `// forwarders`, uncomment the line, and define the forwarder IP:

```sh
forwarders { forwarder_IP; };
```

**3. Verify Forwarder Queries**

```sh
tcpdump -n -t udp port 53
```
