An HTTPS host can still serve an old SSL certificate after renewal. curl retrieves the certificate served by the host, and OpenSSL prints its expiry date or checks a renewal window.

curl can expose the server certificate chain through {certs} in --write-out. Sending that output to OpenSSL lets openssl x509 read the first PEM certificate, print the subject, and show the notAfter expiration timestamp without depending on verbose handshake text.

An expiry date alone does not prove that a certificate is trusted or matches the requested hostname. curl validates the certificate chain and hostname by default; openssl x509 only parses the certificate and checks the requested date window. Normal checks keep validation enabled, with an insecure fallback reserved for inspecting a broken endpoint.

Steps to check SSL certificate expiration with curl and OpenSSL:

  1. Print the presented certificate subject and expiration date for the exact HTTPS hostname.
    $ curl --silent --show-error --output /dev/null \
      --write-out '%{certs}' https://example.com \
      | openssl x509 -noout -subject -enddate
    subject=CN=example.com
    notAfter=Dec 25 22:56:35 2026 GMT

    This requires curl 7.88.0 or newer with an OpenSSL, GnuTLS, Schannel, or Rustls backend; older versions report unknown --write-out variable: 'certs'. The notAfter time is the expiry in GMT (UTC). If curl prints an error, the check failed, even when a date or result still appears after it.

  2. Test whether the same certificate stays valid for at least 30 more days.
    $ curl --silent --show-error --output /dev/null \
      --write-out '%{certs}' https://example.com \
      | openssl x509 -noout -checkend 2592000
    Certificate will not expire

    -checkend 2592000 checks the next 30 days, given in seconds. Certificate will not expire means the certificate lasts beyond that window, and Certificate will expire means renewal is due.

  3. Read the expiry date with --insecure only if the strict check cannot produce a parseable certificate.

    --insecure disables certificate trust and hostname validation, so an impersonating server can supply the certificate shown. Do not use it with credentials or in recurring checks.

    $ curl --silent --show-error --insecure --output /dev/null \
      --write-out '%{certs}' https://expired.badssl.com \
      | openssl x509 -noout -subject -enddate
    subject=OU=Domain Control Validated, OU=PositiveSSL Wildcard, CN=*.badssl.com
    notAfter=Apr 12 23:59:59 2015 GMT
Share this guide