Configure CockroachDB SSL encryption and security hardening

Advanced 45 min Jun 01, 2026 97 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up SSL/TLS encryption, authentication, and advanced security policies for CockroachDB 24.3 in production environments with certificate management and cluster monitoring.

Prerequisites

  • Root or sudo access to the server
  • At least 4GB of RAM and 20GB of disk space
  • Network connectivity between cluster nodes
  • Basic understanding of SSL/TLS certificates

What this solves

CockroachDB requires SSL/TLS encryption and proper authentication to secure distributed database clusters in production. This tutorial configures end-to-end encryption, certificate-based authentication, user management, and security monitoring for CockroachDB 24.3 clusters.

Step-by-step installation and configuration

Install CockroachDB 24.3 with security prerequisites

Download and install the latest CockroachDB binary with required security tools.

curl https://binaries.cockroachdb.com/cockroach-v24.3.0.linux-amd64.tgz | tar -xz
sudo cp -i cockroach-v24.3.0.linux-amd64/cockroach /usr/local/bin/
sudo chmod +x /usr/local/bin/cockroach
sudo apt update && sudo apt install -y ca-certificates openssl
curl https://binaries.cockroachdb.com/cockroach-v24.3.0.linux-amd64.tgz | tar -xz
sudo cp -i cockroach-v24.3.0.linux-amd64/cockroach /usr/local/bin/
sudo chmod +x /usr/local/bin/cockroach
sudo dnf update -y && sudo dnf install -y ca-certificates openssl

Create CockroachDB system user and directories

Set up dedicated user and secure directory structure for CockroachDB with proper permissions.

sudo useradd --system --shell /bin/bash --home /var/lib/cockroach --create-home cockroach
sudo mkdir -p /var/lib/cockroach/{certs,data,logs}
sudo chown -R cockroach:cockroach /var/lib/cockroach
sudo chmod 700 /var/lib/cockroach/certs
sudo chmod 755 /var/lib/cockroach/data
sudo chmod 755 /var/lib/cockroach/logs

Generate Certificate Authority (CA) certificate

Create a Certificate Authority for signing cluster and client certificates.

sudo -u cockroach cockroach cert create-ca \
    --certs-dir=/var/lib/cockroach/certs \
    --ca-key=/var/lib/cockroach/certs/ca.key \
    --key-size=4096 \
    --lifetime=8760h

Create node certificates for cluster nodes

Generate SSL certificates for each CockroachDB node in the cluster with proper hostnames.

sudo -u cockroach cockroach cert create-node \
    localhost \
    $(hostname -f) \
    $(hostname -i) \
    127.0.0.1 \
    203.0.113.10 \
    203.0.113.11 \
    203.0.113.12 \
    cockroach-1.example.com \
    cockroach-2.example.com \
    cockroach-3.example.com \
    --certs-dir=/var/lib/cockroach/certs \
    --ca-key=/var/lib/cockroach/certs/ca.key \
    --key-size=4096 \
    --lifetime=8760h

Create root client certificate

Generate client certificate for the root administrative user with proper permissions.

sudo -u cockroach cockroach cert create-client \
    root \
    --certs-dir=/var/lib/cockroach/certs \
    --ca-key=/var/lib/cockroach/certs/ca.key \
    --key-size=4096 \
    --lifetime=8760h
sudo chmod 600 /var/lib/cockroach/certs/client.root.key
sudo chmod 644 /var/lib/cockroach/certs/client.root.crt

Configure CockroachDB systemd service

Create systemd service file with security hardening and SSL configuration.

[Unit]
Description=CockroachDB distributed SQL database
Requires=network.target
After=network.target

[Service]
Type=notify
User=cockroach
Group=cockroach
ExecStart=/usr/local/bin/cockroach start \
    --certs-dir=/var/lib/cockroach/certs \
    --store=/var/lib/cockroach/data \
    --log-dir=/var/lib/cockroach/logs \
    --listen-addr=0.0.0.0:26257 \
    --http-addr=0.0.0.0:8080 \
    --join=203.0.113.10:26257,203.0.113.11:26257,203.0.113.12:26257 \
    --cache=25% \
    --max-sql-memory=25% \
    --background
TimeoutStopSec=60
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=cockroach

Security hardening

NoNewPrivileges=yes PrivateTmp=yes ProtectHome=yes ProtectSystem=strict ReadWritePaths=/var/lib/cockroach SystemCallFilter=@system-service SystemCallErrorNumber=EPERM [Install] WantedBy=multi-user.target

Configure firewall rules for CockroachDB

Open required ports for secure cluster communication and web UI access.

sudo ufw allow from 203.0.113.0/24 to any port 26257 proto tcp comment 'CockroachDB cluster'
sudo ufw allow from 203.0.113.0/24 to any port 8080 proto tcp comment 'CockroachDB web UI'
sudo ufw reload
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port protocol="tcp" port="26257" accept'
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port protocol="tcp" port="8080" accept'
sudo firewall-cmd --reload

Start CockroachDB service and initialize cluster

Enable the service and initialize the secure cluster with authentication enabled.

sudo systemctl daemon-reload
sudo systemctl enable cockroach
sudo systemctl start cockroach

Initialize cluster (run on first node only)

sudo -u cockroach cockroach init \ --certs-dir=/var/lib/cockroach/certs \ --host=localhost:26257

Create application database users with certificates

Set up dedicated application users with certificate-based authentication and role-based access.

# Create application user certificate
sudo -u cockroach cockroach cert create-client \
    app_user \
    --certs-dir=/var/lib/cockroach/certs \
    --ca-key=/var/lib/cockroach/certs/ca.key \
    --key-size=4096 \
    --lifetime=8760h

Connect as root and create users

sudo -u cockroach cockroach sql \ --certs-dir=/var/lib/cockroach/certs \ --host=localhost:26257 \ --execute="CREATE USER IF NOT EXISTS app_user;"

Create read-only user

sudo -u cockroach cockroach cert create-client \ readonly_user \ --certs-dir=/var/lib/cockroach/certs \ --ca-key=/var/lib/cockroach/certs/ca.key \ --key-size=4096 \ --lifetime=8760h sudo -u cockroach cockroach sql \ --certs-dir=/var/lib/cockroach/certs \ --host=localhost:26257 \ --execute="CREATE USER IF NOT EXISTS readonly_user;"

Configure role-based access control (RBAC)

Set up granular permissions and roles for different application users and administrators.

sudo -u cockroach cockroach sql \
    --certs-dir=/var/lib/cockroach/certs \
    --host=localhost:26257 \
    --execute="
CREATE ROLE IF NOT EXISTS app_role;
CREATE ROLE IF NOT EXISTS readonly_role;

-- Grant permissions to roles
GRANT CREATE, SELECT, INSERT, UPDATE, DELETE ON DATABASE defaultdb TO app_role;
GRANT SELECT ON DATABASE defaultdb TO readonly_role;

-- Assign roles to users
GRANT app_role TO app_user;
GRANT readonly_role TO readonly_user;

-- Create application database
CREATE DATABASE IF NOT EXISTS myapp;
GRANT ALL ON DATABASE myapp TO app_role;
GRANT SELECT ON DATABASE myapp TO readonly_role;

-- Set session timeout for security
SET CLUSTER SETTING server.auth_log.sql_sessions.enabled = true;
SET CLUSTER SETTING server.auth_log.sql_connections.enabled = true;
SET CLUSTER SETTING sql.conn.max_lifetime = '1h';
"

Configure cluster security settings

Enable security features including audit logging, connection limits, and encryption at rest.

sudo -u cockroach cockroach sql \
    --certs-dir=/var/lib/cockroach/certs \
    --host=localhost:26257 \
    --execute="
-- Enable audit logging
SET CLUSTER SETTING sql.log.admin_audit.enabled = true;
SET CLUSTER SETTING sql.log.user_audit.enabled = true;

-- Set connection limits
SET CLUSTER SETTING server.max_connections_per_gateway = 1000;
SET CLUSTER SETTING sql.conn.max_lifetime = '2h';

-- Enable enterprise features trial
SET CLUSTER SETTING cluster.organization = 'Example Organization';
SET CLUSTER SETTING enterprise.license = '';

-- Configure backup settings
SET CLUSTER SETTING backup.table_statistics.enabled = true;

-- Set secure defaults
SET CLUSTER SETTING sql.defaults.password_encryption = 'scram-sha-256';
SET CLUSTER SETTING server.host_based_authentication.configuration = 'host all all all cert-password';
"

Configure web UI access with SSL

Set up secure web UI access with certificate authentication and admin user creation.

# Create web UI admin user with password
sudo -u cockroach cockroach sql \
    --certs-dir=/var/lib/cockroach/certs \
    --host=localhost:26257 \
    --execute="CREATE USER admin_user WITH PASSWORD 'StrongPassword123!';"

sudo -u cockroach cockroach sql \
    --certs-dir=/var/lib/cockroach/certs \
    --host=localhost:26257 \
    --execute="GRANT admin TO admin_user;"

Set web UI session timeout

sudo -u cockroach cockroach sql \ --certs-dir=/var/lib/cockroach/certs \ --host=localhost:26257 \ --execute="SET CLUSTER SETTING server.web_session_timeout = '1h';"

Configure log rotation and monitoring

Set up log rotation for security audit logs and configure monitoring endpoints.

/var/lib/cockroach/logs/*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    create 644 cockroach cockroach
    postrotate
        /bin/systemctl reload cockroach
    endscript
}
# Test log rotation
sudo logrotate -f /etc/logrotate.d/cockroach

Configure systemd journal retention

sudo mkdir -p /etc/systemd/journald.conf.d echo -e "[Journal]\nMaxRetentionSec=30day\nMaxFileSec=100M" | sudo tee /etc/systemd/journald.conf.d/cockroach.conf sudo systemctl restart systemd-journald

Set up backup encryption and automation

Configure encrypted backups with proper access controls and automated scheduling.

sudo mkdir -p /var/backups/cockroach
sudo chown cockroach:cockroach /var/backups/cockroach
sudo chmod 700 /var/backups/cockroach

Create backup script with encryption

sudo tee /usr/local/bin/cockroach-backup.sh > /dev/null << 'EOF' #!/bin/bash set -euo pipefail BACKUP_DIR="/var/backups/cockroach" DATE=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="${BACKUP_DIR}/backup_${DATE}.sql" ENCRYPTED_FILE="${BACKUP_FILE}.gpg"

Generate GPG key for backups if not exists

if ! sudo -u cockroach gpg --list-secret-keys | grep -q "cockroach-backup"; then sudo -u cockroach gpg --batch --full-generate-key << GPG_EOF Key-Type: RSA Key-Length: 4096 Subkey-Type: RSA Subkey-Length: 4096 Name-Real: CockroachDB Backup Name-Email: cockroach-backup@example.com Expire-Date: 0 %no-protection %commit GPG_EOF fi

Create backup

sudo -u cockroach cockroach dump \ --certs-dir=/var/lib/cockroach/certs \ --host=localhost:26257 \ defaultdb > "$BACKUP_FILE"

Encrypt backup

sudo -u cockroach gpg --trust-model always --encrypt \ --recipient "cockroach-backup@example.com" \ --output "$ENCRYPTED_FILE" \ "$BACKUP_FILE"

Remove unencrypted backup

rm "$BACKUP_FILE"

Clean old backups (keep 30 days)

find "$BACKUP_DIR" -name "backup_*.sql.gpg" -mtime +30 -delete echo "Backup completed: $ENCRYPTED_FILE" EOF sudo chmod +x /usr/local/bin/cockroach-backup.sh sudo chown cockroach:cockroach /usr/local/bin/cockroach-backup.sh

Create systemd timer for automated backups

Set up automated daily backups using systemd timers with proper logging.

[Unit]
Description=CockroachDB Backup Service
Wants=network-online.target
After=network-online.target cockroach.service

[Service]
Type=oneshot
User=root
ExecStart=/usr/local/bin/cockroach-backup.sh
StandardOutput=journal
StandardError=journal
[Unit]
Description=Run CockroachDB backup daily
Requires=cockroach-backup.service

[Timer]
OnCalendar=daily
RandomizedDelaySec=1h
Persistent=true

[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable cockroach-backup.timer
sudo systemctl start cockroach-backup.timer
sudo systemctl status cockroach-backup.timer

Verify your setup

Test SSL connections, certificate authentication, and security configurations.

# Check cluster status
sudo -u cockroach cockroach node status \
    --certs-dir=/var/lib/cockroach/certs \
    --host=localhost:26257

Test SSL connection

echo "SELECT version();" | sudo -u cockroach cockroach sql \ --certs-dir=/var/lib/cockroach/certs \ --host=localhost:26257

Verify certificate-based authentication

echo "SELECT current_user();" | sudo -u cockroach cockroach sql \ --certs-dir=/var/lib/cockroach/certs \ --user=app_user \ --host=localhost:26257

Check audit logging

sudo tail -f /var/lib/cockroach/logs/cockroach.log | grep -i audit

Test web UI access (should show SSL certificate)

curl -k --cert /var/lib/cockroach/certs/client.root.crt \ --key /var/lib/cockroach/certs/client.root.key \ https://localhost:8080/

Verify backup timer

sudo systemctl list-timers cockroach-backup.timer

Configure cluster monitoring

For production environments, integrate with monitoring systems to track security events and cluster health.

# Enable Prometheus metrics endpoint
sudo -u cockroach cockroach sql \
    --certs-dir=/var/lib/cockroach/certs \
    --host=localhost:26257 \
    --execute="SET CLUSTER SETTING server.remote_debugging.mode = 'any';"

Configure metrics collection (requires Prometheus)

curl -k --cert /var/lib/cockroach/certs/client.root.crt \ --key /var/lib/cockroach/certs/client.root.key \ https://localhost:8080/_status/vars

Check security audit logs

sudo journalctl -u cockroach -f | grep -E '(authentication|authorization|audit)'

Consider integrating with CockroachDB backup and disaster recovery automation for comprehensive data protection.

Common issues

SymptomCauseFix
Connection refused on port 26257 Firewall blocking cluster communication Configure firewall rules for cluster subnet: sudo ufw allow from cluster_subnet to any port 26257
Certificate verification failed Hostname mismatch in node certificate Recreate node certificate with correct hostnames and IP addresses
Permission denied accessing certificate files Incorrect file ownership or permissions sudo chown cockroach:cockroach /var/lib/cockroach/certs/ and sudo chmod 600 /var/lib/cockroach/certs/.key
Web UI shows certificate error Browser doesn't trust self-signed CA Import CA certificate to browser or use --insecure flag for testing only
Backup service fails Missing GPG key or incorrect permissions Check backup script permissions: sudo systemctl status cockroach-backup.service
High memory usage Default cache settings too aggressive Adjust --cache and --max-sql-memory in systemd service file
Security warning: Never disable SSL in production environments. Always use certificate-based authentication and regularly rotate certificates. Audit logs should be monitored for unauthorized access attempts.

Next steps

Running this in production?

Need this managed? Running distributed databases at scale adds complexity with certificate rotation, monitoring, backup validation, and incident response. Our managed platform covers monitoring, backups and 24/7 response by default.

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle infrastructure security hardening for businesses that depend on uptime. From initial setup to ongoing operations.