Cara Setup DNS Server Menggunakan Dnsmasq

Di infrastruktur jaringan lokal, DNS server berperan penting buat mengarahkan domain ke IP tertentu, baik untuk keperluan internal network, blocking iklan, atau sekedar mempermudah akses ke server-server lokal tanpa harus hafal IP address.

Dnsmasq adalah solusi yang ringan, cepat, dan mudah dikonfigurasi buat kebutuhan DNS dan DHCP server di jaringan kecil hingga menengah.

Panduan ini menjelaskan konsep dasar, kelebihan dnsmasq, dan langkah setup lengkap dari instalasi sampai testing.

Hal yang Perlu Dipahami Sebelum Setup

1. Apa itu Dnsmasq?

Dnsmasq adalah lightweight DNS forwarder dan DHCP server yang umum digunakan di Linux.

Fitur utama:

  • DNS caching → mempercepat query berulang
  • DNS forwarding → meneruskan query ke upstream DNS (Google, Cloudflare, dll)
  • Local DNS records → buat domain custom untuk internal network (misal: server.local, nas.home)
  • DHCP server → opsional, bisa handle IP assignment juga

2. Kapan Pakai Dnsmasq?

SkenarioCocok?
Home lab / jaringan rumah✅ Sangat cocok
Small office (< 50 device)✅ Cocok
Enterprise besar❌ Better pakai BIND/Unbound
Butuh DNS filtering/blocking✅ Bisa dikombinasi

3. Port yang Digunakan

Dnsmasq menggunakan port standar:

  • Port 53 (UDP/TCP) → DNS queries
  • Port 67/68 (UDP) → DHCP (jika diaktifkan)

⚠️ Pastikan tidak ada service lain yang menggunakan port 53 (seperti systemd-resolved) sebelum install.

Prosedur Instalasi dan Konfigurasi

Step 1 — Install Dnsmasq

Jalankan sesuai distro Linux yang digunakan:

Ubuntu/Debian:

sudo apt update
sudo apt install dnsmasq -y

CentOS/RHEL/Rocky:

sudo dnf install dnsmasq -y
# atau untuk versi lama:
sudo yum install dnsmasq -y

Arch Linux:

sudo pacman -S dnsmasq

Step 2 — Handle Port Conflict (systemd-resolved)

Di Ubuntu modern, systemd-resolved biasanya sudah aktif dan menggunakan port 53.

Cek dulu:

sudo lsof -i :53

Kalau ada systemd-resolve, ada 2 opsi:

Opsi A: Disable systemd-resolved

sudo systemctl stop systemd-resolved
sudo systemctl disable systemd-resolved

Edit /etc/resolv.conf supaya tetap punya nameserver:

sudo rm /etc/resolv.conf
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf

Opsi B: Konfigurasi dnsmasq di port berbeda (untuk internal only)

Edit /etc/dnsmasq.conf, tambahkan:

port=5353

Tapi ini kurang praktis untuk network-wide DNS.

📌 Rekomendasi: Opsi A untuk DNS server dedicated.

Step 3 — Konfigurasi Dnsmasq

Backup konfigurasi default dulu:

sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.backup

Edit file konfigurasi:

sudo nano /etc/dnsmasq.conf

Konfigurasi dasar yang direkomendasikan:

# Interface yang dipakai (sesuaikan dengan network interface kamu)
interface=eth0
# atau kalau mau listen di semua interface:
# listen-address=127.0.0.1,192.168.1.10

# Jangan listen di interface yang tidak punya IP
bind-interfaces

# Upstream DNS servers (Google + Cloudflare)
server=8.8.8.8
server=8.8.4.4
server=1.1.1.1

# DNS cache size (optimasi)
cache-size=1000

# Log queries (opsional, untuk debugging)
# log-queries
# log-facility=/var/log/dnsmasq.log

# Local domain
domain=home.local

# Expand hosts dengan domain
expand-hosts

# ===== CUSTOM DNS RECORDS =====

# Format: address=/domain/IP
# Contoh untuk server internal:
address=/router.home/192.168.1.1
address=/nas.home/192.168.1.10
address=/server.local/192.168.1.50
address=/pihole.home/192.168.1.100

# Wildcard subdomain (semua *.lab.local ke IP yang sama)
address=/lab.local/192.168.1.20

# Blocking domain (arahkan ke localhost/0.0.0.0)
address=/iklan-annoying.com/0.0.0.0
address=/tracker.spyware.net/0.0.0.0

Penjelasan singkat:

  • interface → network interface yang menerima DNS query
  • server= → upstream DNS untuk domain yang tidak ada di local records
  • address=/domain/IP → buat custom A record
  • cache-size → jumlah query yang di-cache untuk performa

Step 4 — Konfigurasi /etc/hosts (Opsional)

Dnsmasq juga bisa membaca dari /etc/hosts. Ini berguna untuk management yang lebih sederhana:

Edit /etc/hosts:

sudo nano /etc/hosts

Tambahkan:

192.168.1.1     router router.home
192.168.1.10    nas nas.home storage.home
192.168.1.50    webserver server.local

Dengan expand-hosts dan domain=home.local di dnsmasq.conf, maka:

  • nas → resolves ke nas.home.local
  • router → resolves ke router.home.local

Step 5 — Verifikasi Konfigurasi

Cek apakah konfigurasi valid:

sudo dnsmasq --test --conf-file=/etc/dnsmasq.conf

Output yang diharapkan:

dnsmasq: syntax check OK.

Kalau ada error, dnsmasq akan kasih tahu baris berapa masalahnya.

Step 6 — Start dan Enable Service

sudo systemctl restart dnsmasq
sudo systemctl enable dnsmasq

Cek status service:

sudo systemctl status dnsmasq

Pastikan active (running) dan tidak ada error.

Step 7 — Konfigurasi Firewall

Buka port 53 untuk DNS queries:

UFW (Ubuntu/Debian):

sudo ufw allow 53/tcp
sudo ufw allow 53/udp
sudo ufw reload

Firewalld (RHEL/CentOS):

sudo firewall-cmd --permanent --add-service=dns
sudo firewall-cmd --reload

Iptables:

sudo iptables -A INPUT -p tcp --dport 53 -j ACCEPT
sudo iptables -A INPUT -p udp --dport 53 -j ACCEPT

Step 8 — Testing DNS Server

Test dari server itu sendiri:

# Test resolve domain external
dig @localhost google.com

# Test resolve domain internal
dig @localhost nas.home

# Test reverse DNS (kalau dikonfigurasi)
dig @localhost -x 192.168.1.10

Test dari client lain:

Di komputer client, ubah DNS server ke IP dnsmasq:

Linux (temp):

sudo nano /etc/resolv.conf
# Tambahkan:
nameserver 192.168.1.10  # IP server dnsmasq

Windows:

  1. Control Panel → Network Connections
  2. Properties → IPv4 → Use the following DNS server addresses
  3. Masukkan IP dnsmasq

Lalu test:

ping nas.home
nslookup server.local

Konfigurasi DHCP Server (Opsional)

Kalau mau dnsmasq juga handle DHCP, tambahkan di /etc/dnsmasq.conf:

# DHCP Range: IP mulai, IP akhir, lease time
dhcp-range=192.168.1.100,192.168.1.200,12h

# Default gateway
dhcp-option=3,192.168.1.1

# DNS server yang diberikan ke client (dirinya sendiri)
dhcp-option=6,192.168.1.10

# Static lease (IP tetap berdasarkan MAC)
dhcp-host=AA:BB:CC:DD:EE:FF,printer,192.168.1.50,infinite
dhcp-host=11:22:33:44:55:66,nas,192.168.1.10,infinite

Restart dnsmasq:

sudo systemctl restart dnsmasq

Troubleshooting Umum

Dnsmasq failed to start

# Cek error detail
sudo journalctl -u dnsmasq -n 50 --no-pager

# Cek port conflict
sudo netstat -tulpn | grep :53

Client tidak bisa resolve

  • Pastikan firewall port 53 terbuka
  • Cek SELinux (jika aktif): sudo setsebool -P dnsmasq_enable_ldap on
  • Verifikasi dnsmasq listen di interface yang benar: sudo ss -tulpn | grep dnsmasq

Query lambat

  • Tambahkan cache: cache-size=5000
  • Gunakan upstream DNS yang cepat (1.1.1.1, 8.8.8.8)
  • Enable no-resolv dan definisikan server= manual untuk menghindari resolv.conf yang lambat

Catatan Penting

  • Backup konfigurasi sebelum melakukan perubahan besar
  • Untuk network production, pertimbangkan setup secondary DNS untuk redundancy
  • Dnsmasq bukan pengganti BIND untuk enterprise-grade DNS infrastructure
  • Log query bisa membesar cepat, jangan lupa setup logrotate kalau diaktifkan
  • Untuk blocking iklan yang lebih advanced, kombinasikan dengan Pi-hole (yang juga menggunakan dnsmasq di belakangnya)

Leave a Reply

Your email address will not be published. Required fields are marked *