You can check SSL certificate expiry for any website from a terminal with one openssl command. It needs no login to the server and no browser. This guide shows that command, a shorter one for scripts, a small script that checks many sites, and how to make sure a certificate never runs out without anyone noticing.

The commands were run in our lab on Ubuntu 24.04 with OpenSSL 3 in November 2026, against a test web server with a 20-day certificate. The outputs are real.

The short answer

echo | openssl s_client -connect yourcompany.com:443 -servername yourcompany.com 2>/dev/null | openssl x509 -noout -dates

The line that starts with notAfter= is the expiry date. Replace the domain in both places.

Check SSL certificate expiry on a live site

The same command with the subject and issuer added:

echo | openssl s_client -connect lab.example.test:443 -servername lab.example.test 2>/dev/null | openssl x509 -noout -subject -issuer -dates
subject=CN = lab.example.test
issuer=CN = lab.example.test
notBefore=Nov 13 16:06:17 2026 GMT
notAfter=Dec  3 16:06:17 2026 GMT

What each part does:

  • openssl s_client -connect host:443 opens a TLS connection and prints the certificate the server sends.
  • -servername sends the host name (SNI). Leave it out and a server that hosts several sites may hand you the wrong certificate.
  • echo | closes the connection straight away so the command does not wait for input.
  • openssl x509 -noout -dates reads the certificate and prints only its two dates.

Dates are in GMT. For India, add five and a half hours.

Other ports work the same way. For a mail server that uses STARTTLS:

echo | openssl s_client -connect mail.yourcompany.com:587 -starttls smtp 2>/dev/null | openssl x509 -noout -dates

A yes or no answer for scripts

-checkend takes a number of seconds and tells you whether the certificate will still be valid after that time. 2592000 seconds is 30 days, 604800 is 7 days.

echo | openssl s_client -connect lab.example.test:443 -servername lab.example.test 2>/dev/null | openssl x509 -noout -checkend 2592000
echo "exit code: $?"
Certificate will expire
exit code: 1

With 7 days in place of 30:

Certificate will not expire
exit code: 0

Our lab certificate had 19 days left, so it fails the 30-day test and passes the 7-day one. The exit code is what a monitoring script should read.

The same check with curl

curl -vI https://yourcompany.com 2>&1 | grep -E 'start date|expire date|subject:|issuer:'
*  subject: CN=lab.example.test
*  start date: Nov 13 16:06:17 2026 GMT
*  expire date: Dec  3 16:06:17 2026 GMT
*  issuer: CN=lab.example.test

Check a certificate file on the server

If you are logged in to the server, read the file itself:

sudo openssl x509 -in /etc/letsencrypt/live/yourcompany.com/fullchain.pem -noout -enddate

The file on disk and the certificate the web server presents can differ. A renewed file does nothing until the web server reloads, so always confirm with the live check as well.

A script for many sites

Save this as certdays.sh and make it executable with chmod +x certdays.sh.

#!/bin/bash
# usage: ./certdays.sh host1 host2 ...
for host in "$@"; do
  end=$(echo | openssl s_client -connect "$host:443" -servername "$host" 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2)
  if [ -z "$end" ]; then echo "$host: no certificate read"; continue; fi
  days=$(( ( $(date -d "$end" +%s) - $(date +%s) ) / 86400 ))
  echo "$host: $days days left (expires $end)"
done
./certdays.sh lab.example.test 192.0.2.1
lab.example.test: 19 days left (expires Dec  3 16:06:17 2026 GMT)
192.0.2.1: no certificate read

The second address has no web server, to show what a failed lookup prints. Put the script in cron once a day and have it mail you when a number drops below 20. It uses GNU date, so it runs on Linux as written and needs a small change on macOS.

Why certificates expire sooner than they used to

Browser makers and certificate authorities agreed in April 2025 to shorten certificate lifetimes in steps. A certificate issued today can be valid for 200 days at most.

Issued on or afterLongest validity
15 March 2026200 days
15 March 2027100 days
15 March 202947 days

A certificate bought “for a year” is now reissued during that year, and from 2029 it will be replaced about every six weeks. Renewing by hand does not scale to that, so renewal has to be automatic.

Stop certificates expiring

  1. Use automatic renewal. With Let’s Encrypt and Certbot, sudo certbot renew --dry-run tests the renewal without changing anything, and systemctl list-timers | grep certbot shows whether the timer is active. These two lines are from the Certbot documentation and were not run in our lab.
  2. Make sure the web server reloads after a renewal. Certbot’s --deploy-hook "systemctl reload nginx" does that.
  3. Monitor from outside. The script above, run from a different machine, catches the cases automation misses: a failed renewal, a server that did not reload, a certificate on a load balancer that nobody remembered.
  4. Keep a list of every certificate you own, including mail servers, VPN gateways and admin panels on unusual ports. The forgotten ones are the ones that expire.

When a certificate does expire, browsers show a full-page warning, apps and payment callbacks that connect to your server start failing, and some customers will not come back.

Free tool: our website safety check tests your certificate, HTTPS redirect and security headers from the outside and grades the site in about 30 seconds. Of the first 10 sites checked with it, 2 failed the certificate test.

Related guides

Sources

Need a full check of what your servers expose? Our VAPT services cover TLS settings, exposed services and web application testing. Contact us for a quote.

Post author avatar

Bhaskar Soni

Bhaskar Soni is a Security Engineer and the founder of Youngster Company, based in Ahmedabad, Gujarat. He works hands-on with Linux server hardening, vulnerability assessment and penetration testing (VAPT), ISO 27001 compliance and OSINT investigations. He writes practical, tested guides for IT professionals and Indian businesses, and every article on this site is written or reviewed by him.

Leave a Reply