# Inspect a TLS certificate with OpenSSL

> openssl s_client connects to a TLS server and prints its certificate, and openssl x509 decodes that certificate into readable fields.

URL: https://levelrail.com/learn/domains-https/inspect-a-tls-certificate-with-openssl/
Updated: 2026-11-27

You moved a site to a new server and the browser says the certificate is wrong, but you cannot tell which certificate the server is actually sending. OpenSSL lets you ask the server directly and read the answer in plain text, without a browser hiding the details.

## How it works

When a client connects on port 443, it starts a TLS handshake. In the first message it can include the hostname it wants, called SNI (Server Name Indication). A server that hosts many sites uses that name to choose which certificate to send. If the client sends no name, the server falls back to a default certificate, which is often for a different site.

The server sends a chain: the leaf certificate for your domain, then one or more intermediate certificates. The client checks that the chain leads to a root it already trusts, that today's date is between `notBefore` and `notAfter`, and that the hostname appears in the certificate's `subjectAltName` list. The old `CN` field is not what browsers rely on.

Two commands split the work. `openssl s_client` plays the client and prints what the server sent. `openssl x509` reads one certificate and prints chosen fields. You pipe the first into the second.

## Step 1: Fetch the certificate

```bash
openssl s_client -connect example.com:443 -servername example.com </dev/null
```

`-connect` is host and port. `-servername` sends the SNI name. `</dev/null` closes standard input, because s_client otherwise waits for you to type. If you prefer, `echo |` before the command does the same.

What you should see: a block of connection details, the certificate chain, a PEM block starting with `-----BEGIN CERTIFICATE-----`, and a line about the verification result near the end.

## Step 2: Read the key fields

Pipe the output into `openssl x509`:

```bash
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -issuer -dates
```

`2>/dev/null` hides progress messages. `-noout` stops x509 from printing the certificate again.

What you should see: four lines, `subject=`, `issuer=`, `notBefore=` and `notAfter=`, each with a value.

## Step 3: List the covered names

```bash
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -ext subjectAltName
```

The `-ext` option needs OpenSSL 1.1.1 or newer. Browsers match your hostname against this list.

What you should see: a `X509v3 Subject Alternative Name:` heading followed by entries such as `DNS:example.com, DNS:www.example.com`. A wildcard looks like `DNS:*.example.com` and covers one label only, so `a.b.example.com` is not covered.

## Step 4: Read a local file

```bash
openssl x509 -in cert.pem -noout -text
```

This shows every field of a certificate you saved on disk, including the public key type and extensions.

What you should see: a long block starting with `Certificate:` and `Data:`, with Issuer, Validity and Subject sections.

## Step 5: Check expiry in a script

```bash
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -checkend 604800
```

`-checkend` takes a number of seconds (604800 is seven days). It prints `Certificate will not expire` and exits with 0 if the certificate is still valid after that time, or prints `Certificate will expire` and exits with 1. That makes it easy to use in a cron job that alerts you before renewal is missed.

What you should see: one of those two sentences. Check the exit code with `echo $?`.

## Check it worked

The issuer should be the authority you expect, `notAfter` should be in the future, and your hostname should appear under `subjectAltName` (or match a wildcard entry). In the full Step 1 output, the line `Verification: OK` (or `Verify return code: 0 (ok)` on older versions) means the chain was trusted.

## Edge cases and gotchas

- **You get the default certificate.** Without `-servername`, or when you connect to a bare IP address, the server cannot pick the right site. Always pass the real hostname.
- **s_client hangs.** It waits for input after the handshake. Use `</dev/null` or pipe `echo |` into it.
- **A CDN or proxy answers instead of your server.** You see the proxy's certificate. To test your origin, connect to its IP, for example `-connect 203.0.113.10:443 -servername example.com`.
- **IPv6 and IPv4 serve different things.** The name may resolve to both. Test each with `-4` or `-6` on s_client, since a broken IPv6 listener can show an old certificate.
- **A wrong clock breaks everything.** If the server or your machine has drifted clock time, a good certificate looks expired or not yet valid. Check `date` first.
- **The chain is incomplete.** Browsers sometimes fetch missing intermediates, but curl and many apps do not. Configure the full chain file (`fullchain.pem`), and not only the leaf.
- **Older OpenSSL builds.** `-ext` is missing before 1.1.1. Use `-text` and look for the Subject Alternative Name section instead.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Certificate is for a different site | No SNI sent | Add `-servername example.com` |
| `verify error:num=20:unable to get local issuer certificate` | Server did not send the intermediate certificate | Serve the full chain (`fullchain.pem`) |
| `verify error:num=10:certificate has expired` | `notAfter` is in the past | Renew the certificate and reload the server |
| `connect:errno=111` or `Connection refused` | Nothing listens on port 443 | Start the web server and check it with `ss -tlnp` |
| Command hangs with no output | Firewall drops port 443 | Open 443 in the firewall and any cloud security group |
| Name not in subjectAltName | Certificate was issued for other names | Request a certificate that lists the name |
| Output stops, then waits | Input is open | Add `</dev/null` |
| `verify error:num=18:self-signed certificate` | Server presents a self-signed certificate | Fine for a lab. In production install a certificate from a [certificate authority](https://levelrail.com/learn/glossary/#certificate-authority)) |
| `verify error:num=21:unable to verify the first certificate` | Chain is incomplete for the client | Serve `fullchain.pem` so the intermediate is included |
| `handshake failure` or `no peer certificate available` | Server speaks TLS only for certain names or protocols | Pass `-servername`, and check the server's TLS settings |

## Real-world scenario

You moved `example.com` to a new server on 203.0.113.10. The browser warns about a certificate for another domain, and you are not sure whether the problem is DNS, nginx or the certificate itself.

First you ask the server directly, bypassing DNS:

```bash
openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
```

The output shows a certificate for `old-site.example.net`, issued last year. So DNS is fine, the server is answering, but nginx picked the wrong certificate. You open the server block for `example.com` and find `ssl_certificate` still points at the old path. After pointing it at the new `fullchain.pem`, `sudo nginx -t` passes, you reload, and the same command now lists `DNS:example.com` and a `notAfter` date months away. The browser warning is gone after a refresh.

The useful habit is to separate the layers: name resolution, which certificate is served, and whether the chain verifies. `openssl` answers the second and third without a browser getting in the way. See [how HTTPS and Let's Encrypt work](https://levelrail.com/learn/domains-https/https-and-lets-encrypt/)) for how the certificate got there, and the [guide to verifying a deploy](https://levelrail.com/blog/zero-downtime-deploy-docker-rollback/)) for rollout checks.

## Quick reference

```bash
# Show the chain and verification result
openssl s_client -connect HOST:443 -servername HOST </dev/null
# Subject, issuer and dates
... | openssl x509 -noout -subject -issuer -dates
# Covered names (OpenSSL 1.1.1 or newer)
... | openssl x509 -noout -ext subjectAltName
# Fails (exit 1) if it expires within 7 days
... | openssl x509 -noout -checkend 604800
# Test one address without DNS
openssl s_client -connect 203.0.113.10:443 -servername HOST </dev/null
# Force IPv4 or IPv6
openssl s_client -4 -connect HOST:443 -servername HOST </dev/null
```

## Related lessons

- [HTTPS and Let's Encrypt](https://levelrail.com/learn/domains-https/https-and-lets-encrypt/)): how certificates are issued and trusted.
- [Check DNS records with dig](https://levelrail.com/learn/domains-https/check-dns-with-dig/)): confirm the name points where you think.
- [Renew Let's Encrypt certificates](https://levelrail.com/learn/domains-https/renew-lets-encrypt-certificates/)): keep the dates you just read from running out.

## Next

Now that you can read a certificate, learn how the DNS side is checked in [Check DNS records with dig](https://levelrail.com/learn/domains-https/check-dns-with-dig/)).

## Frequently asked questions

### Why do I need -servername?

One server can hold certificates for many domains. -servername sends the name (SNI) so the server picks the right one.

### What does </dev/null do?

It closes the input at once, so s_client prints the certificate and exits instead of waiting for you to type.

### The browser shows a valid padlock but openssl shows a different certificate. Which is right?

Probably both. Without -servername, or when you connect by IP address, the server may hand back its default certificate. Add -servername with the real hostname to see what a browser sees.

### Can I inspect a site that is behind a CDN or proxy?

Yes, but you see the certificate the proxy presents, not the one on your origin server. To test the origin, connect to its own IP and still pass -servername with the real hostname.


## Sources

- [OpenSSL docs, s_client](https://docs.openssl.org/master/man1/openssl-s_client/)
