Generate a private key
To generate a 2048-bit RSA private key, run `openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private.key`. For an elliptic curve key, which is smaller and faster at equivalent strength, run `openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out private.key`. Prefer `genpkey` over the older `genrsa`: it handles every algorithm through one interface and writes PKCS#8 output, which Java, Go and most modern libraries require.
RSA #
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private.key Replace these values
| Placeholder | What to put there |
|---|---|
-algorithm RSA | The key type. Also accepts EC, ED25519, X25519. |
-pkeyopt rsa_keygen_bits:2048 | Key size. 2048 is the CA/Browser Forum minimum. |
private.key | Output file. Starts -----BEGIN PRIVATE KEY-----. |
Elliptic curve #
Smaller keys, smaller certificates, faster handshakes, and equivalent security. P-256 is universally supported:
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out private.key openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-384 -out private.key Ed25519 #
Modern, fast and fixed-size, but not accepted by public CAs for TLS server certificates. Useful for internal PKI and for signing:
openssl genpkey -algorithm ED25519 -out private.key Choosing #
| Algorithm | Key size | Use when |
|---|---|---|
| RSA 2048 | 2048 bits | You want the option that works everywhere without thought |
| RSA 4096 | 4096 bits | A policy requires it. Otherwise it mostly buys signing cost |
| EC P-256 | 256 bits | Default choice for new TLS work — smaller and faster |
| EC P-384 | 384 bits | A larger margin is required by policy |
| Ed25519 | 256 bits | Internal PKI or signing. Not for public TLS certificates |
Adding a passphrase #
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -aes-256-cbc -out private.key Lock the file down #
chmod 600 private.key Do this immediately, not later. A key created with a default umask may be world-readable for the window between creation and whenever you remember.
Check what you generated #
openssl pkey -in private.key -noout -text | head -n 3 Expected output
Private-Key: (2048 bit, 2 primes) modulus: 00:c4:1f:...
The older subcommands #
Still functional, still widely seen in documentation:
openssl genrsa -out private.key 2048 Next steps #
- Generate a CSR to get this key certified
- Generate a self-signed certificate for local use
- Extract the public key
Frequently asked questions
- Should I use genpkey or genrsa?
genpkey. It covers every algorithm through one interface and writes PKCS#8, the format modern libraries expect.genrsastill works and still writes PKCS#1 by default, which then needs converting for Java or Go.- RSA 2048 or 4096?
- 2048 for almost everything. It is the CA/Browser Forum minimum and is not considered at risk. 4096 roughly quadruples signing cost for a security margin nobody currently needs, which matters on a busy server. Choose EC P-256 instead if you want a real improvement.
- Is an EC key safe to use with a public CA?
- Yes. P-256 and P-384 are supported by every public CA and every browser and TLS library in current use. The certificate will be smaller and handshakes cheaper.
- Should I put a passphrase on the key?
- Not for a web server, which cannot answer a prompt at boot. Protect it with file permissions instead. A passphrase makes sense for a CA key or any key that is handled by a person rather than a service.
References
- openssl-genpkey(1) — the current key generation command and its -pkeyopt options
- RFC 5958: asymmetric key packages — PKCS#8, the self-describing private key container