Sub topic 16
Protecting private keys and account data
This chapter assumes you already have a working Let's Encrypt setup from the earlier chapters: Certbot installed, a certificate issued, nginx serving it, and renewal running. You should be comfortable with Unix file permissions, sudo, and systemd.
TLS security depends on one secret. Anyone who holds the private key for your certificate can impersonate your server until the certificate expires or is revoked. A second secret, the ACME account key, lets someone act as you toward Let's Encrypt. This chapter covers where both secrets live, who can read them, and what to do if they leak.
What needs protecting
Certbot stores several kinds of sensitive data under /etc/letsencrypt.
| Item | Location | Why it matters |
|---|---|---|
| Certificate private key | `archive/<name>/privkeyN.pem`, linked from `live/<name>/privkey.pem` | Lets the holder impersonate your site and decrypt traffic captured from sessions that don't use forward secrecy |
| ACME account key | `accounts/<server>/<id>/private_key.json` | Lets the holder request certificates and revoke yours under your account |
| Renewal configuration | `renewal/<name>.conf` | Shows how each certificate is renewed, including paths to DNS credential files |
| DNS plugin credentials | A path you choose, often `/etc/letsencrypt/cloudflare.ini` or similar | A DNS API token can edit your zone, which is more than a certificate needs |
The certificate itself (cert.pem, fullchain.pem) is public. Your server sends it to every visitor, so it needs no secrecy.
Default permissions and how to check them
Certbot creates /etc/letsencrypt/live and /etc/letsencrypt/archive as root:root with mode 0700, and it writes private keys with mode 0600. These defaults are correct. The most common mistake is loosening them to fix a permission error in another program.
Audit the current state:
sudo find /etc/letsencrypt -name 'privkey*.pem' \
-exec stat -c '%U:%G %a %n' {} +
sudo stat -c '%U:%G %a %n' \
/etc/letsencrypt/live /etc/letsencrypt/archive /etc/letsencrypt/accountsEvery key line should read root:root 600, and the three directories should read root:root 700. Anything wider deserves an explanation.
nginx does not need group or world access to the key. The nginx master process starts as root, reads the certificate and key during startup or reload, and then hands connections to worker processes running as www-data or nginx. The workers never open the key file. If nginx fails to read the key, the usual cause is a wrong path or a broken symlink, not a permission problem. Do not run chmod -R 755 /etc/letsencrypt.
Giving another service access safely
Sometimes a second daemon, such as a mail server or a message broker, needs the same key. Do not open up /etc/letsencrypt. Copy the key to a location with a narrow group, and refresh the copy from a deploy hook so it follows renewals.
The hook below assumes Debian or Ubuntu, where the ssl-cert group and /etc/ssl/private already exist. Save it as /etc/letsencrypt/renewal-hooks/deploy/copy-key.sh and make it executable.
#!/bin/sh
# Certbot sets RENEWED_LINEAGE to the live/<name> path of the renewed certificate.
set -e
install -m 0640 -o root -g ssl-cert \
"$RENEWED_LINEAGE/privkey.pem" /etc/ssl/private/example.com.key
install -m 0644 -o root -g root \
"$RENEWED_LINEAGE/fullchain.pem" /etc/ssl/certs/example.com.pem
systemctl reload postfixAdd only the other service's user to the ssl-cert group. Each additional copy of the key is another place it can leak, so keep the number of copies small and know where they are.
Key lifetime and key reuse
By default, Certbot generates a new private key at every renewal. Since renewal happens roughly every 60 days, a key that leaks without your noticing is useful to an attacker for a limited time.
The option --reuse-key keeps the same key across renewals. People use it when they pin the public key somewhere, such as in a DANE/TLSA DNS record. Using it makes the exposure window as long as the life of the key, so treat it as a deliberate trade-off. To enable it for an existing certificate:
sudo certbot renew --cert-name example.com --reuse-key --force-renewalCertbot saves the setting in renewal/example.com.conf, so later renewals keep the key. Without a pinning requirement, leave the default.
Recent Certbot versions generate ECDSA keys by default. You can choose the type explicitly with --key-type ecdsa or --key-type rsa. An ECDSA P-256 key is small and fast. RSA 2048 remains available for old clients that do not support ECDSA.
Backups without leaking secrets
You may want a backup of /etc/letsencrypt so you keep your account and renewal settings if the server is rebuilt. A backup holds the same secrets as the live directory, so it must be protected to the same standard. Preserve the permissions and symlinks with tar -p, and encrypt the archive before it leaves the machine:
(umask 077; sudo tar -czpf - -C /etc letsencrypt \
| gpg --symmetric --cipher-algo AES256 \
--output letsencrypt-backup.tar.gz.gpg)Keep the passphrase in a password manager, not next to the archive. Two rules apply to repositories and shared storage:
- Never commit
/etc/letsencryptto Git, including private repositories. Repositories get cloned, mirrored and shared. - Never put key material in container images or in configuration management files stored in plain text.
Backups are optional, because you can always issue a new certificate. If you value simplicity, skip them and reissue after a rebuild. Remember the rate limits from the earlier chapter if you rebuild often.
The account key and its credentials
The account key identifies you to Let's Encrypt. If it leaks, an attacker can revoke your certificates or request new ones for any name where they can pass validation. They cannot get a certificate for a domain they don't control merely by holding your account key, because every issuance still requires a validation challenge. Still, treat it as a secret and keep it root-only.
To inspect or retire an account:
sudo certbot show_account
sudo certbot unregisterunregister deactivates the account on the server. Existing certificates keep working until they expire, but they can no longer be renewed under that account. Run sudo certbot register or request a new certificate to create a fresh account.
DNS plugin credentials need extra care, because a DNS token can change records for your whole zone. Create the file with mode 0600, owned by root. Certbot warns if a credentials file is readable by others. Where your DNS provider supports it, scope the token to the single zone and to TXT record edits only.
Responding to a suspected key compromise
If you suspect the private key has been exposed, do not wait for expiry. Follow these steps in order:
- Revoke the certificate with the reason
keycompromise. - Delete the lineage so the old key cannot be reused.
- Issue a new certificate, which gets a fresh key.
- Reload services and remove stale copies of the old key.
- Find how the key leaked and fix that cause.
sudo certbot revoke --cert-name example.com \
--reason keycompromise --delete-after-revoke
sudo certbot certonly --nginx -d example.com -d www.example.com
sudo nginx -t && sudo systemctl reload nginxRevocation tells the CA to stop vouching for the certificate. Browsers differ in how well they check revocation, so replacing the key is the part that actually restores your security. Also check every copy you made, such as hook-created files and backups.
Common mistakes
- Running
chmod 644onprivkey.pemto satisfy a non-root process. Use a group-limited copy instead. - Pointing nginx at
archive/files instead oflive/symlinks. The path then goes stale after the first renewal. - Leaving DNS credential files with default permissions.
- Forgetting copies of the key in old backups, home directories, or
/tmp. - Reusing one key across several servers. If one server is breached, all are exposed. Issue a separate certificate per server where you can.
Exercise
A colleague says nginx cannot read /etc/letsencrypt/live/example.com/privkey.pem and proposes chmod -R o+rX /etc/letsencrypt. What should you do instead, and why is the proposal harmful?
Show answer
First confirm the real cause, since the nginx master process runs as root and normally reads the key without any change to permissions. Check the path in ssl_certificate_key, verify that the symlinks in live/ are intact, and run sudo nginx -t. The proposed command makes every private key, the account key, and any DNS credential files readable by all local users, so any compromised service could steal them. If another daemon truly needs the key, copy it from a deploy hook to a path owned by a restricted group, as shown in this chapter.
Exercise
You find privkey.pem in a Git repository your team uses. List the actions you take, in order.
Show answer
Treat the key as compromised. Revoke the certificate with sudo certbot revoke --cert-name <name> --reason keycompromise --delete-after-revoke, then issue a new certificate so a new key is generated, and reload nginx. Remove the file from the repository history and rotate any other secrets committed beside it. Check who has cloned the repository, and add a rule such as .gitignore plus a pre-commit scan to stop it happening again.