
Are you looking to secure your Cockpit server and protect sensitive data? Follow these steps to Install SSL Certificates on Cockpit and protect your server
Learn how to automate SSL/TLS certificate renewal and deployment using OpenSSL and Bash scripts. Includes CLI examples, best practices, cron automation, and deployment tips.
Securing communication channels using SSL/TLS certificates is critical in today’s cybersecurity landscape. However, managing and renewing these certificates manually is time-consuming and error-prone—especially in environments with dozens or hundreds of hosts. Automating this process using OpenSSL and Bash scripting not only streamlines operations but also enhances security by minimizing certificate expiration risks.
In this guide, we’ll show you how to automate certificate renewal and deployment using OpenSSL and Bash. We’ll include CLI examples, practical scripting tips, and best practices for managing your certificates on Linux-based systems.
Benefit | Description |
---|---|
🔐 Security | Prevents service disruption and data breaches due to expired certificates. |
⚙️ Efficiency | Reduces manual labor and human error through scripting. |
⏰ Timeliness | Ensures timely renewals with scheduled tasks (e.g., cron jobs). |
📦 Scalability | Supports deployment across multiple servers seamlessly. |
To automate certificate renewal and deployment using OpenSSL and Bash, ensure the following tools are installed. Below are commands for both Debian-based (Ubuntu, Debian) and RPM-based (RHEL, CentOS, AlmaLinux, Rocky Linux) distributions.
Tool | Purpose | Debian/Ubuntu | RHEL/CentOS |
---|---|---|---|
openssl | Generate, manage, and validate certificates | sudo apt install openssl | sudo dnf install openssl |
bash | Shell scripting engine | Pre-installed | Pre-installed |
cron | Task scheduler for automation | sudo apt install cron | sudo dnf install cronie |
scp / rsync | Securely transfer certs to remote hosts | sudo apt install openssh-client rsync | sudo dnf install openssh-clients rsync |
systemctl | Restart services post-deployment (e.g., nginx, apache) | Pre-installed (on systemd distros) | Pre-installed (on systemd distros) |
Photo by admingeek from Infotechys
Before renewal, you’ll need a CSR (if using manual or internal CA renewal).
#!/bin/bash
DOMAIN="example.com"
KEY_FILE="$DOMAIN.key"
CSR_FILE="$DOMAIN.csr"
# Generate private key
openssl genrsa -out $KEY_FILE 2048
# Generate CSR
openssl req -new -key $KEY_FILE -out $CSR_FILE -subj "/C=US/ST=NY/L=NYC/O=ExampleOrg/OU=IT/CN=$DOMAIN"
echo "CSR and key generated for $DOMAIN"
✅ Tip: Save this as generate_csr.sh
and make it executable:
chmod +x generate_csr.sh
Let’s say your CA provides renewed certificates via API or email. Once received (e.g., example.com.crt
), automate the replacement.
#!/bin/bash
DOMAIN="example.com"
CERT_DIR="/etc/ssl/certs"
KEY_DIR="/etc/ssl/private"
DEPLOY_DIR="/var/www/certs"
cp $CERT_DIR/$DOMAIN.crt $DEPLOY_DIR/
cp $KEY_DIR/$DOMAIN.key $DEPLOY_DIR/
# Set permissions
chmod 600 $DEPLOY_DIR/$DOMAIN.key
chmod 644 $DEPLOY_DIR/$DOMAIN.crt
echo "Certificate and key deployed to $DEPLOY_DIR"
For environments with multiple servers (e.g., NGINX load balancers), use scp
or rsync
:
#!/bin/bash
DOMAIN="example.com"
REMOTE_HOSTS=("web1.example.com" "web2.example.com")
REMOTE_DIR="/etc/ssl/certs"
for HOST in "${REMOTE_HOSTS[@]}"; do
echo "Deploying certificate to $HOST"
scp /var/www/certs/$DOMAIN.crt user@$HOST:$REMOTE_DIR/
scp /var/www/certs/$DOMAIN.key user@$HOST:/etc/ssl/private/
done
Best practice: Ensure passwordless SSH (ssh-keygen
+ ssh-copy-id
) for seamless automation.
You can now automate the whole process using a cron job.
# Edit crontab
crontab -e
Add the following entry to run renewal every month:
0 0 1 * * /opt/scripts/renew_and_deploy.sh >> /var/log/cert_renewal.log 2>&1
It’s vital to verify the deployed cert to ensure no service interruption:
Check Certificate Expiry: |
openssl x509 -in /etc/ssl/certs/example.com.crt -noout -enddate
Verify Certificate Chain: |
openssl verify -CAfile ca_bundle.crt example.com.crt
Programmatically Check (via Bash): |
openssl x509 -enddate -noout -in /etc/ssl/certs/example.com.crt | cut -d= -f2
Add conditional logic:
EXPIRY_DATE=$(openssl x509 -enddate -noout -in /etc/ssl/certs/example.com.crt | cut -d= -f2)
EXPIRY_EPOCH=$(date -d "$EXPIRY_DATE" +%s)
NOW_EPOCH=$(date +%s)
DAYS_LEFT=$(( ($EXPIRY_EPOCH - $NOW_EPOCH) / 86400 ))
if [ "$DAYS_LEFT" -lt 15 ]; then
echo "⚠️ Certificate will expire in $DAYS_LEFT days"
fi
Practice | Why It Matters |
---|---|
🔁 Backups | Always back up current certs before overwriting. |
🔐 File Permissions | Ensure only root or web server has read access to private keys. |
📅 Set Expiry Reminders | Especially useful for manual CA environments. |
📂 Use Consistent Directory Structures | Helps scripts remain portable. |
🛡️ Monitor SSL Status | Use tools like Zabbix, Nagios, or custom scripts to watch expiry. |
For Let’s Encrypt users, automation becomes even simpler via certbot
. However, OpenSSL and Bash can still be used for deployment post-renewal.
#!/bin/bash
DOMAIN="example.com"
LE_DIR="/etc/letsencrypt/live/$DOMAIN"
DEPLOY_DIR="/var/www/certs"
cp $LE_DIR/fullchain.pem $DEPLOY_DIR/$DOMAIN.crt
cp $LE_DIR/privkey.pem $DEPLOY_DIR/$DOMAIN.key
systemctl reload nginx
echo "Let’s Encrypt certificate deployed and web server reloaded"
To automate with certbot
:
0 3 * * 1 certbot renew --quiet --deploy-hook "/opt/scripts/deploy.sh"
Feature | Manual Approach | Automated Approach |
---|---|---|
Renewal | Manual via GUI/API | Scheduled with Bash |
Deployment | Copy/paste per host | scp , rsync , or Ansible |
Verification | Openssl commands | Scripted checks |
Downtime Risk | High | Low |
Scalability | Limited | High |
Issue | Cause | Solution |
---|---|---|
❌ Permissions error | Incorrect file permissions | Ensure certs are readable by the server, private keys are secure |
❌ Expired cert | Missed renewal | Add log alerts or expiry checks in script |
❌ SSL error in browser | Wrong certificate path | Double-check your web server config |
❌ SCP timeout | Network/firewall issues | Validate SSH access and credentials |
Automating certificate renewal and deployment with OpenSSL and Bash is an effective way to protect your infrastructure, reduce operational overhead, and ensure uninterrupted services. Whether you’re managing a single server or an entire cluster, these automation techniques help you stay secure and compliant.
Remember: automation doesn’t mean “set and forget.” Regularly test, audit, and improve your scripts to keep up with evolving security standards and operational needs.
Did you find this article helpful? Your feedback is invaluable to us! Feel free to share this post with those who may benefit, and let us know your thoughts in the comments section below.
Are you looking to secure your Cockpit server and protect sensitive data? Follow these steps to Install SSL Certificates on Cockpit and protect your server
Want to secure your Jenkins environment and protect sensitive data? Learn how to install SSL certificates on Jenkins and enhance the security of your continuous
Learn how to archive and extract PFX/PKCS#12 certificate files using OpenSSL and Windows tools. Includes CLI examples, tables, and best practices for secure handling. Table