How-Tos

How-To: Check When Your Sites’ TLS Certificates Expire With openssl

Leave a comment

Auto-renewal is great until it quietly stops. I like a dumb, independent check that looks at the certificate a site actually serves, not what the renewal tool thinks it did. Replace example.com with your own names.

1. Check one site

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
  | openssl x509 -noout -subject -issuer -enddate

-servername matters. Without SNI you may get the server’s default certificate instead of the one for your site.

2. Fail if it expires within 14 days

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null \
  | openssl x509 -noout -checkend $((14*24*3600)) && echo OK || echo "RENEW SOON"

3. Loop over a list

while read -r h; do
  printf '%-30s ' "$h"
  echo | timeout 10 openssl s_client -connect "$h:443" -servername "$h" 2>/dev/null \
    | openssl x509 -noout -enddate 2>/dev/null || echo "NO CERT / UNREACHABLE"
done < sites.txt

One name per line in sites.txt. Don’t forget the www names and anything that isn’t on port 443, like mail on 465 or 993.

4. Compare with what’s on disk

sudo certbot certificates

If disk says renewed and the site still serves the old cert, your web server never reloaded.

Put the loop on a timer and have it only speak up when something says RENEW SOON. If you manage servers with Ansible, run it from there so every box gets checked the same way.

God Bless, and Happy hacking!

References & Further Reading

Subscribe (RSS) Follow How-Tos via RSS

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.