OpenSSL Command to Generate, View & Check Certificates
In this article
The primary OpenSSL commands are openssl req -new -x509 -keyout key.pem -out cert.pem -days 365 to generate a self-signed certificate, openssl x509 -in cert.pem -text -noout to view its details, and openssl verify -CAfile ca.pem cert.pem to check a certificate against a CA. This article explains each command in depth, along with common variations for CSRs, private keys, and certificate chains.
Background: What OpenSSL Does
OpenSSL is an open-source cryptographic toolkit that implements the SSL and TLS protocols. Most web servers (Apache, Nginx) and many applications rely on it to generate private keys, create certificate signing requests (CSRs), issue self-signed certificates, and verify the authenticity of certificates presented by remote servers. It is a command-line utility available on Linux, macOS, and Windows (via distributions like Cygwin or native builds).
Certificates follow the X.509 standard, which defines the format of public key certificates. They contain the subject (who the certificate belongs to), the issuer (who signed it), validity dates, the public key, and a digital signature. OpenSSL provides the tools to create and inspect all these fields.
Generating a Private Key and Self-Signed Certificate
The most common one-liner to generate a self-signed certificate (useful for testing or internal use) is:
openssl req -new -x509 -keyout key.pem -out cert.pem -days 365 -nodesThis command does the following:
req: invokes the certificate request and generation utility.-new: creates a new certificate request.-x509: outputs a self-signed certificate instead of a CSR (this is what makes it a certificate).-keyout key.pem: writes the private key tokey.pem.-out cert.pem: writes the certificate tocert.pem.-days 365: sets the certificate validity to 365 days.-nodes: (no DES) leaves the private key unencrypted, so the server can read it without a passphrase. Omit this if you want a password-protected key.
You will be prompted for a passphrase (unless -nodes is used) and for certificate fields: country, state, locality, organization, common name (the domain name or IP), and email. The common name is critical—it must match the hostname you will use.
Viewing a Certificate
To view the contents of a certificate in a human-readable format:
openssl x509 -in cert.pem -text -nooutThis prints the certificate's subject, issuer, serial number, validity period, public key algorithm and key size, extensions (like Subject Alternative Names), and the signature algorithm. The -noout flag suppresses the base64-encoded certificate itself, leaving only the decoded text.
To view only specific fields, you can use -subject and -issuer:
openssl x509 -in cert.pem -subject -issuer -nooutFor a certificate in DER format (binary), add -inform DER:
openssl x509 -in cert.der -inform DER -text -nooutChecking a Certificate Against a CA
To verify that a certificate is validly signed by a given CA (certificate authority) and that it is currently within its validity period:
openssl verify -CAfile ca.pem cert.pemThis returns cert.pem: OK if the chain is valid. If there are intermediate certificates, you need to provide them in a bundle:
openssl verify -CAfile ca.pem -untrusted intermediate.pem cert.pemThe -untrusted option lists intermediate certificates that help build the chain but are not trusted roots themselves.
Generating a CSR (for a Real CA)
If you need a certificate from a commercial CA (like Let's Encrypt), you first generate a private key and a CSR:
openssl req -new -newkey rsa:2048 -nodes -keyout key.pem -out request.csrThis creates a 2048-bit RSA key and a CSR. You submit the CSR to the CA, which signs it and returns a certificate. To view a CSR:
openssl req -in request.csr -text -nooutChecking a Remote Server's Certificate
To inspect the certificate presented by a remote server (e.g., a website), use the s_client command:
openssl s_client -connect example.com:443 -showcertsThis opens a TLS connection and prints the server's certificate chain. To extract just the leaf certificate to a file:
openssl s_client -connect example.com:443 -showcerts < /dev/null | openssl x509 -outform PEM > server.pemThis is useful for debugging certificate errors on a live site.
Common Problems and Solutions
- Certificate expired: Check the validity dates with
openssl x509 -in cert.pem -dates -noout. Regenerate with a longer-daysvalue. - Hostname mismatch: The common name or Subject Alternative Name must match the hostname. Use
-addext "subjectAltName=DNS:example.com"when generating a self-signed cert. - Key too weak: Modern systems expect at least 2048-bit RSA. Use
-newkey rsa:2048. - Passphrase prompts: If the server asks for a passphrase, either remove it with
openssl rsa -in key.pem -out key_nopass.pemor configure the server to provide it. - Chain incomplete: Some servers require the full chain (leaf + intermediates). Concatenate them:
cat cert.pem intermediate.pem > fullchain.pem.
Frequently Asked Questions
How do I generate a self-signed certificate with a SAN?
Use the -addext option: openssl req -new -x509 -keyout key.pem -out cert.pem -days 365 -nodes -addext "subjectAltName=DNS:example.com,DNS:www.example.com". This adds Subject Alternative Names, which modern browsers require even for self-signed certs.
What is the difference between a CSR and a certificate?
A CSR (Certificate Signing Request) is a file you create with your public key and identifying information, which you send to a CA. The CA signs it and returns a certificate—a file that contains your public key plus the CA's signature, proving the identity. You never use the CSR on the server; you use the certificate.
How do I check when a certificate expires?
Run openssl x509 -in cert.pem -dates -noout. This prints the notBefore and notAfter dates. You can also use -enddate to get just the expiration date.
Why does my browser still warn about a self-signed certificate?
Browsers do not trust self-signed certificates because there is no CA to verify the identity. To avoid the warning, install the certificate in the system trust store (not recommended for public sites) or use a CA like Let's Encrypt for free trusted certificates.
Can I use the same key for multiple certificates?
Yes, you can reuse a private key to generate multiple CSRs or certificates. However, it is generally better to use a new key per certificate for security and to avoid complications if one key is compromised.