OpenSSL commands cheat sheet
Private keys
-
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out example.keyCreates a 2048-bit RSA private key, the common length for SSL certificates. The key is saved unencrypted, so restrict the file permissions to the owner only and do not share it. Anyone who holds the key can impersonate the website.
-
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out example.keyCreates a 4096-bit RSA key. The key is stronger, but setting up a connection is slower and the key is longer, and for most websites a length of 2048 bits is enough. An EC key provides a similar level of security with a much shorter and faster key.
-
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -aes256 -out example.keyCreates an RSA key encrypted with a passphrase using AES-256, and the command asks you to type the passphrase twice. An encrypted key stays protected even if the file leaks, but a web server that loads it will ask for the passphrase every time it starts, so server keys are usually saved unencrypted with restricted permissions.
-
openssl genrsa -out example.key 2048The older way to create an RSA key, which is still supported and appears in many guides. The result is practically the same as with genpkey, but genpkey is the general command that works for all key types.
-
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out example.keyCreates an EC (ECDSA) key on the P-256 curve, also called prime256v1. A 256-bit key provides security similar to a much longer RSA key, the connection is faster, and certificate authorities and browsers support it widely.
-
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-384 -out example.keyCreates an EC key on the P-384 curve, also called secp384r1. The curve is stronger than P-256 and is sometimes required by strict security standards, but for a regular website P-256 is enough.
-
openssl ecparam -name prime256v1 -genkey -noout -out example.keyThe older way to create an EC key. The -noout option keeps the curve parameters block (EC PARAMETERS) out of the file, so the file contains only the key. The file is saved in the EC-specific format, which starts with the line BEGIN EC PRIVATE KEY.
-
openssl genpkey -algorithm ED25519 -out example.keyCreates an Ed25519 key, a modern, fast signature algorithm with a short key. Public certificate authorities do not usually issue SSL certificates for such a key, so it suits mainly file signing and internal services that support it.
-
openssl pkey -in example.key -aes256 -out example-encrypted.keyEncrypts an existing private key with a passphrase and saves it to a new file. The command is useful for backing up or transferring a key. It is best to keep the passphrase separate from the file, because without the passphrase the key cannot be recovered.
-
openssl pkey -in example-encrypted.key -out example.keyRemoves the encryption from a private key after you type the passphrase, for example so the web server can load it without a passphrase. Caution: the new file is not protected, so restrict its permissions to the owner only.
-
openssl pkey -in example-encrypted.key -aes256 -out example-new.keyChanges the passphrase of an encrypted key: the command asks for the current passphrase and then for a new one, and saves the key to a new file. After you check that the new file opens correctly, you can delete the old file.
-
openssl pkey -in example.key -pubout -out example.pubExtracts the public key from the private key. You can share the public key freely, and it is used to verify signatures and to check that a key matches a certificate.
-
openssl pkey -in example.key -noout -textShows the details of the private key, including its type and length in bits, and for an RSA key also the modulus and the exponent. The first line of the output is enough to identify the key length. Caution: the output includes the private key itself. Keep the output in a safe place and do not share it.
-
openssl pkey -pubin -in example.pub -noout -textShows the details of a public key, such as the key type, its length, and the curve of an EC key. The -pubin option states that the file contains a public key, not a private key.
-
openssl rsa -in example.key -check -nooutChecks that an RSA key is valid, and returns RSA key ok when it is. The check finds a damaged file or a key that was edited by mistake, but it does not check whether the key matches a particular certificate.
Example:
RSA key ok -
openssl ec -in example.key -check -nooutChecks that an EC key is valid, like the RSA key check. When the key is valid, the output ends with the line EC Key valid.
Example:
EC Key valid. -
openssl pkcs8 -topk8 -in example.key -nocrypt -out example-pkcs8.keyConverts a key from the format specific to its key type, which starts with the line BEGIN RSA PRIVATE KEY or BEGIN EC PRIVATE KEY, to the general PKCS#8 format, which starts with the line BEGIN PRIVATE KEY. Newer versions of OpenSSL create keys in PKCS#8 by default, so the conversion is needed mainly for old keys or keys created with other tools. The -nocrypt option saves the key unencrypted, and without it the command asks for a passphrase and encrypts the key.
Certificate signing requests (CSR)
-
openssl req -new -key example.key -out example.csrCreates a certificate signing request (CSR) from an existing private key, and asks about the request details, such as the country, the organization name, and the Common Name. You send the CSR file to the certificate authority, and the private key stays with you. To leave a field empty, enter a dot.
-
openssl req -new -key example.key -subj "/C=IL/O=Example Ltd/CN=example.com" -out example.csrCreates a CSR without prompts, with the request details set in the -subj option. The fields are C for the two-letter country code, O for the organization name, and CN for the domain name. For a DV certificate, the certificate authority usually ignores every field except the domain name.
-
openssl req -new -key example.key -subj "/CN=example.com" -addext "subjectAltName=DNS:example.com,DNS:www.example.com" -out example.csrCreates a CSR with a list of Subject Alternative Names (SAN), which includes every name the certificate needs to cover. Browsers check the website name against the SAN list, not against the CN field, so the list must also include the name in the CN. A wildcard certificate is requested with a name such as *.example.com.
-
openssl req -new -newkey rsa:2048 -nodes -keyout example.key -out example.csr -subj "/CN=example.com"Creates a new RSA key and a CSR in one command. The -nodes option saves the key unencrypted. Since OpenSSL 3.0 it is deprecated, and the current name is -noenc, but both names still work. Without the option, the command asks for a passphrase and encrypts the key. Caution: do not run the command in a folder that holds an active key with the same name, because the file will be overwritten.
-
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout example.key -out example.csr -subj "/CN=example.com"Creates a new EC key on the P-256 curve and a CSR in one command. Here too, -nodes saves the key unencrypted, so restrict the file permissions to the owner only. An existing key file with the same name will be overwritten.
-
printf '[req]\nprompt = no\ndistinguished_name = dn\nreq_extensions = ext\n[dn]\nCN = example.com\n[ext]\nsubjectAltName = DNS:example.com,DNS:www.example.com\n' > san.cnfCreates a configuration file named san.cnf for a CSR with alternative names. The file is convenient when there are many names, or when you create repeated requests with the same details. You can edit the file and add more names to the subjectAltName line, separated by commas.
-
openssl req -new -key example.key -config san.cnf -out example.csrCreates a CSR from the san.cnf configuration file, including the main name and the alternative names set in it. The prompt = no setting in the file turns off the interactive prompts, so the details come from the file only.
-
openssl req -in example.csr -noout -textShows all the CSR details: the Common Name, the key type and length, the signature algorithm, and the alternative names. It is best to run this check before you send the request to the certificate authority, to make sure the request includes every name you need.
-
openssl req -in example.csr -noout -subjectShows only the subject fields of the CSR, such as the Common Name and the organization, for a quick check.
Example:
subject=CN=example.com -
openssl req -in example.csr -noout -text | grep -A1 "Subject Alternative Name"Shows only the list of Subject Alternative Names (SAN) in the request. When the output is empty, the request includes no alternative names, and some certificate authorities will include only the CN name in the certificate.
Example:
DNS:example.com, DNS:www.example.com -
openssl req -in example.csr -noout -verifyVerifies the CSR signature, meaning that the request was signed with the private key that matches the public key inside it and was not changed after signing. The certificate authority will reject a request that fails this check.
Example:
Certificate request self-signature verify OK
Self-signed certificates and a local CA
-
openssl req -x509 -newkey rsa:2048 -nodes -keyout example.key -out example.crt -days 365 -subj "/CN=example.com" -addext "subjectAltName=DNS:example.com,DNS:www.example.com"Creates a new key and a self-signed certificate valid for one year, in one command. Such a certificate encrypts the connection, but browsers show a warning because no certificate authority has approved it. It suits development environments and internal services, not a public website. The key is saved unencrypted, and existing files with the same names will be overwritten.
-
openssl req -new -x509 -key example.key -days 365 -subj "/CN=example.com" -addext "subjectAltName=DNS:example.com" -out example.crtCreates a self-signed certificate from an existing private key. The -days option sets the number of days the certificate is valid, and without it the certificate is valid for only 30 days.
-
openssl x509 -req -in example.csr -signkey example.key -days 365 -out example.crtCreates a self-signed certificate from an existing CSR and its key. The alternative names in the request are not copied to the certificate by default, so add them from a file with -extfile, or in newer versions with -copy_extensions copy.
-
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -aes256 -out ca.keyCreates an encrypted private key for a local certificate authority (CA). Anyone who holds this key can issue certificates that every computer trusting the CA will accept, so it is best to encrypt it with a strong passphrase and store it in a protected place.
-
openssl req -new -x509 -key ca.key -sha256 -days 3650 -subj "/CN=Example Local CA" -addext "basicConstraints=critical,CA:TRUE" -addext "keyUsage=critical,keyCertSign,cRLSign" -out ca.crtCreates the root certificate of the local CA, valid for ten years. The -addext options explicitly mark the certificate as a CA certificate allowed to sign certificates, regardless of the system configuration file. For browsers and systems to trust the certificates the CA signs, install the ca.crt file in the trusted certificate store of each computer or device.
-
openssl x509 -in ca.crt -noout -text | grep -A1 "Basic Constraints"Checks that the certificate is marked as a CA certificate, which may sign other certificates. A certificate without this marking is not accepted as a certificate authority, and verification of the certificates it signs fails.
Example:
CA:TRUE -
printf 'basicConstraints = CA:FALSE\nkeyUsage = digitalSignature, keyEncipherment\nextendedKeyUsage = serverAuth\nsubjectAltName = DNS:example.com,DNS:www.example.com\n' > example.extCreates an extensions file for a server certificate that the local CA signs. The file states that the certificate is not a CA, that it is meant for a web server, and its alternative names. Browsers require alternative names, and a certificate without a SAN is rejected even when the CA is trusted.
-
openssl x509 -req -in example.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 365 -sha256 -extfile example.ext -out example.crtSigns a CSR with the local CA and issues a server certificate according to the extensions file. On the first signing, the -CAcreateserial option creates the ca.srl file, which stores the serial number, and on later signings the number is read from the file and incremented automatically.
-
openssl verify -CAfile ca.crt example.crtChecks that the certificate was signed by the local CA and that it is valid. The OK output means the verification succeeded, and on failure the reason for the error is shown.
Example:
example.crt: OK
Inspecting certificates
-
openssl x509 -in example.crt -noout -textShows all the certificate details: the subject, the issuer, the validity dates, the serial number, the key type, the alternative names, and the extensions. This is the basic command for inspecting any certificate in PEM format.
-
openssl x509 -in example.crt -noout -datesShows the date the certificate becomes valid (notBefore) and its expiration date (notAfter). The times are shown in UTC, which the output marks as GMT.
Example:
notAfter=Feb 22 23:59:59 2027 GMT -
openssl x509 -in example.crt -noout -enddateShows only the certificate expiration date, for a quick check or for use in a script.
Example:
notAfter=Feb 22 23:59:59 2027 GMT -
openssl x509 -in example.crt -noout -checkend 2592000Checks whether the certificate will expire within the given number of seconds, here 30 days. The command returns exit code 0 when the certificate will still be valid after that period, and exit code 1 when it will expire, so it suits an alert script. A value of 0 checks whether the certificate has already expired.
Example:
Certificate will not expire -
openssl x509 -in example.crt -noout -subject -issuerShows the subject of the certificate, meaning who it was issued to, and the issuer, meaning the certificate authority or intermediate certificate that signed it. In a self-signed certificate, both values are the same.
Example:
subject=CN=example.com -
openssl x509 -in example.crt -noout -text | grep -A1 "Subject Alternative Name"Shows the list of Subject Alternative Names (SAN) the certificate covers. Browsers check the website name against this list only, so a name that does not appear in it causes a warning. In newer versions you can also use the -ext subjectAltName option.
Example:
DNS:example.com, DNS:www.example.com -
openssl x509 -in example.crt -noout -fingerprint -sha256Calculates the certificate fingerprint with a SHA-256 hash. The value identifies the certificate uniquely, and is used to compare certificates and for certificate pinning in apps.
-
openssl x509 -in example.crt -noout -serialShows the certificate serial number in hexadecimal. The certificate authority identifies a certificate by this number, for example in a revocation request.
Example:
serial=04A1B2C3D4E5F60718293A4B5C6D7E8F9012 -
openssl x509 -in example.crt -noout -pubkeyExtracts the public key from the certificate, in PEM format. The output is used for comparison with the private key and for verifying signatures.
-
openssl x509 -in example.crt -noout -text | grep "Signature Algorithm"Shows the certificate signature algorithm, for example sha256WithRSAEncryption or ecdsa-with-SHA256. Browsers reject certificates signed with old algorithms such as SHA-1.
Example:
Signature Algorithm: sha256WithRSAEncryption -
openssl x509 -in example.crt -noout -purposeShows which purposes the certificate suits, such as SSL server, SSL client, or email signing. The check is useful when a service rejects a certificate as unsuitable for the purpose.
-
openssl x509 -in example.crt -noout -ocsp_uriShows the address of the certificate authority's OCSP server, where you can check whether the certificate was revoked. Empty output means the certificate does not include such an address.
-
openssl x509 -in example.der -inform DER -noout -textShows the certificate details when it is stored in the binary DER format, which is common in files with the der or cer extension. Newer versions of OpenSSL detect the format without the option, but older versions require it.
-
openssl crl2pkcs7 -nocrl -certfile fullchain.pem | openssl pkcs7 -print_certs -nooutShows the subject and issuer of each certificate in a chain file. The x509 command reads only the first certificate in the file, so this is how you make sure the file includes all the certificates, in the right order.
-
openssl x509 -in example.crt -noout -subject_hashCalculates the hash of the subject name, which OpenSSL uses to find certificates in a trusted certificates folder. The files in such a folder are named by this value, for example 1a2b3c4d.0.
Example:
1a2b3c4d
Matching keys, certificates, and CSRs
-
openssl x509 -in example.crt -noout -pubkey | openssl sha256Calculates a hash of the public key in the certificate. When the hash is the same as the hash of the public key extracted from the private key, the certificate and the key match. The method works for all key types, both RSA and EC.
-
openssl pkey -in example.key -pubout | openssl sha256Calculates a hash of the public key extracted from the private key, for comparison with the hash of the certificate or the CSR. The hash is calculated on the public key only, so the private key is not shown.
-
openssl req -in example.csr -noout -pubkey | openssl sha256Calculates a hash of the public key in the CSR. A hash that matches the private key confirms that the request was created from that key, and a hash that matches the certificate confirms that the certificate was issued for this request.
-
diff <(openssl x509 -in example.crt -noout -pubkey) <(openssl pkey -in example.key -pubout) && echo "match"Compares the public key in the certificate directly with the public key extracted from the private key, and prints match when they are the same. When they do not match, the differences are shown and the word match is not printed. The command is meant for the bash shell.
Example:
match -
openssl x509 -in example.crt -noout -modulus | openssl sha256The older way to check that a certificate with an RSA key matches its key: calculates a hash of the modulus in the certificate, for comparison with the modulus of the key. The method suits RSA keys only, and for EC keys compare the public key instead.
-
openssl rsa -in example.key -noout -modulus | openssl sha256Calculates a hash of the modulus of a private RSA key, for comparison with the hash of the certificate. Different values mean the key does not match the certificate, and the web server will not start with this combination.
-
openssl req -in example.csr -noout -modulus | openssl sha256Calculates a hash of the modulus in a CSR with an RSA key, for comparison with the private key and with the issued certificate.
Converting formats
-
openssl x509 -in example.crt -outform DER -out example.derConverts a certificate from PEM format, text that starts with the line BEGIN CERTIFICATE, to the binary DER format. Some systems, mainly Windows and Java, expect a binary file, sometimes with the cer extension.
-
openssl x509 -in example.der -inform DER -out example.crtConverts a certificate from DER format to PEM format, which most web servers on Linux use. A file with the cer extension can be in either format, so it is best to open it in a text editor: a PEM file is readable, and a DER file is binary.
-
openssl pkey -in example.key -outform DER -out example-key.derConverts a private key from PEM format to DER format. The new file is not encrypted, so keep it with restricted permissions.
-
openssl pkey -in example-key.der -inform DER -out example.keyConverts a private key from DER format to PEM format.
-
openssl pkcs12 -export -inkey example.key -in example.crt -certfile chain.crt -out example.pfxBundles the private key, the certificate, and the intermediate certificates into one PKCS#12 file, with the pfx or p12 extension, used mainly on Windows, IIS, and Java. The command asks for a password for the file, and it is best to set one, because the file includes the private key.
-
openssl pkcs12 -export -inkey example.key -in example.crt -certfile chain.crt -name "example.com" -out example.pfxCreates a PFX file with a Friendly Name, which the Windows certificate management tools show, and which makes the certificate easier to identify when several certificates are installed.
-
openssl pkcs12 -export -legacy -inkey example.key -in example.crt -certfile chain.crt -out example.pfxCreates a PFX file with the older encryption algorithms. Newer versions of OpenSSL encrypt PFX files with AES by default, which older systems, such as older versions of Windows, cannot open. In older versions of OpenSSL, the -legacy option does not exist, and their default is already the older format. It is best to use the option only when the target system rejects the regular file.
-
openssl pkcs12 -in example.pfx -info -nooutShows the structure of the file and its encryption algorithms after you type the password, without extracting the content. The check helps you tell whether a file was created with old or new encryption.
-
openssl pkcs12 -in example.pfx -out example.pem -nodesConverts a PFX file to a single PEM file that includes the private key, the certificate, and the intermediate certificates. The -nodes option saves the key unencrypted. Since OpenSSL 3.0 it is deprecated, and the current name is -noenc, but both names still work. Caution: the new file includes an unencrypted private key, so restrict its permissions.
-
openssl pkcs12 -in example.pfx -clcerts -nokeys -out example.crtExtracts only the server certificate from a PFX file, without the key and without the intermediate certificates.
-
openssl pkcs12 -in example.pfx -nocerts -nodes -out example.keyExtracts only the private key from a PFX file, unencrypted. Caution: restrict the file permissions to the owner only. When you need an encrypted key, omit -nodes and set a new passphrase.
-
openssl pkcs12 -in example.pfx -cacerts -nokeys -out chain.crtExtracts only the intermediate certificates and the root certificate from a PFX file, if they were included in it. You add these certificates to the server certificate to build the full chain.
-
openssl pkcs12 -in example.pfx -out example.pem -nodes -legacyOpens an old PFX file encrypted with older algorithms such as RC2. Newer versions of OpenSSL do not load these algorithms by default and show an unsupported algorithm error, and the -legacy option makes reading the file possible.
-
openssl pkcs7 -in example.p7b -print_certs -out example.pemConverts a PKCS#7 file, with the p7b or p7c extension, to a PEM file that includes all of its certificates. A PKCS#7 file contains certificates only, without a private key, so take the key from where the CSR was created. When the file is binary, add -inform DER.
-
openssl crl2pkcs7 -nocrl -certfile example.crt -certfile chain.crt -out example.p7bCreates a PKCS#7 file from the server certificate and the intermediate certificates, for systems that expect a p7b file, such as some Windows and Java servers.
-
openssl x509 -in example.crt -out example-clean.crtRewrites a certificate as clean PEM and removes text that comes before the BEGIN CERTIFICATE line, such as Bag Attributes lines created when extracting from a PFX file. Some servers refuse to load a file that includes such text.
Verifying certificate chains
-
openssl verify -CAfile root.crt -untrusted intermediate.crt example.crtVerifies the full chain: the server certificate, the intermediate certificate that signed it, and the root certificate. The -CAfile option sets the trusted certificate, and the -untrusted option sets intermediate certificates used only to build the chain.
Example:
example.crt: OK -
openssl verify -untrusted chain.crt example.crtVerifies the certificate against the system's trusted certificate store, with the help of the intermediate certificates in the file. This way you can check that the chain you received from the certificate authority is complete before you install it on the server.
-
openssl verify -partial_chain -CAfile intermediate.crt example.crtChecks that the server certificate was signed by a specific intermediate certificate, without needing the root certificate. The check helps you find a wrong intermediate certificate, for example after the certificate authority replaced one.
-
openssl verify -CAfile chain.pem -verify_hostname example.com example.crtVerifies the chain and also checks that the certificate covers the given domain name. When the name is not in the list of alternative names, verification fails with a hostname mismatch error.
-
cat example.crt intermediate.crt > fullchain.pemBuilds a full chain file (fullchain) from the server certificate and the intermediate certificate. The order matters: the server certificate comes first, followed by the intermediate certificates in signing order. There is no need to include the root certificate, because the browser already has it.
-
openssl x509 -in example.crt -noout -text | grep "CA Issuers"Shows the address where you can download the intermediate certificate that signed the certificate (AIA). The address is useful when the intermediate certificate is missing, and the file downloaded from it is usually in DER format, which you convert to PEM before adding it to the chain.
Example:
CA Issuers - URI:http://ca.example.com/intermediate.crt -
openssl x509 -in intermediate.crt -noout -subjectShows the subject of the intermediate certificate. The value should be the same as the issuer field of the server certificate, which is a quick way to make sure you have the right intermediate certificate.
-
openssl verify -purpose sslserver -CAfile root.crt -untrusted intermediate.crt example.crtVerifies the chain and also checks that the certificate is suitable for use on an SSL server. The check finds a certificate issued for another purpose, such as code signing or client authentication.
Testing remote servers
-
openssl s_client -connect example.com:443 -servername example.comConnects to the server over TLS and shows the certificate, the chain the server sends, the protocol version, and the selected cipher. The -servername option sends the website name (SNI), which a server hosting several websites uses to choose the certificate. Newer versions send the name automatically based on the address in -connect, but it is best to state it explicitly. To close the connection, press Ctrl+C.
-
openssl s_client -connect example.com:443 -servername example.com </dev/nullConnects to the server, shows the connection details, and disconnects right away, without waiting for input. This form is the basis for checks that pass the output to another command.
-
openssl s_client -connect example.com:443 -servername example.com -brief </dev/nullShows a short summary of the connection: the protocol version, the cipher, the certificate subject, and the verification result. The command suits a quick check without the full output.
Example:
Protocol version: TLSv1.3 -
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/nullShows all the certificates the server sends, not only the server certificate. This way you can check whether the server sends the intermediate certificates: when only one certificate appears, the chain is incomplete, and some browsers and devices will show an error.
-
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -datesChecks the validity dates of the certificate the server actually presents. The check finds cases where a certificate was renewed but the server still presents the old one, for example because it was not restarted after the renewal.
Example:
notAfter=Feb 22 23:59:59 2027 GMT -
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -checkend 1209600Checks whether the server's certificate will expire within 14 days, and returns exit code 1 if it will. The command suits a scheduled task that alerts you before the certificate expires.
Example:
Certificate will not expire -
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuerShows who the certificate the server presents was issued to, and who its issuer is. The check confirms that the server presents the right certificate and that the certificate authority is the expected one.
-
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -text | grep -A1 "Subject Alternative Name"Shows the alternative names in the certificate the server presents. When the website shows a name mismatch warning, the check shows which names the certificate actually covers.
Example:
DNS:example.com, DNS:www.example.com -
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -fingerprint -sha256Calculates the fingerprint of the certificate the server presents, for comparison with the fingerprint of the local certificate file. Matching values confirm that the server presents exactly the certificate that was installed.
-
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -out example-server.crtSaves the certificate the server presents to a local PEM file, for detailed inspection or for use with other commands. The file name differs from example.crt so the local certificate file is not overwritten.
-
openssl s_client -connect example.com:443 -servername example.com -verify_return_error -verify_hostname example.com </dev/nullConnects and verifies the chain and the hostname against the system's trusted certificate store. The -verify_return_error option makes the connection fail on a verification error, instead of continuing and only showing it. The end of the output shows the Verify return code line, and the value 0 means verification succeeded.
Example:
Verify return code: 0 (ok) -
openssl s_client -connect 192.0.2.10:443 -servername example.com </dev/nullConnects to a specific IP address and sends the website name in SNI, to check the certificate on a new server before you point the DNS to it, or on one of several servers that serve the same website. When you connect to an IP address, you must set -servername, otherwise the website name is not sent.
-
openssl s_client -connect example.com:443 -servername example.com -tls1_2 </dev/nullChecks whether the server supports TLS 1.2, with a connection limited to this version only. When the server does not support the version, the connection fails with a handshake error.
-
openssl s_client -connect example.com:443 -servername example.com -tls1_3 </dev/nullChecks whether the server supports TLS 1.3, the latest version of the protocol. When the connection succeeds, the output shows TLSv1.3 as the protocol version.
-
openssl s_client -connect example.com:443 -servername example.com -tls1_1 </dev/nullChecks whether the server still accepts TLS 1.1, an old version that browsers no longer support, and that is best disabled on the server. A failed connection is the result you want, but it may also happen because the local OpenSSL blocks the old version, so such a result is not conclusive proof.
-
openssl s_client -connect example.com:443 -servername example.com -tls1_2 -cipher ECDHE-RSA-AES128-GCM-SHA256 </dev/nullChecks whether the server accepts a specific cipher in TLS 1.2. The connection succeeds only when the server supports the cipher. The cipher in the example is for a server with an RSA certificate, and for a server with an EC certificate, replace RSA in its name with ECDSA. In TLS 1.3, ciphers are set with a separate option, -ciphersuites.
-
openssl s_client -connect example.com:443 -servername example.com -alpn h2,http/1.1 </dev/null 2>/dev/null | grep ALPNChecks which application protocols the server selects through ALPN. The answer h2 means HTTP/2 support, and the answer http/1.1 or No ALPN negotiated means the server does not offer HTTP/2 on this connection.
Example:
ALPN protocol: h2 -
openssl s_client -connect example.com:443 -servername example.com -status </dev/null 2>/dev/null | grep -E "OCSP response:|OCSP Response Status"Checks whether the server attaches a signed OCSP response to the connection (OCSP stapling), which confirms that the certificate was not revoked. When the server does not attach a response, the line OCSP response: no response sent is shown. Some certificate authorities have stopped supporting OCSP, so a missing response is not necessarily a fault.
Example:
OCSP Response Status: successful (0x0) -
printf 'HEAD / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' | openssl s_client -connect example.com:443 -servername example.com -quietSends a basic HTTP request over an encrypted connection and shows the server's response headers. The check lets you inspect redirects and security headers even when the server has no other tools.
-
openssl s_client -connect mail.example.com:587 -starttls smtp -servername mail.example.com </dev/nullConnects to an outgoing mail server on port 587, upgrades the connection to encryption with STARTTLS, and shows the certificate. You can check port 25, which is used for mail transfer between servers, the same way.
-
openssl s_client -connect mail.example.com:143 -starttls imap -servername mail.example.com </dev/nullChecks the certificate of an IMAP server on port 143 with STARTTLS. To check POP3 on port 110, use the -starttls pop3 option, and to check FTP on port 21, use -starttls ftp.
-
openssl s_client -connect mail.example.com:993 -servername mail.example.com </dev/nullChecks the certificate of an IMAP server on port 993, where the connection is encrypted from the start and STARTTLS is not needed. You check SMTP on port 465 and POP3 on port 995 the same way.
Hashing, encryption, and other tools
-
openssl dgst -sha256 example.zipCalculates the SHA-256 hash of a file, to check that a downloaded file is identical to the original. When the website you downloaded the file from publishes a hash value, compare it with the output.
-
openssl dgst -sha512 example.zipCalculates the SHA-512 hash of a file. In the same way, you can use sha384 or sha3-256, depending on the value the publisher provided.
-
openssl dgst -md5 example.zipCalculates the MD5 hash of a file, to compare it with an older value published as MD5. The algorithm is not safe against deliberate changes to files, so do not rely on it for security.
-
printf '%s' 'example text' | openssl dgst -sha256Calculates the hash of a text string. Using printf instead of echo avoids adding a newline character at the end of the string, which would change the hash value.
-
openssl dgst -sha256 -hmac 'secret-key' example.txtCalculates an HMAC, a hash that combines a secret key and is used to verify that a message was not changed and was sent by someone who holds the key, for example when verifying webhooks. Caution: a key typed on the command line may be saved in the shell history.
-
openssl dgst -sha256 -sign example.key -out example.sig example.zipSigns a file with a digital signature using the private key, and saves the signature to a separate file. Anyone who holds the public key can verify that the file was not changed and that it was signed with this key. With an Ed25519 key, omit -sha256, because the algorithm sets the hash itself.
-
openssl dgst -sha256 -verify example.pub -signature example.sig example.zipVerifies a digital signature on a file using the public key. The Verified OK output means the file has not changed since it was signed.
Example:
Verified OK -
openssl base64 -in example.bin -out example.b64Encodes a file in Base64, a text representation of binary data suitable for channels that support only text. The output is split into lines of 64 characters, and for output on a single line, add -A.
-
openssl base64 -d -in example.b64 -out example.binDecodes a Base64 file back to the original file. When the encoded data is on one long line, add -A, otherwise decoding may return an empty result.
-
printf '%s' 'example text' | openssl base64Encodes a text string in Base64. Base64 is an encoding, not encryption, and anyone can decode it.
Example:
ZXhhbXBsZSB0ZXh0 -
openssl rand -hex 32Generates 32 secure random bytes and shows them as 64 hexadecimal characters. The output suits secret keys, tokens, and identifiers that cannot be guessed.
Example:
4451dc131073252e140651547c31676f3701ead9b9d92a03ffd593c0921bcf33 -
openssl rand -base64 24Generates 24 random bytes and shows them in Base64 encoding, as a string of 32 characters. The output suits a strong password or a secret key in a configuration file.
-
openssl rand -out random.bin 1024Creates a 1024-byte file with random content, for example for use as a key file or for testing.
-
openssl enc -aes-256-cbc -salt -pbkdf2 -iter 100000 -in example.txt -out example.txt.encEncrypts a file with a password using AES-256. The -pbkdf2 option derives the encryption key from the password with a method resistant to guessing, and the -iter option sets the number of iterations. The encryption does not protect against changes to the file, so for highly sensitive data it is best to use a dedicated encryption tool.
-
openssl enc -d -aes-256-cbc -pbkdf2 -iter 100000 -in example.txt.enc -out example.txtDecrypts a file encrypted with the enc command. Use the same algorithm, the same -pbkdf2, and the same number of iterations used for encryption, otherwise decryption fails with a bad decrypt error.
-
openssl enc -aes-256-cbc -salt -pbkdf2 -iter 100000 -a -in example.txt -out example.txt.b64Encrypts a file and saves the result in Base64 encoding, so you can copy it as text, for example to paste it into a message. When decrypting, add -a as well.
-
openssl passwd -6Asks for a password and creates a SHA-512 crypt hash from it, which starts with $6$ and is used in the password files of Linux systems and in the configuration files of various services.
-
openssl passwd -apr1Asks for a password and creates a hash from it in the Apache apr1 format, which suits htpasswd files for password-protecting a folder. A line in the file is made of the username, a colon, and the hash.
Example:
$apr1$t4QZNoId$cgKppnS6b9ADICvQERajn0 -
openssl dhparam -out dhparam.pem 2048Creates parameters for Diffie-Hellman key exchange, which some server configurations require. Generating them may take a long time. Most connections today use ECDHE, which does not need this file, so it is needed only when the server configuration points to it.
-
openssl speed rsa2048Measures the speed of signing and verification with a 2048-bit RSA key on the local processor. The test loads the processor and may take about a minute, so it is best to run it at a time of low load.
-
openssl speed -evp aes-256-gcmMeasures the speed of AES-256-GCM encryption, including the processor's hardware acceleration when it is available. The results are shown in thousands of bytes per second, by block size.
-
openssl version -aShows the installed OpenSSL version, the build date, and the folder that holds the configuration file and the certificate store (OPENSSLDIR). It is a good idea to check the version when a command fails on an unknown option.
-
openssl ciphers -v 'HIGH:!aNULL:!MD5'Shows the list of ciphers that match the filter string, with the protocol version, the key exchange method, and the algorithms of each cipher. The command helps you check what a server cipher setting will actually include. TLS 1.3 ciphers are always shown, because they are set separately.
-
openssl list -digest-algorithmsShows the hash algorithms the local version of OpenSSL supports. For the list of encryption algorithms, use list -cipher-algorithms, and for the list of public key algorithms, use list -public-key-algorithms.
No matching commands.
OpenSSL is a command-line tool for creating and managing keys, CSRs, and SSL certificates, converting between formats, testing encrypted connections to servers, and running encryption and hashing operations. It is installed by default on most Linux systems and on macOS, and is also available for Windows. The names example.com, example.key, and example.crt in the commands are examples, so replace them with your real domain and file names. Some options exist only in newer versions of OpenSSL, and you can check the installed version with the openssl version command.
For common checks without the command line, you can use our dedicated tools: the SSL certificate checker to check a website's certificate and chain, the SSL certificate decoder and the CSR decoder to show the details of a certificate or request file, the Hash generator to calculate the hash of text, and the Base64 encoder and decoder for encoding and decoding.
The private key is the most sensitive part: anyone who holds it can impersonate the website. Do not send it by email or paste it into online tools, and keep it with permissions that allow access to the owner only. Commands with -nodes or -noenc save a key unencrypted, and before you run a command that writes to an existing file, it is best to back the file up, because it is overwritten without a warning.
OpenSSL commands FAQ
How do I create a CSR with OpenSSL?
The quickest way is the openssl req -new -newkey rsa:2048 -nodes -keyout example.key -out example.csr -subj "/CN=example.com" command, which creates a new key and a certificate signing request in one step. For a certificate that covers several names, for example www as well, add the -addext option with a subjectAltName list. You send the CSR file to the certificate authority, and you keep the key file on the server and never send it to anyone.
How do I check when an SSL certificate expires?
For a certificate file, use the openssl x509 -in example.crt -noout -enddate command, which shows the expiration date. For the certificate a website actually presents, pass the output of openssl s_client to the same command, and the -checkend option lets a script check whether the certificate will expire within a given period.
How do I convert a PFX file to PEM files?
The openssl pkcs12 -in example.pfx -out example.pem -nodes command converts all the content to a single PEM file, and after you type the file password, you can extract the certificate separately with -clcerts -nokeys and the key with -nocerts. When an unsupported algorithm error appears, the file was created with older encryption, so add the -legacy option.
How do I check that a private key matches a certificate?
Compare the hash of the public key in both files: openssl x509 -in example.crt -noout -pubkey | openssl sha256 for the certificate, and openssl pkey -in example.key -pubout | openssl sha256 for the key. Matching values confirm that the key and the certificate match, and you can check the CSR the same way.
What is a certificate chain, and why does it matter?
A certificate chain is the sequence that links the website certificate to a root certificate the browser trusts, through one or more intermediate certificates. The server needs to send the website certificate together with the intermediate certificates, and an incomplete chain causes errors in some browsers, apps, and devices. You can check the chain the server sends with the openssl s_client command and the -showcerts option.
What is the difference between a self-signed certificate and a certificate from a certificate authority?
Both encrypt the connection at the same level, and the difference is trust: a certificate from a recognized certificate authority is accepted automatically by all browsers, and a self-signed certificate causes a security warning. A self-signed certificate suits development environments and internal services, and a public website should use a certificate from a certificate authority.