# OpenSSL 查看证书内容：x509 常用命令

> OpenSSL 3.x 查看证书的命令：完整内容、主题、有效期、指纹、SAN、到期检查、远程证书、DER 文件、证书链、OCSP/CRL 地址和 SCT。

Source: https://ssl.sh/zh/cookbook/inspect-certificates/
Last updated: 2026-11-10

`openssl x509` 用来读取证书，想看什么就加对应参数：`-text` 看全部，`-subject`、`-enddate`、`-fingerprint`、`-ext subjectAltName` 看单项。每条查看命令都加上 `-noout`，否则 OpenSSL 会把 PEM 原文再打印一遍。下面的示例读取的是 2026 年 11 月 example.com 实际返回的证书和一条本地测试链，环境为 OpenSSL 3.6.1。

手上只有域名时，从下文“查看远程服务器的证书”开始；想确认证书链是否有效而不只是读内容，见[验证证书链](https://ssl.sh/zh/cookbook/verify-chains/)。

## 打印证书全部内容

`-text -noout` 把所有字段解码成可读文本。

```bash
openssl x509 -in example.com.crt -noout -text
```

```text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            01:ee:e6:aa:bb:52:1d:5e:14:fc:31:5f:d9:98:56:90
        Signature Algorithm: ecdsa-with-SHA256
        Issuer: C=US, O=SSL Corporation, CN=Cloudflare TLS Issuing ECC CA 3
        Validity
            Not Before: Sep 26 22:49:11 2026 GMT
            Not After : Dec 25 22:56:35 2026 GMT
        Subject: CN=example.com
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
        X509v3 extensions:
            X509v3 Basic Constraints: critical
                CA:FALSE
            Authority Information Access: 
                CA Issuers - URI:http://i.cf-i.ssl.com/Cloudflare-TLS-I-E3.cer
                OCSP - URI:http://o.cf-i.ssl.com
            X509v3 Subject Alternative Name: 
                DNS:example.com, DNS:*.example.com
            X509v3 Certificate Policies: 
                Policy: 2.23.140.1.2.1
            ...
```

策略 OID `2.23.140.1.2.1` 表示 DV 证书，`2.23.140.1.2.2` 是 OV，`2.23.140.1.1` 是 EV，区别见 [DV、OV、EV](https://ssl.sh/zh/learn/dv-ov-ev/)。阿里云、腾讯云下载的证书包里 Nginx 目录下的 `.pem`/`.crt` 文件都可以直接这样查看。也可以用[证书解析工具](https://ssl.sh/zh/tools/certificate-decoder/)在浏览器里看。

## 只看主题、颁发者、有效期和序列号

点名要哪几个字段，比翻完整输出快得多。

```bash
openssl x509 -in example.com.crt -noout -subject -issuer -dates -serial
```

```text
subject=CN=example.com
issuer=C=US, O=SSL Corporation, CN=Cloudflare TLS Issuing ECC CA 3
notBefore=Sep 26 22:49:11 2026 GMT
notAfter=Dec 25 22:56:35 2026 GMT
serial=01EEE6AABB521D5E14FC315FD9985690
```

只要到期时间就用 `-enddate`。时间一律是 GMT，换成北京时间加 8 小时。吊销证书时 CA 要的就是这个序列号，CRL 里列的也是它。某些工具要求反序逗号格式时加 `-nameopt RFC2253`。

## 查看证书指纹

指纹是整张 DER 证书的哈希，能唯一标识一张证书。

```bash
openssl x509 -in example.com.crt -noout -fingerprint -sha256
```

```text
sha256 Fingerprint=85:CA:6A:B0:68:E9:BC:CE:88:B6:C4:AA:3C:47:F7:D1:72:28:13:4A:45:7F:87:0D:38:00:E6:22:3A:0D:F0:7A
```

新场景一律用 SHA-256；Windows 证书查看器里的“指纹”是 SHA-1，对照时要用 `-sha1`。每次续期指纹都会变，如果一定要做固定（pinning），固定公钥而不是证书指纹。

## 查看 SAN 和其他扩展

`-ext`（1.1.1 起）按名字打印指定扩展，多个用逗号分隔。

```bash
openssl x509 -in example.com.crt -noout -ext subjectAltName,keyUsage,extendedKeyUsage
```

```text
X509v3 Subject Alternative Name: 
    DNS:example.com, DNS:*.example.com
X509v3 Extended Key Usage: 
    TLS Web Server Authentication
X509v3 Key Usage: critical
    Digital Signature
```

其他常用名字：`basicConstraints`、`authorityInfoAccess`、`crlDistributionPoints`、`certificatePolicies`、`ct_precert_scts`。EKU 里如果还有 `TLS Web Client Authentication`，做 [mTLS](https://ssl.sh/zh/learn/mtls/) 时要注意，Chrome 根证书计划正在把 clientAuth 从公网 TLS 体系中剔除：2026 年 6 月 15 日起开始淘汰混用用途的证书体系，2027 年 3 月 15 日起签发的证书只能声明 serverAuth。

## 检查域名或 IP 是否匹配

`-checkhost` 和 `-checkip` 用和 TLS 客户端相同的规则做匹配。

```bash
openssl x509 -in example.com.crt -noout -checkhost www.example.com
openssl x509 -in example.com.crt -noout -checkhost a.b.example.com
openssl x509 -in selfsigned.crt -noout -checkip 203.0.113.10
```

```text
Hostname www.example.com does match certificate
Hostname a.b.example.com does NOT match certificate
IP 203.0.113.10 does match certificate
```

排查 [NET::ERR_CERT_COMMON_NAME_INVALID](https://ssl.sh/zh/errors/net-err-cert-common-name-invalid/) 时很快就能定位：通配符只覆盖一级子域名，IP 只和 IP 类型的 SAN 比对。

## 检查证书是否快到期

`-checkend N` 判断 N 秒后证书是否仍然有效，并通过退出码返回结果。

```bash
openssl x509 -in example.com.crt -noout -checkend 2592000; echo "exit=$?"   # 30 天
openssl x509 -in example.com.crt -noout -checkend 8640000; echo "exit=$?"   # 100 天
```

```text
Certificate will not expire
exit=0
Certificate will expire
exit=1
```

退出码 0 表示到那时仍有效，1 表示届时已过期。公网证书最长有效期 2026 年 3 月 15 日起降到 200 天，2029 年降到 47 天（见[有效期时间线](https://ssl.sh/zh/timeline/certificate-validity/)），在 cron 里加一条 `-checkend` 告警，配合[自动续期](https://ssl.sh/zh/learn/automate-certificate-renewal/)成本很低。

## 查看远程服务器的证书

把 `s_client` 的输出交给 `x509`；`-servername` 发送 SNI，共享主机上才能拿到正确的证书。

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

```text
subject=CN=example.com
issuer=C=US, O=SSL Corporation, CN=Cloudflare TLS Issuing ECC CA 3
notBefore=Sep 26 22:49:11 2026 GMT
notAfter=Dec 25 22:56:35 2026 GMT
```

前面的 `echo |` 让连接立即关闭，不会卡住等输入。网站套了 CDN 或负载均衡、想直接看源站证书时，连源站 IP（`-connect 203.0.113.10:443`），`-servername` 仍写域名。更多连接参数见[测试 TLS 连接](https://ssl.sh/zh/cookbook/test-connections/)。

## 读取 DER 格式证书

二进制的 `.cer`、`.der` 文件在 OpenSSL 1.1.1 上要加 `-inform DER`，3.x 一般能自动识别。

```bash
openssl x509 -in example.com.der -inform DER -noout -subject
file example.com.der example.com.crt
```

```text
subject=CN=example.com
example.com.der: Certificate, Version=3
example.com.crt: PEM certificate
```

报 `Could not read certificate` 或 `no start line`，多半是文件其实是 DER、PKCS#7 或 PKCS#12 而不是 PEM。IIS 用的 `.pfx`、Tomcat 用的 `.jks` 都不能直接用 x509 读，识别和转换方法见[证书格式转换](https://ssl.sh/zh/cookbook/convert-formats/)。

## 列出证书链文件里的每张证书

`x509` 读完第一张就停，证书包要借 `pkcs7 -print_certs` 一次列出。

```bash
openssl crl2pkcs7 -nocrl -certfile fullchain.pem | openssl pkcs7 -print_certs -noout
```

```text
subject=CN=example.com
issuer=C=US, O=SSL Corporation, CN=Cloudflare TLS Issuing ECC CA 3

subject=C=US, O=SSL Corporation, CN=Cloudflare TLS Issuing ECC CA 3
issuer=C=US, O=SSL Corporation, CN=SSL.com TLS Transit ECC CA R2

subject=C=US, O=SSL Corporation, CN=SSL.com TLS Transit ECC CA R2
issuer=C=US, O=SSL Corporation, CN=SSL.com TLS ECC Root CA 2022
```

每张证书的 issuer 应该等于下一张的 subject，对不上就是[缺中间证书](https://ssl.sh/zh/errors/missing-intermediate-certificate/)。`openssl storeutl -noout -certs fullchain.pem`（3.0 起）可以数一共几张，再加 `-text` 会全部展开。

## 逐张查看服务器发来的证书

`-showcerts` 输出完整链，拆成单独文件后可以对每张执行任意 `x509` 命令。

```bash
echo | openssl s_client -connect example.com:443 -servername example.com -showcerts 2>/dev/null \
  | awk '/BEGIN CERT/{n++} /BEGIN CERT/,/END CERT/{print > ("cert-" n ".pem")}'
for f in cert-*.pem; do openssl x509 -in "$f" -noout -subject -enddate; done
```

```text
subject=CN=example.com
notAfter=Dec 25 22:56:35 2026 GMT
subject=C=US, O=SSL Corporation, CN=Cloudflare TLS Issuing ECC CA 3
notAfter=May 27 19:49:44 2035 GMT
subject=C=US, O=SSL Corporation, CN=SSL.com TLS Transit ECC CA R2
notAfter=Oct 17 17:02:22 2037 GMT
```

`s_client -showcerts` 输出开头的摘要行（`0 s:` 主题、`i:` 颁发者、`a:` 密钥和签名算法、`v:` 有效期）不拆文件也能看个大概。服务器应该发送服务器证书和中间证书；多发根证书不会出错，只是浪费流量。

## 查看公钥算法和长度

取出公钥打印头部，或者在完整输出里 grep。

```bash
openssl x509 -in example.com.crt -noout -pubkey | openssl pkey -pubin -noout -text | head -1
openssl x509 -in example.com.crt -noout -text | grep -E "Public Key Algorithm|NIST CURVE"
```

```text
Public-Key: (256 bit)
            Public Key Algorithm: id-ecPublicKey
                NIST CURVE: P-256
```

`rsaEncryption` 加 `(2048 bit)` 是 RSA，`id-ecPublicKey` 加曲线名是 ECDSA。要确认证书和私钥是否配对，见[生成私钥](https://ssl.sh/zh/cookbook/generate-keys/)里的比对方法。

## 查找 OCSP、颁发者证书和 CRL 地址

吊销检查和补全证书链要用的地址在两个扩展里，`-ocsp_uri` 直接打印 OCSP 地址。

```bash
openssl x509 -in example.com.crt -noout -ocsp_uri
openssl x509 -in example.com.crt -noout -ext authorityInfoAccess,crlDistributionPoints
```

```text
http://o.cf-i.ssl.com
Authority Information Access: 
    CA Issuers - URI:http://i.cf-i.ssl.com/Cloudflare-TLS-I-E3.cer
    OCSP - URI:http://o.cf-i.ssl.com
X509v3 CRL Distribution Points: 
    Full Name:
      URI:http://c.cf-i.ssl.com/ae801ed1c55bb579d79208b0d772acfb8cc3a208.crl
```

Let's Encrypt 从 2025 年 5 月 7 日起签发的证书不再带 OCSP 地址，改用 CRL，所以 `-ocsp_uri` 输出为空是正常的，详见 [OCSP 与 CRL](https://ssl.sh/zh/learn/ocsp-and-crl/)。CA Issuers 地址是浏览器自动补全缺失中间证书时用的。查询这些地址的命令见 [OCSP 与 CRL 命令](https://ssl.sh/zh/cookbook/ocsp-crl/)。

## 查看内嵌的 SCT

公网证书里带有签名证书时间戳（SCT），证明它已经写入证书透明度（CT）日志。

```bash
openssl x509 -in example.com.crt -noout -ext ct_precert_scts
```

```text
CT Precertificate SCTs: 
    Signed Certificate Timestamp:
        Version   : v1 (0x0)
        Log ID    : 94:4E:43:87:FA:EC:C1:EF:81:F3:19:24:26:A8:18:65:
                    01:C7:D3:5F:38:02:01:3F:72:67:7D:55:37:2E:19:D8
        Timestamp : Sep 26 22:59:12.866 2026 GMT
        Extensions: none
        Signature : ecdsa-with-SHA256
    Signed Certificate Timestamp:
        ...
```

Chrome 和 Safari 要求公网证书带有来自多个日志的 SCT 才信任；私有 CA 签的证书没有 SCT，也不需要。Log ID 标识是哪个日志。用 [CT 查询工具](https://ssl.sh/zh/tools/ct-search/)可以查出你的域名签发过的所有证书，原理见[证书透明度](https://ssl.sh/zh/learn/certificate-transparency/)。
