Skip to content

Instantly share code, notes, and snippets.

@radwebhosting
Created March 27, 2025 05:04
Show Gist options
  • Save radwebhosting/e64e4d827234c8140f9c5a0055c79cc3 to your computer and use it in GitHub Desktop.
Save radwebhosting/e64e4d827234c8140f9c5a0055c79cc3 to your computer and use it in GitHub Desktop.
Here's a Bash automation script to set up a PowerDNS Master server with a MySQL backend on Ubuntu 22.04+.
#!/bin/bash
# PowerDNS Master Setup Script for Ubuntu
# === Configurable Variables ===
DB_NAME="powerdns"
DB_USER="pdns"
DB_PASS="StrongPasswordHere"
MYSQL_ROOT_PASS="RootPasswordHere"
# === Update and install packages ===
echo "[+] Updating system and installing packages..."
apt update && apt upgrade -y
apt install -y pdns-server pdns-backend-mysql mariadb-server wget curl unzip
# === Secure MySQL ===
echo "[+] Securing MariaDB..."
mysql -u root <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PASS';
DELETE FROM mysql.user WHERE User='';
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
FLUSH PRIVILEGES;
EOF
# === Create PowerDNS Database ===
echo "[+] Creating PowerDNS database and user..."
mysql -u root -p"$MYSQL_ROOT_PASS" <<EOF
CREATE DATABASE IF NOT EXISTS $DB_NAME;
CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';
GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost';
FLUSH PRIVILEGES;
EOF
# === Download and import schema ===
echo "[+] Downloading and importing PowerDNS schema..."
wget -q https://raw.githubusercontent.com/PowerDNS/pdns/master/modules/gmysqlbackend/schema/schema.mysql.sql -O /tmp/schema.mysql.sql
mysql -u $DB_USER -p"$DB_PASS" $DB_NAME < /tmp/schema.mysql.sql
# === Configure PowerDNS ===
echo "[+] Configuring PowerDNS..."
PDNS_CONF="/etc/powerdns/pdns.conf"
sed -i 's/^launch=.*/launch=gmysql/' $PDNS_CONF
# Remove old gmysql lines if they exist
sed -i '/^gmysql-/d' $PDNS_CONF
# Append gmysql config
cat <<EOF >> $PDNS_CONF
# gmysql backend config
gmysql-host=127.0.0.1
gmysql-user=$DB_USER
gmysql-password=$DB_PASS
gmysql-dbname=$DB_NAME
EOF
# === Restart PowerDNS ===
echo "[+] Restarting PowerDNS..."
systemctl restart pdns
systemctl enable pdns
# === Done ===
echo ""
echo "✅ PowerDNS Master setup is complete."
echo "You can now add DNS zones directly into the '$DB_NAME' database."
@radwebhosting
Copy link
Author

📦 Usage

  1. Save the script:
nano setup-pdns-master.sh
  1. Paste the contents, save, and exit.
  2. Make it executable:
chmod +x setup-pdns-master.sh
  1. Run it as root or with sudo:
sudo ./setup-pdns-master.sh

📌 Notes

  • Be sure to replace StrongPasswordHere and RootPasswordHere with real passwords before running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment