Sub topic 16
Protecting Private Keys and Account Data
Assume you are comfortable with Linux permissions, sudo, nginx, and Certbot renewal. This chapter focuses on limiting access to the files that can impersonate your domain or control your certificate account.
Identify the sensitive files
A TLS private key proves that your server owns a certificate. Anyone who obtains the key can impersonate the domain until the certificate expires or you revoke it. The key is normally stored through a Certbot path such as:
/etc/letsencrypt/live/example.com/privkey.pem: a symbolic link to the current private key./etc/letsencrypt/archive/example.com/: versioned certificates and keys./etc/letsencrypt/renewal/example.com.conf: renewal settings. DNS plugin credentials may also be referenced here./etc/letsencrypt/accounts/: ACME account keys and registration data.
The live files are links, not independent copies. Protect both the links and their targets.
Check ownership and permissions
Run an audit as root:
sudo namei -l /etc/letsencrypt/live/example.com/privkey.pem
sudo find /etc/letsencrypt -maxdepth 3 -type f -printf '%M %u:%g %p\n' \
| sortA private key should normally be owned by root and readable only by its owner:
sudo chown root:root /etc/letsencrypt/archive/example.com/privkey*.pem
sudo chmod 600 /etc/letsencrypt/archive/example.com/privkey*.pemKeep the parent directories inaccessible to ordinary users where practical. Certbot normally manages these permissions, so inspect the result after upgrades or manual changes.
nginx usually starts with a privileged master process. The master opens the private key, then worker processes run with fewer privileges. If your setup uses a different service account or container, grant that identity access explicitly and document the reason. Avoid adding broad users to the root group.
| Data | Typical protection | Reason |
|---|---|---|
| `privkey.pem` | `root:root`, mode `600` | Can impersonate the domain |
| `fullchain.pem` | Readable by the web server | Public certificate chain |
| Renewal configuration | Root-only where possible | May contain paths or credentials |
| DNS API credential | Root-only, minimum provider scope | Can alter domain validation records |
Protect account credentials and backups
DNS-01 automation often uses an API token. Create a token limited to the required zone and permission to change only DNS records. Store it in a root-readable file, not directly in a command or source repository.
Back up private keys and ACME account data only when necessary. Encrypt backups, restrict their storage, and test that restoration preserves permissions. Do not paste private keys into tickets, chat, monitoring output, or shell history.
The following check finds private-key files that are readable by group or other users:
sudo find /etc/letsencrypt -type f -name 'privkey*.pem' \
\( -perm /0077 -o ! -user root \) -lsIf a key may have been exposed, treat it as compromised. Revoke the certificate, issue a replacement, and remove the exposed copy from logs and backups where possible.
Exercise
Why is changing privkey.pem to mode 644 an unsafe fix for an nginx permission error?
Show answer
It allows every local user and potentially other processes to read the private key. The safer fix is to identify the process that needs access and grant only that identity the minimum permission.