Most Common OpenSSL Commands: A Tested Cheat Sheet for Keys, Certificates and TLS

Summarize this article with AI:

Table of Contents

The commands you will use most are genrsa for keys, req for requests and self-signed certificates, and x509 for reading certificates. Add s_client for testing servers and dgst for hashes. We ran every command below on OpenSSL 3.0.13 and note where older advice breaks.

Definition: OpenSSL is an open source toolkit and command-line program for keys, certificates, hashes and TLS connections.

What Are the Most Common OpenSSL Commands?

Most daily work needs about eight commands. This table maps each task to the command we tested. The sections after it show the full syntax, real output and the traps we found on a current OpenSSL release.

TaskCommand
Create a private keyopenssl genrsa -out private.key 2048
Create a certificate requestopenssl req -new -key private.key -out csr.pem
Create a self-signed certificateopenssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out certificate.pem -days 365
Read a certificateopenssl x509 -in certificate.pem -noout -text
Convert PEM to DERopenssl x509 -in certificate.pem -out certificate.der -outform DER
Test a live serveropenssl s_client -connect host:443 -servername host
Hash a fileopenssl dgst -sha256 file.txt
Encrypt a file with a passwordopenssl enc -aes-256-cbc -salt -pbkdf2 -in file.txt -out file.enc

How Do You Create a Private Key and a Certificate Request?

Run genrsa to make a 2048-bit key, then req with -new to build the request. Add -subj to skip the prompts. Keep the key private, because the CA only needs the request. Check the request with -verify before you send it to anyone.

openssl genrsa -out private.key 2048 openssl req -new -key private.key -out csr.pem -subj "/C=US/O=Example Lab/CN=www.example.test"
openssl req certificate request verified OK in terminal

Creating and checking a request on OpenSSL 3.0.13. Tested on 2 December 2026.

On OpenSSL 3, genrsa writes a PKCS#8 key with a BEGIN PRIVATE KEY header. Some software wants the older BEGIN RSA PRIVATE KEY header, and rsa -traditional makes that.

How Do You Create a Self-Signed Certificate?

One req command creates a key and a certificate together. Use -x509 for a certificate, -nodes to skip key encryption, and -days for validity. Add a subjectAltName, because browsers match the hostname against it. Self-signed certificates suit testing and internal use, not public sites.

openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out certificate.pem -days 365 -subj "/CN=www.example.test" -addext "subjectAltName=DNS:www.example.test"

This worked as written in our test. The old version of this page left out the subjectAltName option, so we added it.

How Do You Read a Certificate with OpenSSL?

Use x509 with -noout and pick the fields you need. -text prints everything. For a quick check, ask for -subject, -issuer and -dates. The -checkend option answers whether a certificate expires within a number of seconds, which suits scripts.

openssl x509 -in certificate.pem -noout -subject -issuer -dates openssl x509 -in certificate.pem -noout -checkend 86400
openssl x509 subject issuer dates and checkend output

Reading a test certificate. -checkend returns exit code 0 when it will not expire within the time given.

How Do You Convert Between PEM, DER and PKCS#7?

x509 converts a single certificate between PEM and DER with -outform and -inform. crl2pkcs7 packs certificates into a PKCS#7 file. We ran all three conversions and checked the headers. For PFX files, pkcs12 -export bundles a key and certificate.

  • PEM to DER: openssl x509 -in certificate.pem -out certificate.der -outform DER
  • DER to PEM: openssl x509 -in certificate.der -inform DER -out certificate.pem -outform PEM
  • PEM to P7B: openssl crl2pkcs7 -nocrl -certfile certificate.pem -out certificate.p7b
  • PEM to PFX: openssl pkcs12 -export -inkey key.pem -in certificate.pem -out bundle.p12

The DER file was recognised as a certificate, and the P7B file started with BEGIN PKCS7. Our PEM file guide explains the formats.

How Do You Test a Live Server with s_client?

Connect with s_client, give the hostname with -servername so the right certificate is chosen, and read the result. Add -brief for a short summary. We tested against a local server, where it reported TLS 1.3, a cipher suite and a verification result.

openssl s_client -connect www.example.com:443 -servername www.example.com -brief

Our self-signed test server printed verify error 18, which is correct because no trusted CA signed it. For missing intermediates, read what an intermediate certificate is.

How Do You Check That a Key Matches a Certificate?

Hash the public key from each file and compare the results. If the two hashes match, the key belongs to the certificate. A mismatch is why many servers refuse to start after an install. The same method works for a request file.

openssl pkey -in private.key -pubout -outform der | openssl sha256 openssl x509 -in certificate.pem -pubkey -noout | openssl pkey -pubin -pubout -outform der | openssl sha256

How Do You Hash and Encrypt Files?

dgst computes a hash, and SHA-256 is a good default. MD5 and SHA-1 still run but should not be used for security. For passwords-based encryption, enc works, but add -pbkdf2. Without it, OpenSSL prints a deprecated key derivation warning, as we saw.

openssl dgst -sha256 file.txt openssl enc -aes-256-cbc -salt -pbkdf2 -iter 600000 -in file.txt -out file.enc

The OpenSSL manual for the enc command lists the -pbkdf2 option. We encrypted and decrypted a test file with it and got the original text back.

Which Old OpenSSL Commands Should You Stop Using?

Stop using rsautl, which OpenSSL 3 marks as deprecated and replaces with pkeyutl. Stop using enc without -pbkdf2. Drop MD5 and SHA-1 for anything that matters. We ran each one, and the output named the replacement where one exists.

openssl deprecated command warnings in terminal

Warnings from two older commands on OpenSSL 3.0.13.

pkeyutl encrypted and decrypted our test file correctly. Keep in mind that raw RSA encryption only handles small amounts of data, so use it for keys, not files.

Where Does OpenSSL Store Keys and Certificates?

It depends on the system. On our Ubuntu test machine, /etc/ssl held certs, private and openssl.cnf, and there was no csr folder. OpenSSL does not store your own files for you. Keep keys where your server can read them, with tight permissions.

The old version of this page said CSRs go under /etc/ssl/csr. We did not find that directory, so we removed the claim.

Frequently Asked Questions

Is OpenSSL free?

Yes. It is open source software. Check the license of your version if you need the exact terms.

What is the difference between OpenSSL and LibreSSL?

LibreSSL is a separate fork maintained by the OpenBSD project. Some command options differ, so test on your platform.

Why does openssl enc show a deprecated key derivation warning?

It used the old password-to-key method. Add -pbkdf2 and, ideally, -iter with a high count.

Can I run OpenSSL commands on Windows?

Yes, if OpenSSL is installed. Commands are the same, but file paths and quoting differ in PowerShell.

Should I use a 2048-bit RSA key?

2048 bits is a common minimum for web certificates. Your CA may accept larger keys or ECDSA.