什么是 openssl s_client -connect?
openssl s_client 是 OpenSSL 提供的一个命令行工具,用于与 SSL/TLS 服务器建立连接并进行交互。
通过 -connect host:port 参数,你可以指定要连接的目标地址和端口,常用于调试 HTTPS、SMTPS、IMAPS 等加密服务。
基本语法
openssl s_client -connect <host>:<port> [选项]
常用示例
1. 测试 HTTPS 网站(默认端口 443)
openssl s_client -connect example.com:443
2. 指定 SNI(Server Name Indication)
openssl s_client -connect example.com:443 -servername example.com
3. 查看证书链
openssl s_client -connect example.com:443 -showcerts
4. 测试 SMTP over SSL(端口 465)
openssl s_client -connect smtp.example.com:465
交互式演示(模拟输出)
点击下方按钮查看典型命令的输出片段:
CONNECTED(00000003) depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA verify return:1 depth=1 C = US, O = DigiCert Inc, CN = DigiCert SHA2 Secure Server CA verify return:1 depth=0 C = US, ST = California, L = San Francisco, O = "Example, Inc.", CN = *.example.com verify return:1 --- Certificate chain 0 s:CN = *.example.com i:C = US, O = DigiCert Inc, CN = DigiCert SHA2 Secure Server CA 1 s:C = US, O = DigiCert Inc, CN = DigiCert SHA2 Secure Server CA i:C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA --- SSL handshake has read 3245 bytes and written 443 bytes Verification: OK --- New, TLSv1.3, Cipher is TLS_AES_128_GCM_SHA256 ...
常见用途
- 验证服务器证书是否有效
- 检查 TLS 协议版本和加密套件支持
- 调试证书链问题
- 手动发送 HTTP 请求测试响应(在连接后输入)
- 排查 CDN 或负载均衡器配置
注意事项
- 若目标使用 SNI(大多数现代网站),务必加上 -servername 参数。
- 该命令不会自动关闭连接,可手动按 Ctrl+C 退出。
- 输出中 Verify return code: 0 (ok) 表示证书验证成功。