首页·终端命令

openssl s_client

网络

用法

openssl s_client -connect <host>:443 -servername <host>

开一条裸的 TLS 连接,用来查看证书和握手过程;不写 `-servername`,一个地址上放多个站点的服务器就会给你默认证书,而不加 `< /dev/null` 它会一直等着输入不返回。

常用选项

选项含义
-connect host:443Where to open the TLS connection.
-servername <host>The SNI name. Without it a shared host serves its default certificate.
-showcertsPrint the whole chain the server sent, not just the leaf.
-tls1_2 / -tls1_3Force one protocol version, to test what the server still accepts.
-verify_return_errorFail instead of continuing when the chain does not validate.
< /dev/nullClose stdin, or the command sits and waits forever.

示例

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

When the certificate expires.

openssl s_client -connect example.com:443 -showcerts < /dev/null

Inspect the full chain for a missing intermediate.

连不上的时候,这些能把范围缩小——是名字解析不了、没有路由,还是端口被挡。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. openssl s_client 是做什么的?

开一条裸的 TLS 连接,用来查看证书和握手过程;不写 `-servername`,一个地址上放多个站点的服务器就会给你默认证书,而不加 `< /dev/null` 它会一直等着输入不返回。

Q. 怎么写?

openssl s_client -connect <host>:443 -servername <host> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man openssl。这条命令属于网络。

相关命令

man 手册: man openssl-s_client