Somebody just messaged you that a site “is showing a certificate warning.” You don’t want to open a browser and click through three dialogs to read a date. You want the notAfter value in your terminal, right now, and you want to know it’s right.
OpenSSL will tell you in one line. Here are the commands I’d keep in a notes file, with the gotchas that trip people up. Everything below was run against test certificates with OpenSSL 3.0.13, including the exit codes.
Check the expiration date of a live server
This is the one you’ll use most. It connects to the server, grabs whatever certificate it presents, and hands it to openssl x509 to print the dates:
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -dates
You get two lines back:
notBefore=Oct 28 12:06:56 2026 GMT
notAfter=Jan 26 12:06:56 2027 GMT
A few pieces of that command are doing real work.
-servername sets the SNI value in the TLS handshake. The OpenSSL docs say that if you leave it off, it’s taken from -connect when that looks like a DNS name, otherwise it falls back to localhost. Connect by IP address and forget -servername, and on a server hosting several sites you may well get the wrong certificate back. That’s the number one reason people think a cert “looks fine” when it isn’t.
</dev/null feeds s_client an empty stdin so it doesn’t sit there waiting for you to type. The OpenSSL docs note that non-interactive runs can close the connection early, particularly with TLS 1.3, and the pattern worked fine against my HTTPS test servers.
2>/dev/null hides the connection chatter on stderr so only the certificate goes down the pipe.
If you only want the expiry date, swap -dates for -enddate. The docs describe -enddate as printing the notAfter date, and -dates as printing both the start and expiry dates.
Check a certificate file
Same idea, no network. Point openssl x509 at the file:
openssl x509 -in server.crt -noout -subject -issuer -dates
I usually add -fingerprint -sha256 when I’m trying to work out whether the file on disk is the same certificate the server is presenting:
openssl x509 -in server.crt -noout -fingerprint -sha256
Run the same flag on the output of the s_client pipeline above and compare. If they differ, you renewed the certificate but the service is still serving the old one, which is a very common way to get an outage after a “successful” renewal.
Check the names on the certificate
A certificate with the right date and the wrong names is still broken. Print the subject alternative names with -ext:
openssl x509 -in server.crt -noout -ext subjectAltName
The docs describe -ext as printing the certificate extensions in text form. Browsers match against the SAN list, not the old common name field, so this is the line to read.
Script it with -checkend
Printing a date is fine for a human. For a script you want an exit code. -checkend takes a number of seconds and exits zero if the certificate will still be valid that long, nonzero if it won’t:
# Will this certificate still be valid in 30 days? (30 * 86400 = 2592000 seconds)
openssl x509 -in server.crt -noout -checkend 2592000
echo $?
When it will expire inside that window, OpenSSL prints Certificate will expire and returns 1. When it’s fine, it prints Certificate will not expire and returns 0. I confirmed both against test certs.
Put that together with s_client and you can check a live host in a script:
host=example.com
if ! openssl s_client -connect "$host:443" -servername "$host" </dev/null 2>/dev/null \
| openssl x509 -noout -checkend 2592000 >/dev/null; then
echo "$host: certificate expires within 30 days (or could not be read)"
fi
Notice the last part of that message. If s_client can’t connect, openssl x509 has nothing to parse and also returns nonzero. Your script can’t tell “expiring” from “unreachable” unless you check them separately. Keep that in mind before you wire this into anything that pages someone at 3 a.m.
Two more details from the docs. A certificate that only becomes valid after the window is reported as not expiring, so -checkend isn’t a validity check, only an expiry check. And a certificate that’s already expired returns the nonzero result too, which is what you want.
Mail servers and other STARTTLS services
SMTP on port 587 and friends start in plaintext and upgrade. -starttls handles that. The docs list smtp, imap, pop3, ftp, ldap, postgres and mysql among the supported protocols:
openssl s_client -connect mail.example.com:587 -starttls smtp -servername mail.example.com </dev/null 2>/dev/null | openssl x509 -noout -dates
Days remaining, not just a date
A date is easy to misread when you’re tired. This prints the number of days left on a GNU/Linux box:
end=$(openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -enddate | cut -d= -f2)
echo $(( ( $(date -d "$end" +%s) - $(date +%s) ) / 86400 )) days left
date -d is a GNU date feature. On macOS you’ll want GNU coreutils or a different approach.
What openssl s_client does not tell you
s_client is a test tool. By default it prints verification errors and carries on, so a connection that succeeds does not mean the certificate is trusted. The docs say as much: it will continue after verification errors unless you add -verify_return_error. For an expired certificate with that flag you’ll see this:
verify error:num=10:certificate has expired
Verify return code: 10 (certificate has expired)
It’s also a one-endpoint-at-a-time tool. It shows what that one server presents right now. It says nothing about the other forty places the same certificate was copied, or the internal ones nobody wrote down. I wrote about why that gap bites in certificate discovery, and the background on why dates matter at all is in understanding certificate expiration and revocation.
Where CertMS fits
OpenSSL is the right tool for “is this one cert okay?” It’s not the right tool for “which of our certificates are about to expire, where do they live, and who’s been told?” That’s the job CertMS does. It monitors URLs, reads certificates from your Windows CA, and pulls certificates from Windows and Linux servers with small agents, then reports and alerts before they expire. It only monitors; it doesn’t sit in the middle of issuance. If you’re comparing doing it yourself with a tool, manual tracking vs. CertMS is the honest side-by-side. Or just take a look at CertMS.