Check a TLS certificate from the terminal: 12 practical contexts
A certificate problem is rarely “the certificate is broken”. It is usually one of five questions:
- What is the leaf certificate? Who issued it, and when does it expire?
- Does it cover the name I asked for? SANs, wildcards and IPs.
- Is the chain complete? The leaf alone is not a chain.
- What does the client think? curl, Java, a browser and an old appliance can disagree.
- What is the context? HTTPS, SNI, a mail server, a database, a local file or a self-signed lab.
These are twelve short, independent checks. They are not a full TLS audit: protocol versions, cipher suites, HSTS and OCSP stapling are separate topics. The point here is to read the certificate and know which question you are answering.
The video series is collected in TLS certificate checks →.
The five questions
A useful order:
- Read the leaf.
- Check the name.
- Check the chain.
- Reproduce the failure from the client that fails.
- Then change the context: SNI, port, STARTTLS, trust store.
If you start by reinstalling the certificate, you are guessing. If you start by reading it, you are debugging.
1. Read the certificate a website sends
This is the baseline check: connect to HTTPS, extract the leaf certificate, and print only the fields that answer the question.
openssl s_client -connect www.example.com:443 \
-servername www.example.com </dev/null 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates
Typical output:
subject=CN = example.com
issuer=C = US, O = SSL Corporation, CN = Cloudflare TLS Issuing ECC CA 3
notBefore=Nov 24 23:48:34 2026 GMT
notAfter=Feb 22 23:53:08 2027 GMT
Two details matter:
-servernamesets SNI explicitly. OpenSSL 1.1.1 and later also send SNI automatically when-connectis given a hostname; against an IP address it cannot, and old OpenSSL versions need the flag. Explicit is portable and makes the intent visible.- Piping to
openssl x509keeps the screen readable. You do not need the whole handshake transcript to answer “who issued this and when does it expire?”.
OpenSSL 3 formats the subject as subject=CN = example.com (with spaces around
=). Older versions may print subject=CN=example.com or an RFC 2253-style
name. Compare the fields, not the exact spacing.
If the command is silent or hangs, make sure stdin is closed (</dev/null) and
that you are connecting to port 443, not an HTTP port.
2. Why one IP can show two certificates
A load balancer or reverse proxy can serve many certificates from one IP. The server chooses by SNI (Server Name Indication). Without SNI it falls back to its default vhost.
openssl s_client -connect 192.0.2.10:443 </dev/null 2>/dev/null \
| openssl x509 -noout -subject
openssl s_client -connect 192.0.2.10:443 \
-servername one.example.test </dev/null 2>/dev/null \
| openssl x509 -noout -subject
The first subject is the default certificate; the second is the certificate for
one.example.test. Neither is necessarily “wrong” — they answer different
questions.
This is also why a one-liner that loops over IP addresses and forgets
-servername "$host" can produce a misleading report. On OpenSSL 1.1.1 and
later, a loop over hostnames sends SNI automatically because -connect receives
the name; the failure mode is connecting to an IP, using -noservername, or
running an old OpenSSL. Setting -servername explicitly is still the clearest
and most portable form when the name is part of the question.
3. Read a certificate file
When the certificate is a file, there is no network and no handshake to blame. Ask for one field at a time:
openssl x509 -in cert.pem -noout -subject -issuer -dates
openssl x509 -in cert.pem -noout -ext subjectAltName
openssl x509 -in cert.pem -noout -ext keyUsage
openssl x509 -in cert.pem -noout -ext extendedKeyUsage
The useful fields:
- subject: the legacy CN and organisation.
- issuer: who signed it.
- notBefore / notAfter: the validity window.
- subjectAltName: the names that actually matter to modern clients.
- keyUsage / extendedKeyUsage: whether the certificate is allowed to be a TLS server certificate.
Avoid dumping the full -text unless the full text is the lesson. It is a wall
of fields; selecting the four lines that answer the question is the whole skill.
-ext, -addext and -starttls postgres need OpenSSL 1.1.1 or later. That is
the same version boundary as automatic SNI; if you support older systems, check
openssl version before assuming the command exists.
4. Does the certificate cover this hostname or IP?
The certificate authority should have put the names in SAN (subject alternative name), not only in the CN. OpenSSL can check the certificate directly:
openssl x509 -in cert.pem -noout -checkhost www.example.com
openssl x509 -in cert.pem -noout -checkhost api.example.com
openssl x509 -in cert.pem -noout -checkhost wrong.example.org
openssl x509 -in cert.pem -noout -checkip 192.0.2.10
Output is explicit:
Hostname www.example.com does match certificate
Hostname api.example.com does match certificate
Hostname wrong.example.org does NOT match certificate
IP 192.0.2.10 does match certificate
Two caveats:
- Read the output, not only the exit code. Some OpenSSL builds return
0even for “does NOT match”; the sentence is the reliable result. - Wildcards are one label deep.
*.example.comcoversapi.example.com, notapi.eu.example.com.
Modern browsers require SAN. Some OpenSSL/curl combinations still fall back to CN when there is no SAN, but that compatibility is not a promise. Put the names in SAN.
5. Verify the chain, not just the leaf
A server can send one certificate or a chain. The leaf is the certificate for the name; the intermediate is the signer that connects it to a trusted root. A browser may fetch a missing intermediate automatically. curl, Java and many devices will not.
Count what the server actually sends:
openssl s_client -connect www.example.com:443 -servername www.example.com \
-showcerts </dev/null 2>/dev/null \
| grep -c -- 'BEGIN CERTIFICATE'
Then verify the chain explicitly:
openssl verify -CAfile root.pem cert.pem
openssl verify -CAfile root.pem -untrusted intermediate.pem cert.pem
The first command fails with a leaf-only chain (the printed subject line comes
from openssl verify; the exact formatting varies by version):
CN = example.com
error 20 at 0 depth lookup: unable to get local issuer certificate
error cert.pem: verification failed
The second succeeds:
cert.pem: OK
That difference is the whole episode: the leaf is valid, but the chain is incomplete.
6. Read curl’s certificate error instead of guessing
curl gives you the error and an exit code. You can ask for a compact summary.
-w writes to stdout, so the summary survives 2>/dev/null. -s suppresses
the progress meter; -S is what makes curl write its own verbose error to
stderr, which is exactly what 2>/dev/null hides:
curl -sS -o /dev/null --max-time 10 \
-w 'wrong: %{exitcode} %{errormsg}\n' \
https://wrong.host.badssl.com/ 2>/dev/null
curl -sS -o /dev/null --max-time 10 \
-w 'self: %{exitcode} %{errormsg}\n' \
https://self-signed.badssl.com/ 2>/dev/null
curl -sS -o /dev/null --max-time 10 \
-w 'expired: %{exitcode} %{errormsg}\n' \
https://expired.badssl.com/ 2>/dev/null
Three different failures:
wrong: 60 SSL: no alternative certificate subject name matches target hostname 'wrong.host.badssl.com'
self: 60 SSL certificate problem: self-signed certificate
expired: 60 SSL certificate problem: certificate has expired
Older curl versions wrote “host name” as two words; newer ones write
“hostname”. The error code alone is not enough; the message names the cause.
Do not “fix” a hostname mismatch by adding -k. That disables the check you are
trying to debug.
The badssl.com endpoints are a convenience, not a permanent fixture. Check them
immediately before recording: if wrong.host.badssl.com’s own certificate has
expired, curl reports “certificate has expired” instead of the hostname
mismatch you wanted to demonstrate.
7. Test another IP without changing DNS
A load balancer, CDN or blue/green deployment can make the public DNS answer
differ from the origin you want to inspect. curl --resolve sends the request
to a chosen IP while keeping the hostname and SNI:
curl -sS -vI --cacert root.pem \
--resolve app.example.test:443:203.0.113.10 \
https://app.example.test/
Read the lines that matter. Connected is printed before the handshake; the
response headers use the < prefix in verbose mode:
* Connected to app.example.test (203.0.113.10) port 443
* subject: CN=app.example.test
* issuer: CN=TLS Cert Lab Intermediate
* SSL certificate verify ok.
* using HTTP/1.x
> HEAD / HTTP/1.1
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
--resolve changes only the address lookup. The URL still supplies the Host
header and the SNI name, which is exactly what you want when comparing two
origins. If ALPN negotiates HTTP/2, the status line will be HTTP/2 200
instead of HTTP/1.1 200; the certificate lines above it are unchanged.
The order of the verbose lines varies by curl version: curl 8.5 prints
Connected before the certificate lines, while older curl builds may print the
certificate first. The fields are the same; do not treat the line order as part
of the TLS result.
8. Check a mail server: STARTTLS and direct TLS
Mail is not HTTP. SMTP usually starts in plaintext and upgrades with
STARTTLS; IMAP can upgrade on 143 or use TLS directly on 993. Those are two
different modes, so show both clearly.
printf 'SMTP STARTTLS: '; echo | openssl s_client \
-starttls smtp -connect smtp.gmail.com:587 \
-servername smtp.gmail.com 2>/dev/null \
| openssl x509 -noout -subject -dates
printf 'IMAP STARTTLS: '; echo | openssl s_client \
-starttls imap -connect imap.example.com:143 \
-servername imap.example.com 2>/dev/null \
| openssl x509 -noout -subject -dates
printf 'IMAP direct TLS: '; echo | openssl s_client \
-connect imap.gmail.com:993 \
-servername imap.gmail.com 2>/dev/null \
| openssl x509 -noout -subject -dates
The certificate check does not log in. It only proves which certificate the mail service serves. Gmail’s IMAP on 993 is a convenient direct-TLS example; for an IMAP STARTTLS example, use a mail server you control on 143. Port 143 may be blocked or the server may not offer STARTTLS, so test it before putting it in a recording.
9. Check a database certificate with STARTTLS
MySQL and PostgreSQL also have protocol-specific handshakes before TLS. OpenSSL knows how to start them:
printf 'PostgreSQL: '; echo | openssl s_client \
-starttls postgres -connect db.example.test:5432 2>/dev/null \
| openssl x509 -noout -subject -dates
printf 'MySQL: '; echo | openssl s_client \
-starttls mysql -connect db.example.test:3306 2>/dev/null \
| openssl x509 -noout -subject -dates
The subject and dates are the same fields as HTTPS. The context is different: the database chooses its certificate from its own configuration, and SNI may not be part of the protocol. Do not assume a hostname-based virtual host model applies here.
10. Turn expiry into an exit code
A date in a terminal is not monitoring. -checkend takes a number of seconds
and returns an exit status:
openssl x509 -in short.pem -noout -checkend 2592000
printf 'short exit=%s\n' $?
openssl x509 -in long.pem -noout -checkend 2592000
printf 'long exit=%s\n' $?
2592000 is 30 days. Typical output:
Certificate will expire
short exit=1
Certificate will not expire
long exit=0
That is enough for cron or a monitoring probe. The detail that matters is the
exit status: 1 means “inside the window”. Read the command’s status
immediately; another command will overwrite $?.
11. Does the certificate match the private key?
After a renewal or deployment, a certificate and key can be individually valid and still not belong together. Compare the public keys, not the private keys:
openssl x509 -in cert.pem -pubkey -noout \
| openssl pkey -pubin -outform DER | sha256sum
openssl pkey -in key.pem -pubout -outform DER | sha256sum
Matching hashes mean the pair belongs together. Do the same for an ECDSA pair; the commands do not change:
openssl x509 -in ec-cert.pem -pubkey -noout \
| openssl pkey -pubin -outform DER | sha256sum
openssl pkey -in ec-key.pem -pubout -outform DER | sha256sum
The old openssl x509 -modulus | md5sum trick works for RSA, but it is not
universal and it uses MD5. The public-key/hash comparison is clearer and covers
both RSA and ECDSA.
12. A self-signed certificate with a SAN
Self-signed certificates are a good lab tool and a common production mistake. Modern clients care about the SAN; a certificate with a SAN for the wrong name is still a hostname failure even if the client is told to trust it.
Create two disposable certificates and trust them explicitly:
# wrong name: served at self.example.test, valid for other.example.test
openssl req -x509 -newkey rsa:2048 -nodes \
-keyout bad.key -out bad.pem -days 30 \
-subj '/CN=self.example.test' \
-addext 'subjectAltName=DNS:other.example.test'
# right name
openssl req -x509 -newkey rsa:2048 -nodes \
-keyout self.key -out self.pem -days 30 \
-subj '/CN=self.example.test' \
-addext 'subjectAltName=DNS:self.example.test'
Serve the wrong-SAN certificate on a local TLS server in one terminal:
openssl s_server -accept 8443 -cert bad.pem -key bad.key -www
Then point the hostname at localhost in another terminal. The certificate is trusted explicitly, but the name still does not match:
curl -sS -o /dev/null --cacert bad.pem \
--resolve self.example.test:8443:127.0.0.1 \
-w 'wrong SAN: %{exitcode} %{errormsg}\n' \
https://self.example.test:8443/
Stop that server and start it again with the matching certificate:
openssl s_server -accept 8443 -cert self.pem -key self.key -www
Now the same request succeeds:
curl -sS -o /dev/null --cacert self.pem \
--resolve self.example.test:8443:127.0.0.1 \
-w 'right SAN: %{exitcode} %{errormsg}\n' \
https://self.example.test:8443/
A CN-only certificate is not a safe shortcut. Some OpenSSL/curl versions still fall back to CN, but browsers and many other clients do not. Put the name in SAN and test with the client that actually fails.
What to remember
- SNI selects the certificate. Against an IP, with
-noservername, or on an old OpenSSL, you may be reading the default vhost; set-servernameexplicitly when the name is the question. - SAN is the name. CN is legacy; use SAN and check with the client.
- Leaf ≠ chain. A valid leaf with a missing intermediate still fails.
- The client matters. curl, Java, a browser and a database do not share one trust store.
- StartTLS is still TLS. The certificate fields are the same; the handshake before them is not.
- Do not use
-kto make a check pass. It disables the question. - Expiry is an exit code. Put it in monitoring, not in a wiki page.
The series is built as twelve standalone checks. Pick the one that matches the failure in front of you; you should not need to watch the others first.
Series map
- Check a website’s TLS certificate with
openssl s_client - Why
opensslshows the wrong certificate (SNI) - Read a certificate file: subject, SANs, dates, key usage
- Does this certificate cover this hostname or IP?
- Verify a certificate chain and find the missing intermediate
- Read curl’s certificate error instead of guessing
- Test a certificate on another IP without changing DNS
- Check a mail server’s certificate: STARTTLS and direct TLS
- Check a database certificate with STARTTLS
- Alert when a certificate expires in 30 days
- Does the certificate match the private key?
- Create a self-signed certificate with a SAN and see why curl rejects the wrong name
The whole series is also a playlist: TLS certificate checks →.