A practical, no-nonsense guide from beginner to advanced.
All commands assumeopensslis installed and in your$PATH.
.crt, .pem, .cer) = public key + identity info + signature.Common file formats:
.pem = Base64 text with -----BEGIN ... / -----END ....key = private key (usually PEM).crt / .cer = certificate (PEM or DER).pfx / .p12 = PKCS#12 bundle (cert + key + chain), usually password protectedopenssl version
openssl version -a # with build info
# 2048-bit RSA key (enough for most uses)
openssl genrsa -out server.key 2048
# 4096-bit (stronger but heavier)
openssl genrsa -out server-4096.key 4096
genpkey)openssl genpkey -algorithm RSA -out server.key -pkeyopt rsa_keygen_bits:2048
# List available curves
openssl ecparam -list_curves
# Generate EC key using prime256v1 (aka secp256r1)
openssl ecparam -name prime256v1 -genkey -noout -out server-ec.key
# Add password to private key (PEM)
openssl rsa -aes256 -in server.key -out server-protected.key
# Remove password (be careful!)
openssl rsa -in server-protected.key -out server-nopass.key
openssl req -new -key server.key -out server.csr
Youβll be asked for:
example.comopenssl.cnf example snippet:
[ req ]
default_bits = 2048
default_md = sha256
prompt = no
distinguished_name = dn
req_extensions = req_ext
[ dn ]
C = US
ST = Some-State
L = Some-City
O = My Company
OU = IT
CN = example.com
[ req_ext ]
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = example.com
DNS.2 = www.example.com
Generate CSR:
openssl req -new -key server.key -out server.csr -config openssl.cnf
openssl req -x509 -newkey rsa:2048 -keyout server.key -out server.crt \
-days 365 -nodes
-x509: make cert, not CSR-newkey rsa:2048: create key + cert in one go-nodes: do NOT encrypt the private key (no password)openssl x509 -req -in server.csr -signkey server.key -out server.crt -days 365
openssl x509 -in server.crt -noout -text
openssl req -in server.csr -noout -text
# RSA key
openssl rsa -in server.key -check -noout
# EC key
openssl ec -in server-ec.key -check -noout
# From private key
openssl pkey -in server.key -pubout -out server.pub
# From certificate
openssl x509 -in server.crt -pubkey -noout > server.pub
# PEM cert -> DER
openssl x509 -in server.crt -outform der -out server.der
# DER cert -> PEM
openssl x509 -in server.der -inform der -out server.pem
# Key + cert -> PFX
openssl pkcs12 -export -out server.pfx -inkey server.key -in server.crt
# Key + cert + chain -> PFX
openssl pkcs12 -export -out server.pfx \
-inkey server.key \
-in server.crt \
-certfile chain.crt
# Extract private key
openssl pkcs12 -in server.pfx -nocerts -out server.key
# Extract certificate
openssl pkcs12 -in server.pfx -clcerts -nokeys -out server.crt
# Extract everything
openssl pkcs12 -in server.pfx -nodes -out all.pem
openssl dgst -md5 file.txt
openssl dgst -sha1 file.txt
openssl dgst -sha256 file.txt
openssl dgst -sha512 file.txt
openssl dgst -sha256 -hmac "secretkey" file.txt
Note: built-in
encis ok for quick stuff, but for serious security use modern tools/libs.
# AES-256-CBC encryption
openssl enc -aes-256-cbc -salt -in plain.txt -out encrypted.bin
Youβll be asked for a password.
openssl enc -d -aes-256-cbc -in encrypted.bin -out decrypted.txt
# Encrypt and output Base64
openssl enc -aes-256-cbc -salt -in plain.txt -out encrypted.b64 -base64
# Decrypt from Base64
openssl enc -d -aes-256-cbc -in encrypted.b64 -out decrypted.txt -base64
# Create signature.sig using private key
openssl dgst -sha256 -sign private.key -out file.sig file.txt
# Verify with public key
openssl dgst -sha256 -verify public.pem -signature file.sig file.txt
(Exit code 0 = success, non-zero = failure.)
openssl x509 -in server.crt -noout -subject -issuer -dates
openssl verify -CAfile ca.crt server.crt
# ca-chain.crt contains intermediate + root
openssl verify -CAfile ca-chain.crt server.crt
openssl s_client -connect example.com:443
openssl s_client -connect example.com:443 -showcerts
openssl s_client -connect example.com:443 -servername example.com
openssl s_client -starttls smtp -connect mail.example.com:587
Other -starttls options: http, imap, pop3, ftp, etc.
(Used in some older configs; modern setups often use ECDHE instead.)
openssl dhparam -out dhparam.pem 2048
# 16 random bytes as hex
openssl rand -hex 16
# 32 random bytes, base64
openssl rand -base64 32
# SHA-512 based password hash (interactive)
openssl passwd -6
Common switches:
-1 = MD5 (old, donβt use)-5 = SHA-256-6 = SHA-512# Try as certificate
openssl x509 -in unknown-file -noout -text
# Try as CSR
openssl req -in unknown-file -noout -text
# Try as private key
openssl rsa -in unknown-file -check -noout # RSA
openssl ec -in unknown-file -check -noout # EC
# Try to see if it's a PKCS#12 bundle
openssl pkcs12 -in unknown-file -info
If one of those works β you know what you have π
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
| openssl x509 -noout -dates
openssl x509 -in server.crt -noout -subject
# then look for CN=
# Combine leaf + intermediate into one chain file
cat server.crt intermediate.crt > fullchain.crt
Protect private keys:
chmod 600 server.keyFor production certificates:
| Task | Command (short version) |
|---|---|
| Generate RSA key | openssl genrsa -out key.pem 2048 |
| Generate EC key | openssl ecparam -name prime256v1 -genkey -out key.pem |
| Create CSR | openssl req -new -key key.pem -out req.csr |
| Self-signed cert | openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes |
| View cert | openssl x509 -in cert.pem -noout -text |
| View CSR | openssl req -in req.csr -noout -text |
| Cert β DER | openssl x509 -in cert.pem -outform der -out cert.der |
| Make PFX | openssl pkcs12 -export -out cert.pfx -inkey key.pem -in cert.pem |
| Verify cert | openssl verify -CAfile ca.pem cert.pem |
| Hash file (SHA-256) | openssl dgst -sha256 file |
| Random 32 bytes (base64) | openssl rand -base64 32 |
| Connect to HTTPS | openssl s_client -connect example.com:443 |
Feel free to copy this file as OPENSSL-CHEATSHEET.md into your GitHub repo.
::contentReference[oaicite:0]{index=0}