brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: openssl pkey adds or removes the passphrase on a private key

Openssl
~/tips/tip-openssl-pkey-passphrase

openssl pkey -in site.key -aes256 -out site-enc.key

Encrypt a private key file with a passphrase, or take it off for a service that cannot ask.

A private key on a laptop or in a backup should have a passphrase. A key that nginx loads at boot usually cannot have one. openssl pkey switches between the two.

Add a passphrase; OpenSSL prompts for it twice:

openssl pkey -in site.key -aes256 -out site-enc.key

The new file starts with BEGIN ENCRYPTED PRIVATE KEY. To take the passphrase off again, read the encrypted key without a cipher option:

openssl pkey -in site-enc.key -out site.key
chmod 600 site.key

This works the same for RSA, EC and Ed25519 keys, which is why I prefer pkey over the older rsa command for this job.

My advice is to keep the encrypted copy in backups and only put the plain key on the server that needs it. A plain key file is only protected by file permissions, so check those after every copy.

Avoid putting the passphrase on the command line with -passout pass:.... It ends up in shell history and is visible in the process list while the command runs. If a script really needs it, -passin file:path or env:VAR are better. And keep the passphrase somewhere safe: without it the encrypted key is useless, and nobody can recover it for you.

Further reading: OpenSSL: openssl-pkey, OpenSSL: passphrase options

More Openssl tips openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null | openssl x509 -noout -subject openssl x509 -in cert.pem -days 365 -signkey key.pem -out cert-new.pem printf %s hello | openssl dgst -sha256 -hmac secret openssl pkcs12 -legacy -in old.pfx -nokeys echo 'aGVsbG8gd29ybGQ=' | openssl base64 -d All Openssl tips (34) All topics

Link