Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 20Security · TLS

How to check when a TLS certificate expires

An expired certificate takes a site, an API or a whole cluster down — and it always happens on a weekend. Let's Encrypt certificates last only 90 days (and will get shorter), and Let's Encrypt no longer sends expiry emails, so you must watch the dates. This how-to checks a live site, a certificate file, the whole chain, many hosts at once, and shows how to turn it into an alert.

  • 5 Lessen
  • 7 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

20.1

The quick answer

echo | openssl s_client -connect shop.example.com:443 -servername shop.example.com 2>/dev/null | openssl x509 -noout -subject -issuer -enddate
subject=CN=shop.example.com
issuer=C=US, O=Let's Encrypt, CN=R12
notAfter=Feb 10 08:14:22 2027 GMT
openssl x509 -in /etc/ssl/certs/shop.crt -noout -enddate
certbot certificates

20.2

Step by step

  1. A live site — always add -servername (SNI), or you may get the wrong (default) certificate:
echo | openssl s_client -connect shop.example.com:443 -servername shop.example.com 2>/dev/null | openssl x509 -noout -dates -ext subjectAltName

-ext subjectAltName shows all names the certificate is valid for.

  1. Will it expire soon? -checkend gives an exit code — perfect for scripts and monitoring (2592000 seconds = 30 days):
openssl x509 -in shop.crt -noout -checkend 2592000 && echo "OK for 30+ days" || echo "EXPIRES within 30 days"
  1. Other ports and protocols (mail, databases):
echo | openssl s_client -connect mail.example.com:465 2>/dev/null | openssl x509 -noout -enddate
echo | openssl s_client -connect mail.example.com:25 -starttls smtp 2>/dev/null | openssl x509 -noout -enddate
  1. The whole chain — a missing intermediate certificate breaks some clients even when the server certificate is fine:
echo | openssl s_client -connect shop.example.com:443 -servername shop.example.com -showcerts 2>/dev/null | grep -E ' s:| i:'
echo | openssl s_client -connect shop.example.com:443 -servername shop.example.com 2>/dev/null | grep 'Verify return code'
Verify return code: 0 (ok)
  1. Many hosts at once — a small script for a daily report:
/usr/local/bin/cert-report.sh
#!/bin/bash
for h in shop.example.com api.example.com mail.example.com; do
  end=$(echo | openssl s_client -connect "$h:443" -servername "$h" 2>/dev/null | openssl x509 -noout -enddate | cut -d= -f2)
  days=$(( ( $(date -d "$end" +%s) - $(date +%s) ) / 86400 ))
  echo "$days days  $h  ($end)"
done | sort -n
  1. Kubernetes with cert-manager:
kubectl get certificate -A
kubectl get secret shop-tls -n shop -o jsonpath='{.data.tls\.crt}' | base64 -d | openssl x509 -noout -enddate

20.3

Check that it worked

After a renewal, check the live site again (not only the file): the notAfter date is new. If the file is new but the site shows the old date, the web server was not reloaded (systemctl reload nginx).

20.4

Common problems

ProblemCauseFix
You see the wrong certificateNo -servername (SNI) — you got the default site's certificate.Always add -servername HOST.
File renewed, site still shows the old dateThe web server keeps the old certificate in memory.Reload it; add a deploy hook to certbot.
Verify return code: 20 (unable to get local issuer certificate)The server does not send the intermediate certificate.Use the full chain file (fullchain.pem), not only cert.pem.
Verify return code: 10 (certificate has expired)Expired.Renew now (certbot renew --force-renewal for one cert with --cert-name), then reload.
The script hangss_client waits for input.Start it with echo | or </dev/null.
date: invalid date in the scriptNot GNU date (macOS).Run it on Linux, or use -checkend.

20.5

Go further

1Why must you add -servername to openssl s_client?

Many sites share one IP; without SNI the server may send the certificate of another (default) site.

2Which openssl option tells a script "this certificate expires within 30 days"?

openssl x509 -checkend 2592000 (exit code 1 if it expires in that time).

Alle commando's in dit hoofdstuk