How to Check SSL Certificate Expiration Date in Linux

Summarize this article with AI:

Table of Contents

To check a certificate's expiry date in Linux, use openssl x509 -enddate -noout -in file.pem for a file. For a live site, use curl -vkI https://example.com and read the expire date line. Each method below was run on a test certificate.

Definition: The expiry date, called notAfter in a certificate, is the last moment the certificate is valid. After it, browsers and clients show an error.

How Do You Check a Certificate File?

Run openssl x509 -enddate -noout -in server.crt. The command reads the file and prints the notAfter line. Use -dates if you also want the start date. This works for .pem, .crt and .cer files that are in PEM format, which is the usual case.

openssl x509 enddate output for a test certificate

The end date of a test certificate, read from its file.

For more flags and a full command list, see our guide to the OpenSSL commands that check a certificate.

How Do You Check a Live Website with Curl?

Run curl -vkI https://example.com 2>&1 | grep "expire date". The -v flag shows the TLS details, and -I fetches only the headers. The -k flag lets curl continue when the certificate is expired or self-signed, so you can still read the date.

curl expire date output for a test server

The expire date line from curl against a local test server.

Our shell cannot reach outside sites, so we ran this against a local test server. If curl reports a handshake problem instead, read our fix for the SSL handshake failed error.

How Do You Check a PFX File or a Java Keystore?

Use OpenSSL for a PFX file and keytool for a Java keystore. Both ask for the store password. The PFX command unpacks only the certificate. The keytool command prints the validity range for every entry in the store, including its alias.

keytool and openssl pkcs12 validity dates

The same test certificate read from a Java keystore and a PFX file.

openssl pkcs12 -in server.p12 -nokeys | openssl x509 -noout -enddate
keytool -list -v -keystore server.jks

Keytool printed its dates in local time, which was IST on our test machine. OpenSSL prints GMT, so compare them with care.

How Do You See How Many Days Are Left?

Use a short script that turns the end date into a day count. The script below reads the end date, converts it with date -d, and subtracts today. It exits with 1 when fewer than 30 days remain, which makes it easy to wire into your alerts.

#!/bin/bash
f="$1"
end=$(openssl x509 -enddate -noout -in "$f" | cut -d= -f2)
days=$(( ( $(date -d "$end" +%s) - $(date +%s) ) / 86400 ))
echo "$f expires $end ($days days left)"
[ "$days" -lt 30 ] && echo "WARNING: renew soon" && exit 1

We saved these lines as certdays.sh and ran it on the test certificate. It reported 89 days left and exited with 0.

How Do You Check Many Certificates at Once?

Put the script in a loop. A for loop can walk a list of files or a list of domains. For files, pass each name to the script. For domains, fetch each certificate with s_client and pipe it to openssl x509.

bash loop printing expiry date and days left

A loop over two files prints each end date and the days left.

The test certificate had 89 days left, while the test root CA had 3,649. A list like this shows what to renew first.

How Do You Get an Alert Before a Certificate Expires?

Run the check on a schedule and alert on its exit code. A cron job can call the script daily and send a mail or message when it exits with 1. If you use Let's Encrypt, Certbot also keeps its own renewal timer.

  • Schedule the script daily with cron or a systemd timer.
  • Send the output to mail, chat or a log that you watch.
  • On Certbot hosts, run certbot certificates to list each certificate with its expiry date.

The Certbot documentation says that command shows the expiry date and validity, and that certificates live in /etc/letsencrypt/live. We did not run Certbot, because it is not installed on our test machine.

Where Do Certificates Live on a Linux Server?

The location depends on how the certificate was issued. System CA certificates sit in /etc/ssl/certs on many distributions. Let's Encrypt certificates sit in /etc/letsencrypt/live, under a folder named for the domain. Your web server config shows the exact path, so check it first.

Certificates sit beside their private keys, so keep both folders locked down. You can also check expiry in Chrome if you prefer a browser.

What If the Check Fails or Shows a Strange Date?

Three problems come up most. The certificate may already be expired, it may be self-signed, or your system clock may be wrong. Check the date with date first. Then re-run the command with -k if curl blocks you, to read the dates.

  • Expired: renew the certificate, then reload the web server.
  • Self-signed: curl stops unless you add -k, but OpenSSL still prints the dates.
  • Wrong date: a clock that is off by days can make a good certificate look expired.

For a deeper look at expiry with OpenSSL alone, see checking the expiry date using OpenSSL.

Frequently Asked Questions

How do I check the SSL certificate expiry date in Linux?

For a file, run openssl x509 -enddate -noout -in file.pem. For a live site, run curl -vkI https://example.com 2>&1 | grep "expire date".

How do I check SSL certificate details in Linux?

Run openssl x509 -in certificate.crt -text -noout. It prints the subject, issuer, dates and extensions.

Can I check several certificates at once?

Yes. Loop over files or domains and run the same check on each one.

How do I get automatic expiry notifications?

Run a check script from cron or a systemd timer. Send its output to mail or chat when the exit code is 1.

How do I know if my certificate works in Linux?

Run openssl s_client -connect example.com:443 -servername example.com and look for the verify return code. Code 0 means the chain verified.