CoreDNS is a free and open-source DNS Server. A flexible, extensible DNS server written in Go
1. Prerequisites
2. Supported Operating Systems
This guide supports installation on:
3. Installation
RHEL/CentOS/Rocky Linux/AlmaLinux
# Install EPEL repository if needed
sudo dnf install -y epel-release
# Install coredns
sudo dnf install -y coredns none
# Enable and start service
sudo systemctl enable --now coredns
# Configure firewall
sudo firewall-cmd --permanent --add-service=coredns
sudo firewall-cmd --reload
# Verify installation
coredns --version || systemctl status coredns
Debian/Ubuntu
# Update package index
sudo apt update
# Install coredns
sudo apt install -y coredns none
# Enable and start service
sudo systemctl enable --now coredns
# Configure firewall
sudo ufw allow 53
# Verify installation
coredns --version || systemctl status coredns
Arch Linux
# Install coredns
sudo pacman -S coredns
# Enable and start service
sudo systemctl enable --now coredns
# Verify installation
coredns --version || systemctl status coredns
Alpine Linux
# Install coredns
apk add --no-cache coredns
# Enable and start service
rc-update add coredns default
rc-service coredns start
# Verify installation
coredns --version || rc-service coredns status
openSUSE/SLES
# Install coredns
sudo zypper install -y coredns none
# Enable and start service
sudo systemctl enable --now coredns
# Configure firewall
sudo firewall-cmd --permanent --add-service=coredns
sudo firewall-cmd --reload
# Verify installation
coredns --version || systemctl status coredns
macOS
# Using Homebrew
brew install coredns
# Start service
brew services start coredns
# Verify installation
coredns --version
FreeBSD
# Using pkg
pkg install coredns
# Enable in rc.conf
echo 'coredns_enable="YES"' >> /etc/rc.conf
# Start service
service coredns start
# Verify installation
coredns --version || service coredns status
Windows
# Using Chocolatey
choco install coredns
# Or using Scoop
scoop install coredns
# Verify installation
coredns --version
Initial Configuration
Basic Configuration
# Create configuration directory if needed
sudo mkdir -p /etc/coredns
# Set up basic configuration
sudo tee /etc/coredns/coredns.conf << 'EOF'
# CoreDNS Configuration
. { forward . 8.8.8.8 9.9.9.9, cache 30 }
EOF
# Test configuration
sudo coredns -t || sudo coredns configtest
# Reload service
sudo systemctl reload coredns
Security Hardening
# Set appropriate permissions
sudo chown -R coredns:coredns /etc/coredns
sudo chmod 750 /etc/coredns
# Enable security features
# See security section for detailed hardening steps
5. Service Management
systemd (RHEL, Debian, Ubuntu, Arch, openSUSE)
# Enable service
sudo systemctl enable coredns
# Start service
sudo systemctl start coredns
# Stop service
sudo systemctl stop coredns
# Restart service
sudo systemctl restart coredns
# Reload configuration
sudo systemctl reload coredns
# Check status
sudo systemctl status coredns
# View logs
sudo journalctl -u coredns -f
OpenRC (Alpine Linux)
# Enable service
rc-update add coredns default
# Start service
rc-service coredns start
# Stop service
rc-service coredns stop
# Restart service
rc-service coredns restart
# Check status
rc-service coredns status
rc.d (FreeBSD)
# Enable in /etc/rc.conf
echo 'coredns_enable="YES"' >> /etc/rc.conf
# Start service
service coredns start
# Stop service
service coredns stop
# Restart service
service coredns restart
# Check status
service coredns status
launchd (macOS)
# Using Homebrew services
brew services start coredns
brew services stop coredns
brew services restart coredns
# Check status
brew services list | grep coredns
Windows Service Manager
# Start service
net start coredns
# Stop service
net stop coredns
# Using PowerShell
Start-Service coredns
Stop-Service coredns
Restart-Service coredns
# Check status
Get-Service coredns
Advanced Configuration
Performance Optimization
# Configure performance settings
cat >> /etc/coredns/coredns.conf << 'EOF'
. { forward . 8.8.8.8 9.9.9.9, cache 30 }
EOF
# Apply system tuning
sudo sysctl -w net.core.somaxconn=65535
sudo sysctl -w net.ipv4.tcp_max_syn_backlog=65535
# Restart service
sudo systemctl restart coredns
Clustering and High Availability
# Configure clustering (if supported)
# See official documentation for cluster setup
# Basic load balancing setup example
# Configure multiple instances on different ports
Reverse Proxy Setup
nginx Configuration
upstream coredns_backend {
server 127.0.0.1:53;
server 127.0.0.1:{default_port}1 backup;
}
server {
listen 80;
server_name coredns.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name coredns.example.com;
ssl_certificate /etc/ssl/certs/coredns.example.com.crt;
ssl_certificate_key /etc/ssl/private/coredns.example.com.key;
location / {
proxy_pass http://coredns_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# WebSocket support (if needed)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Apache Configuration
<VirtualHost *:80>
ServerName coredns.example.com
Redirect permanent / https://coredns.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName coredns.example.com
SSLEngine on
SSLCertificateFile /etc/ssl/certs/coredns.example.com.crt
SSLCertificateKeyFile /etc/ssl/private/coredns.example.com.key
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:53/
ProxyPassReverse / http://127.0.0.1:53/
# WebSocket support (if needed)
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://127.0.0.1:53/$1" [P,L]
</VirtualHost>
HAProxy Configuration
frontend coredns_frontend
bind *:80
bind *:443 ssl crt /etc/ssl/certs/coredns.pem
redirect scheme https if !{ ssl_fc }
default_backend coredns_backend
backend coredns_backend
balance roundrobin
option httpchk GET /health
server coredns1 127.0.0.1:53 check
server coredns2 127.0.0.1:{default_port}1 check backup
Security Configuration
Basic Security Setup
# Set appropriate permissions
sudo chown -R coredns:coredns /etc/coredns
sudo chmod 750 /etc/coredns
# Configure firewall
sudo firewall-cmd --permanent --add-service=coredns
sudo firewall-cmd --reload
# Enable SELinux policies (if applicable)
sudo setsebool -P httpd_can_network_connect on
# Configure fail2ban
sudo tee /etc/fail2ban/jail.d/coredns.conf << 'EOF'
[coredns]
enabled = true
port = 53
filter = coredns
logpath = /var/log/coredns/*.log
maxretry = 5
bantime = 3600
EOF
SSL/TLS Configuration
# Generate SSL certificates
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/coredns.key \
-out /etc/ssl/certs/coredns.crt
# Configure SSL in coredns
# See official documentation for SSL configuration
Database Setup
PostgreSQL Backend (if applicable)
# Create database and user
sudo -u postgres psql << EOF
CREATE DATABASE coredns_db;
CREATE USER coredns_user WITH ENCRYPTED PASSWORD 'secure_password';
GRANT ALL PRIVILEGES ON DATABASE coredns_db TO coredns_user;
EOF
# Configure coredns to use PostgreSQL
# See official documentation for database configuration
MySQL/MariaDB Backend (if applicable)
# Create database and user
sudo mysql << EOF
CREATE DATABASE coredns_db;
CREATE USER 'coredns_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON coredns_db.* TO 'coredns_user'@'localhost';
FLUSH PRIVILEGES;
EOF
Performance Optimization
System Tuning
# Kernel parameters
sudo tee -a /etc/sysctl.conf << EOF
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.ip_local_port_range = 1024 65535
net.core.netdev_max_backlog = 5000
vm.swappiness = 10
EOF
sudo sysctl -p
# CoreDNS specific tuning
. { forward . 8.8.8.8 9.9.9.9, cache 30 }
Resource Limits
# Configure system limits
sudo tee -a /etc/security/limits.conf << EOF
coredns soft nofile 65535
coredns hard nofile 65535
coredns soft nproc 32768
coredns hard nproc 32768
EOF
Monitoring
Prometheus Integration
# prometheus.yml configuration
scrape_configs:
- job_name: 'coredns'
static_configs:
- targets: ['localhost:53']
metrics_path: '/metrics'
Health Checks
# Basic health check script
#!/bin/bash
if systemctl is-active --quiet coredns; then
echo "CoreDNS is running"
exit 0
else
echo "CoreDNS is not running"
exit 1
fi
Log Monitoring
# Configure log rotation
sudo tee /etc/logrotate.d/coredns << 'EOF'
/var/log/coredns/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 coredns coredns
postrotate
systemctl reload coredns > /dev/null 2>&1 || true
endscript
}
EOF
9. Backup and Restore
Backup Script
#!/bin/bash
# CoreDNS backup script
BACKUP_DIR="/backup/coredns"
DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p "$BACKUP_DIR"
# Stop service (if required)
systemctl stop coredns
# Backup configuration
tar -czf "$BACKUP_DIR/coredns-config-$DATE.tar.gz" /etc/coredns
# Backup data (adjust paths as needed)
tar -czf "$BACKUP_DIR/coredns-data-$DATE.tar.gz" /var/lib/coredns
# Start service
systemctl start coredns
# Clean old backups (keep 30 days)
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +30 -delete
echo "Backup completed: $BACKUP_DIR"
Restore Procedure
# Stop service
sudo systemctl stop coredns
# Restore configuration
sudo tar -xzf /backup/coredns/coredns-config-*.tar.gz -C /
# Restore data
sudo tar -xzf /backup/coredns/coredns-data-*.tar.gz -C /
# Set permissions
sudo chown -R coredns:coredns /etc/coredns
sudo chown -R coredns:coredns /var/lib/coredns
# Start service
sudo systemctl start coredns
6. Troubleshooting
Common Issues
1. Service won't start:
# Check logs
sudo journalctl -u coredns -n 100
sudo tail -f /var/log/coredns/*.log
# Check configuration
sudo coredns -t || sudo coredns configtest
# Check permissions
ls -la /etc/coredns
ls -la /var/lib/coredns
2. Connection refused:
# Check if service is listening
sudo ss -tlnp | grep 53
sudo netstat -tlnp | grep 53
# Check firewall
sudo firewall-cmd --list-all
sudo iptables -L -n
# Test connection
telnet localhost 53
nc -zv localhost 53
3. Performance issues:
# Check resource usage
top -p $(pgrep coredns)
htop -p $(pgrep coredns)
# Check connections
ss -ant | grep :53 | wc -l
# Monitor I/O
iotop -p $(pgrep coredns)
Debug Mode
# Run in debug mode
sudo coredns -d
# or
sudo coredns debug
# Increase log verbosity
# Edit configuration to enable debug logging
Integration Examples
Docker Compose
version: '3.8'
services:
coredns:
image: coredns:latest
container_name: coredns
ports:
- "53:53"
volumes:
- ./config:/etc/coredns
- ./data:/var/lib/coredns
environment:
- coredns_CONFIG=/etc/coredns/coredns.conf
restart: unless-stopped
networks:
- coredns_net
networks:
coredns_net:
driver: bridge
Kubernetes Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns
spec:
replicas: 3
selector:
matchLabels:
app: coredns
template:
metadata:
labels:
app: coredns
spec:
containers:
- name: coredns
image: coredns:latest
ports:
- containerPort: 53
volumeMounts:
- name: config
mountPath: /etc/coredns
volumes:
- name: config
configMap:
name: coredns-config
---
apiVersion: v1
kind: Service
metadata:
name: coredns
spec:
selector:
app: coredns
ports:
- port: 53
targetPort: 53
type: LoadBalancer
Ansible Playbook
---
- name: Install and configure CoreDNS
hosts: all
become: yes
tasks:
- name: Install coredns
package:
name: coredns
state: present
- name: Configure coredns
template:
src: coredns.conf.j2
dest: /etc/coredns/coredns.conf
owner: coredns
group: coredns
mode: '0640'
notify: restart coredns
- name: Start and enable coredns
systemd:
name: coredns
state: started
enabled: yes
handlers:
- name: restart coredns
systemd:
name: coredns
state: restarted
Maintenance
Update Procedures
# RHEL/CentOS/Rocky/AlmaLinux
sudo dnf update coredns
# Debian/Ubuntu
sudo apt update && sudo apt upgrade coredns
# Arch Linux
sudo pacman -Syu coredns
# Alpine Linux
apk update && apk upgrade coredns
# openSUSE
sudo zypper update coredns
# FreeBSD
pkg update && pkg upgrade coredns
# Always backup before updates
tar -czf /backup/coredns-pre-update-$(date +%Y%m%d).tar.gz /etc/coredns
# Restart after updates
sudo systemctl restart coredns
Regular Maintenance Tasks
# Clean logs
find /var/log/coredns -name "*.log" -mtime +30 -delete
# Verify integrity
sudo coredns --verify || sudo coredns check
# Update databases (if applicable)
sudo coredns-update-db
# Optimize performance
sudo coredns-optimize
# Check for security updates
sudo coredns --security-check
Additional Resources
---
Note: This guide is part of the HowToMgr collection. Always refer to official documentation for the most up-to-date information.