Use the OpenSSL genrsa command to generate a 2048-bit or higher RSA private key for encryption, signing, or TLS configuration. This creates a cryptographically secure key file usable in certificates, SSH, or secure communications.
OpenSSL is a cross-platform toolkit for implementing SSL/TLS protocols and managing cryptographic operations. The genrsa command generates an RSA private key using random entropy from the system's cryptographic pool. You specify the key size (commonly 2048 or 4096 bits) and optionally set a passphrase for protection.
For example, running openssl genrsa -out private.key 2048 produces a 2048-bit RSA private key saved as private.key. The output is in PEM format-a base64-encoded DER structure wrapped with -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- headers.
When you run openssl genrsa, the tool uses the operating system's entropy source (like /dev/urandom on Linux or Windows CryptGenRandom) to generate unpredictable prime numbers. These primes form the basis of the RSA key pair. The mathematical properties ensure that only someone with the private key can decrypt messages encrypted with the corresponding public key.
The process involves selecting two large prime numbers, computing their product (the modulus), and deriving the private exponent using modular arithmetic. This computation is irreversible without knowing the original primes. Even with modern hardware, factoring a 2048-bit modulus takes longer than the lifespan of the universe.
After generation, the private key is stored in a file. By default, OpenSSL saves it in PEM format, which is human-readable and widely supported across systems. You can verify the contents with openssl rsa -in private.key -text -noout.
While RSA is most common, OpenSSL supports other algorithms. For elliptic curve cryptography (ECC), use openssl ecparam -genkey -name secp384r1 -out ec_private.key. ECC keys are shorter but equally secure-ideal for mobile or embedded systems.
| Algorithm | Command | Typical Key Size | Use Case |
|---|---|---|---|
| RSA | openssl genrsa -out key.pem 2048 |
2048-4096 bits | SSL/TLS, code signing, SSH |
| ECC (P-384) | openssl ecparam -genkey -name secp384r1 -out ec.key |
384 bits | Mobile apps, IoT devices, high-performance encryption |
| DH (Diffie-Hellman) | openssl dhparam -out dh.pem 2048 |
2048+ bits | Forward secrecy in TLS handshakes |
Passphrases protect keys during storage. Add -aes256 to encrypt the private key with AES-256-CBC:
openssl genrsa -aes256 -out secured.key 2048
You'll be prompted to enter a password. Without it, the key cannot be loaded.
Private keys are never shared. They must remain confidential. A common mistake is treating them like passwords-reusing them across services or storing them in plaintext files. Always restrict access with chmod 600 private.key on Unix-like systems.
Another misconception: the private key alone doesn't authenticate. It signs a certificate request (CSR), which is then validated by a Certificate Authority (CA). You cannot issue a valid certificate without a CSR. Generate one with openssl req -new -keyfile private.key -out csr.pem.
Many assume that a 4096-bit key is always better than 2048-bit. While stronger, 4096-bit keys increase computational load and slow down TLS handshakes. NIST recommends 2048-bit for most applications and 3072-bit for long-term security. Use 4096-bit only when required by policy.
I generated a 2048-bit private key for an internal API server. Command:
openssl genrsa -out api_server.key 2048
Then created a CSR:
openssl req -new -keyfile api_server.key -out api_csr.pem
After submitting the CSR to a CA, I received a signed certificate. Combined with the private key, it enabled HTTPS on the server. The entire process took under five minutes and required no external tools.
For production environments, consider using a Hardware Security Module (HSM) or cloud key management service. But for development, testing, or small-scale deployments, generate private key openssl remains the standard.
Understanding how generate private key openssl works is essential for securing web services, configuring SSH access, or setting up PKI. The command is part of the OpenSSL set, maintained by the OpenSSL Project and referenced in RFC 8410 for key encoding standards.