OpenSSL commands

OpenSSL command lines from the project's own manual pages for the 3.6 series (current release 3.6.5), grouped by task: keys, requests and certificates, PKCS#12, verification, TLS testing, hashing, encryption and random data. File names such as cert.pem come from the manual's examples; words in capitals are placeholders. Rows that start with a dash are options to add to the s_client line above them. S/MIME, CMS, OCSP and the ca database command are left out.

103 commands in 12 sections. Checked row by row against openssl-genpkey(1) manual page, OpenSSL 3.6 documentation (private key generation) on . Edited by Jay.

Private keys 15

Password options such as -pass, -passin and -passout also accept env:VAR, file:PATHNAME and stdin; the pass: form shows the password to other users in ps.

CommandWhat it does
openssl genpkey -algorithm RSA -out key.pemGenerate an RSA keyThe key is 2048 bits unless an rsa_keygen_bits option says otherwise; without -out it goes to standard output.
openssl genpkey -algorithm RSA -out key.pem -pkeyopt rsa_keygen_bits:2048Set the RSA key sizeEach -pkeyopt passes one algorithm setting and must come after -algorithm; the manual example also sets the public exponent.
openssl genpkey -algorithm RSA -out key.pem -aes-128-cbc -pass pass:helloGenerate a passphrase-protected keyAny cipher name accepted by OpenSSL can replace -aes-128-cbc; -pass names where the passphrase comes from.
openssl genpkey -algorithm EC -out eckey.pem -pkeyopt ec_paramgen_curve:P-384 -pkeyopt ec_param_enc:named_curveGenerate an EC keyNIST names such as P-256 or P-384 pick the curve; named_curve is also the default parameter encoding.
openssl genpkey -algorithm X25519 -out xkey.pemGenerate an X25519 keyX25519, X448, ED25519 and ED448 take no key generation options, so the algorithm name is all that is needed.
openssl genpkey -algorithm ML-DSA-65 -out ml-dsa-key.pemGenerate an ML-DSA keyML-DSA and ML-KEM key generation is available since OpenSSL 3.5.
openssl genpkey -algorithm ML-KEM-768 -out ml-kem-key.pemGenerate an ML-KEM key
openssl genrsa -out key.pem 2048Generate an RSA key with genrsaMakes RSA keys only; the req manual pairs it with req -new -key to build a CSR in two steps.
openssl pkey -in key.pem -out keyout.pemRemove a key passphraseNo cipher option is given, so the copy written to keyout.pem is not encrypted.
openssl pkey -in key.pem -des3 -out keyout.pemEncrypt an existing keyAny cipher name, such as aes128, can stand in for des3; DER output cannot be encrypted.
openssl pkey -in key.pem -text -nooutPrint key componentsShows the key parameters as plain text; -noout leaves out the encoded PEM copy of the key.
openssl pkey -in key.pem -pubout -out pubkey.pemExtract the public keyOnly the public half is written; together with -text this gives the same output as -text_pub.
openssl pkey -pubin -in FILE -text -nooutRead a public key fileWith -pubin the input is read as a public key instead of a private one.
openssl pkey -in FILE -check -nooutCheck a key pairTests the public and private parts of any key type for consistency.
openssl rsa -in FILE -check -nooutCheck an RSA private keyRuns an RSA consistency test; -noout keeps the key itself from being printed again.

Certificate requests and signing 8

In x509, -signkey was renamed -key in OpenSSL 3.0; the old name still works as an alias.

CommandWhat it does
openssl req -new -key key.pem -out req.pemCreate a CSR from a keyPrompts for the subject fields set up in the configuration file; -subj supplies them on the command line instead.
openssl req -newkey rsa:2048 -keyout key.pem -out req.pemNew key and CSR togetherThe key written to the -keyout file is encrypted with AES-256-CBC unless -noenc is added.
openssl req -new -subj "/C=GB/CN=foo" -addext "subjectAltName = DNS:foo.co.uk" -newkey rsa:2048 -keyout key.pem -out req.pemCSR with subject and SANNo prompts: -subj sets the name, and each -addext adds one extension written as a config file key=value pair.
openssl req -in req.pem -text -verify -nooutExamine and verify a CSRThe self-signature is checked first, and nothing is printed if that check fails.
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out req.pemSelf-signed certificateWrites a certificate instead of a request, valid 30 days unless -days says otherwise; -noenc skips key encryption.
openssl x509 -req -in careq.pem -extfile openssl.cnf -extensions v3_ca -key key.pem -out cacert.pemSelf-sign a CSR as a CA-key does the self-signing (-signkey is its old alias); -extfile and -extensions add the v3_ca section from openssl.cnf.
openssl x509 -req -in req.pem -extfile openssl.cnf -extensions v3_usr -CA cacert.pem -CAkey key.pem -CAcreateserialSign a CSR with your CAThe CA subject becomes the issuer; -CAcreateserial creates the serial number file when it does not exist yet.
openssl x509 -x509toreq -in cert.pem -out req.pem -key key.pemTurn a certificate into a CSRExtensions of the certificate are dropped unless -copy_extensions or -extfile brings them across.

Reading certificates 13

CommandWhat it does
openssl x509 -in cert.pem -noout -textShow certificate detailsIncludes subject, issuer, serial, public key and extensions; -noout drops the encoded certificate from the output.
openssl x509 -in cert.pem -noout -ext subjectAltNameShow the SAN extensionTakes a comma-separated list, so subjectAltName,nsCertType prints both extensions.
openssl x509 -in cert.pem -noout -subjectShow the subject nameAdd -nameopt RFC2253 to print the name in RFC 2253 form.
openssl x509 -in FILE -noout -issuerShow the issuer nameAccepts the same -nameopt display options as -subject.
openssl x509 -in cert.pem -noout -serialShow the serial number
openssl x509 -in FILE -noout -datesShow validity datesPrints notBefore and notAfter; -startdate or -enddate prints only one of them.
openssl x509 -in FILE -noout -enddateShow the expiry dateAdd -dateopt iso_8601 for an ISO 8601 date instead of the default RFC 822 format.
openssl x509 -in chain.pem -multi -checkend $[3600*24*30]Check for coming expiryExit status is nonzero when any certificate in the file expires within 30 days; without -multi only one is checked.
openssl x509 -sha1 -in cert.pem -noout -fingerprintShow the fingerprintHashes the DER form of the certificate; use -sha256 in place of -sha1 for a SHA-256 fingerprint.
openssl x509 -in FILE -noout -pubkeyExtract the certificate public keyThe SubjectPublicKeyInfo block is printed in PEM form.
openssl x509 -in FILE -noout -checkhost HOSTCheck the host nameReports whether the certificate matches that host; -checkip and -checkemail test an address or email instead.
openssl x509 -in FILE -noout -purposeList certificate purposesRuns the extension checks for each predefined purpose, such as SSL server, and prints the result of each.
openssl x509 -in FILE -noout -ocsp_uriShow the OCSP responder URL

Converting formats 5

CommandWhat it does
openssl x509 -in cert.pem -inform PEM -out cert.der -outform DERConvert a certificate to DERSwap the -inform and -outform values to turn a DER certificate back into PEM.
openssl pkey -in key.pem -outform DER -out keyout.derConvert a private key to DERDER output cannot be encrypted, so do not combine it with a cipher option.
openssl pkcs7 -in file.pem -print_certs -out certs.pemExtract certificates from PKCS#7Also prints any CRLs in the file, each item preceded by its subject and issuer names.
openssl base64 -in file.bin -out file.b64Base64-encode a fileOutput lines break after 64 characters unless -A is added.
openssl base64 -d -in file.b64 -out file.binDecode a Base64 fileAdd -A when the Base64 input is a single line without newlines.

PKCS#12 bundles 8

CommandWhat it does
openssl pkcs12 -export -in cert.pem -inkey key.pem -out file.p12Bundle a key and certificateProtected with AES-256-CBC and PBKDF2 by default; -passout gives the export password.
openssl pkcs12 -export -in cert.pem -inkey key.pem -out file.p12 -legacyBundle with legacy algorithmsUses the older defaults of the legacy provider instead of AES-256-CBC.
openssl pkcs12 -export -in file.pem -out file.p12 -name "My PSE" -certfile othercerts.pemAdd a friendly name and extra certificates-name sets the label that importing software displays; -certfile adds extra certificates to the bundle.
openssl pkcs12 -in file.p12 -out file.pemUnpack a PKCS#12 fileKey and certificates come out as PEM, with the key encrypted unless -noenc is added.
openssl pkcs12 -in file.p12 -clcerts -out file.pemExtract only the client certificate-cacerts keeps only CA certificates instead; -nokeys and -nocerts leave out keys or certificates.
openssl pkcs12 -in file.p12 -out file.pem -noencUnpack with an unencrypted key-noenc replaces -nodes, which is deprecated since OpenSSL 3.0.
openssl pkcs12 -in file.p12 -info -nooutInspect a PKCS#12 fileShows the structure, algorithms and iteration counts; -noout only verifies the contents without writing them.
openssl pkcs12 -in file.p12 -info -noout -legacyInspect a legacy PKCS#12 fileNeeded for older files protected with algorithms such as RC2-40-CBC.

Verifying certificates 6

OpenSSL ships no trusted CA certificates of its own; the default store is whatever the operating system or distribution configured.

CommandWhat it does
openssl verify CERTIFICATEVerify a certificateSeveral files can be named, one certificate each; with none, a single certificate is read from standard input.
openssl verify -CAfile CAFILE CERTIFICATEVerify against a CA fileTrusts the certificates in CAFILE for this check; -no-CAfile, -no-CApath and -no-CAstore switch the defaults off.
openssl verify -CAfile CAFILE -untrusted FILENAME CERTIFICATESupply intermediate certificatesUntrusted certificates only help build the chain; trust still has to come from -CAfile or the default store.
openssl verify -show_chain -CAfile CAFILE CERTIFICATEShow the built chainCertificates taken from the -untrusted list are flagged as untrusted in the output.
openssl verify -verify_hostname HOSTNAME -CAfile CAFILE CERTIFICATEVerify for a host nameFails unless the name matches a DNS entry in the Subject Alternative Name or the Common Name.
openssl verify -crl_check -CRLfile FILENAME -CAfile CAFILE CERTIFICATECheck revocation with a CRLFails when no valid CRL covers the leaf certificate; -crl_check_all extends the check to the whole chain.

Testing TLS connections 18

CommandWhat it does
openssl s_client -connect servername:443Open a TLS connectionPrints certificate and session information, then sends what you type, such as GET /, to the server.
openssl s_client -brief -starttls smtp -connect smtp.example.com:25Test STARTTLS on a mail serverOther -starttls keywords include pop3, imap, ftp, xmpp, postgres, mysql and ldap.
-servername NAMESet the SNI nameWithout it, the -connect host is sent as SNI when it looks like a DNS name.
-showcertsShow the certificates sentLists the server certificates in the order received, which is not a verified chain.
-briefPrint a short summaryReplaces the long session printout with a few lines of connection parameters.
-tls1_2Require TLS 1.2TLS 1.2 becomes the only version offered, so a server without it fails the handshake.
-tls1_3Require TLS 1.3TLS 1.3 becomes the only version offered; -no_tls1_3 does the opposite and leaves it out.
-alpn PROTOCOLSOffer ALPN protocolsA comma-separated list with the preferred protocol first, for example http/1.1.
-statusAsk for OCSP staplingPrints the stapled OCSP response if the server sends one.
-verify_return_errorStop on verification errorsBy default s_client reports certificate errors but carries on with the handshake.
-CAfile FILETrust a specific CA fileAdds the certificates in that file to the trusted set used to check the server.
-cert FILENAMESend a client certificateUsed only when the server asks for a client certificate; pair it with -key.
-key FILENAMEClient certificate private keyWhen it is left out, the key is read from the -cert file.
-keylogfile FILELog TLS secretsAppends the session secrets so Wireshark or a similar tool can decrypt a capture of the connection.
-ign_eofKeep going at end of inputKeeps a scripted run with no terminal input from closing the connection early.
-crlfSend CR+LF line endingsTurns each typed line feed into CR+LF for servers that require it.
-msgShow protocol messages-trace prints a more verbose decode of the same handshake messages.
openssl s_server -port PORT -cert CERT -key KEY -wwwRun a test TLS serverListens on 4433 when no port is given; -www answers a browser with an HTML page about the session.

Inside an s_client session 5

These letters only work in an interactive session started without -quiet, -ign_eof, -nocommands or -adv.

CommandWhat it does
QQuit the sessionTyped as the first letter of a line; closes the connection and exits s_client.
RRenegotiate the sessionWorks up to TLS 1.2 only.
CReconnect with resumptionTests whether the server accepts a resumed session.
kSend a key updateTLS 1.3 only; the server is not asked to update its own keys.
KKey update with a replyTLS 1.3 only; asks the server to send its own key update back.

Hashes and signatures 5

CommandWhat it does
openssl dgst -sha256 FILESHA-256 checksumSHA-256 is also the default digest, and a digest name works as the command too: openssl sha256.
openssl dgst -listList digest names
openssl dgst -sha256 -sign privatekey.pem -out signature.sign file.txtSign a fileWrites a binary signature; Ed25519, ED448 and ML-DSA keys sign without a digest option.
openssl dgst -sha256 -verify publickey.pem -signature signature.sign file.txtVerify a signaturePrints Verified OK or Verification Failure.
openssl dgst -hmac KEY FILECompute an HMACUses SHA-256 unless another digest is named; the manual recommends openssl mac for this job.

Encrypting files 5

CommandWhat it does
openssl enc -aes128 -pbkdf2 -in file.txt -out file.aes128Encrypt a file with a passwordPrompts for the password; -pbkdf2 runs 10,000 iterations unless -iter sets another count.
openssl enc -aes128 -pbkdf2 -d -in file.aes128 -out file.txtDecrypt a fileThe manual example adds -pass with a pass: source to give the password on the command line.
openssl enc -aes-256-ctr -pbkdf2 -a -in file.txt -out file.aes256Encrypt to Base64 text-a Base64-encodes the ciphertext so it can be pasted into mail or other text.
openssl enc -aes-256-ctr -pbkdf2 -d -a -in file.aes256 -out file.txtDecrypt Base64 ciphertextDecodes the Base64 before decrypting; the manual reads the password from a file with -pass.
openssl enc -listList cipher namesGCM and CCM never appear because enc does not support authenticated modes.

Random data and passwords 4

CommandWhat it does
openssl rand -base64 NUMRandom Base64 stringNUM counts random bytes, not output characters; a K, M, G or T suffix scales it.
openssl rand -hex NUMRandom hex string
openssl rand -out FILE NUMRandom bytes to a fileWrites raw binary bytes; without -out they go to standard output.
openssl passwd -6 -salt STRING PASSWORDHash a password for cryptBuilds a SHA-512 crypt hash, -5 gives SHA-256; leave out the password to be asked for it.

Version and algorithm lists 11

CommandWhat it does
openssl version -aShow full version detailsAdds build date, compiler flags, platform and the OPENSSLDIR directory to the version string.
openssl version -dShow the OPENSSLDIRThe default openssl.cnf configuration file lives in this directory.
openssl helpGet help on commandsBare openssl does the same since OpenSSL 3.0 removed the interactive mode; every command also accepts -help.
openssl list -commandsList standard commandsPut -1 first to print the names in a single column.
openssl list -digest-algorithmsList digest algorithms
openssl list -cipher-algorithmsList cipher algorithms
openssl list -public-key-algorithmsList public key algorithms
openssl list -providersList loaded providersShows name, version and status of each; -verbose adds every provider parameter.
openssl list -disabledList disabled featuresShows what was compiled out of this build.
openssl ciphers -vList TLS cipher suitesWith no cipher list argument the default list is used; -v adds the details of each suite.
openssl ciphers -s -v 'ALL:@SECLEVEL=2'Suites allowed at a security level-s keeps only suites usable at that level and within the configured protocol versions.

Notes

From key to certificate

openssl genpkey -algorithm RSA -out key.pem makes a private key; the same command takes EC, X25519 and, since OpenSSL 3.5, ML-DSA and ML-KEM. openssl req -new -key key.pem -out req.pem turns the key into a certificate signing request for a CA, and openssl req -x509 -newkey rsa:2048 -keyout key.pem -out req.pem skips the CA for a test certificate, valid for 30 days unless -days is given; add -noenc if the key must not be encrypted. openssl pkcs12 -export -in cert.pem -inkey key.pem -out file.p12 bundles key and certificate for software that imports PKCS#12 (PFX) files.

Reading and debugging certificates

openssl x509 -in cert.pem -noout -text prints everything in a certificate, and openssl x509 -in FILE -noout -enddate just the expiry; openssl x509 -in chain.pem -multi -checkend $[3600*24*30] turns that into an exit status a script can test. openssl verify -CAfile CAFILE CERTIFICATE checks a chain against a CA you name; without -CAfile it relies on the trust store the system configured, because OpenSSL has none of its own. openssl s_client -connect servername:443 shows what a server actually sends: add -servername for SNI, -showcerts for every certificate it presents and -verify_return_error to stop at the first verification failure.

Hashing, random data and encryption

openssl dgst -sha256 FILE prints a checksum for comparing downloads, and openssl rand -base64 NUM generates a random secret from NUM bytes. openssl enc -aes128 -pbkdf2 -in file.txt -out file.aes128 encrypts a file with a password and openssl enc -aes128 -pbkdf2 -d -in file.aes128 -out file.txt decrypts it; use the same cipher and -pbkdf2 on both sides. enc does not support GCM, CCM or any other authenticated mode.

Questions

How do I check when a certificate expires?

openssl x509 -in FILE -noout -enddate prints the notAfter date. In a script, openssl x509 -in chain.pem -multi -checkend $[3600*24*30] exits nonzero if any certificate in the file expires within 30 days.

How do I see the certificate chain a server sends?

Run openssl s_client -connect servername:443 with -showcerts added. It lists the certificates in the order the server sent them, which is not the same as a verified chain. Add -servername NAME when the -connect target is an IP address or not the name the server expects.

Should I use -nodes or -noenc?

Use -noenc. The -nodes option of req and pkcs12 is deprecated since OpenSSL 3.0; openssl pkcs12 -in file.p12 -out file.pem -noenc, for example, unpacks a bundle with the key left unencrypted.

Where does openssl verify get its trusted CA certificates?

From the store your operating system or distribution configured; OpenSSL itself ships none. openssl verify -CAfile CAFILE CERTIFICATE names the CA explicitly, and openssl verify -CAfile CAFILE -untrusted FILENAME CERTIFICATE adds intermediates kept in a separate file. openssl version -d shows the OPENSSLDIR directory of the build.

Can openssl enc encrypt with AES-GCM?

No. enc does not support authenticated modes such as GCM and CCM, and the manual says it will not, which is why openssl enc -list does not show them.

Sources and checks

14 more source pages

Every row on this page was matched to a line in the documentation above on . A binding that could not be found there is not listed.

Found a wrong or missing key? Tell us and it gets checked against the source. How we verify describes the process and its limits.