温馨提示×

linux openssl怎样调试连接

小樊
40
2026-09-07 15:04:50
栏目: 云计算

在 Linux 下使用 OpenSSL 调试网络连接,常见场景包括:

  • 查看 TLS/SSL 握手过程
  • 验证证书链
  • 排查连接失败原因
  • 手动模拟 HTTPS / SMTP / IMAP 等 TLS 服务

下面按常用方法示例说明。


一、最常用:openssl s_client 调试 TLS 连接

1. 基本用法(HTTPS 示例)

openssl s_client -connect example.com:443

看到的信息包括:

  • 证书内容
  • 证书链
  • 使用的协议和 cipher
  • 握手是否成功

Ctrl+C 或输入 Q 退出。


2. 显示详细信息(推荐)

openssl s_client -connect example.com:443 -servername example.com -showcerts

说明:

  • -servername:指定 SNI(虚拟主机很重要)
  • -showcerts:显示完整证书链

3. 指定协议版本(排查协议不兼容)

openssl s_client -connect example.com:443 -tls1_2
openssl s_client -connect example.com:443 -tls1_3

可用版本:

  • -ssl3(一般不推荐)
  • -tls1
  • -tls1_1
  • -tls1_2
  • -tls1_3

4. 调试握手过程(非常有用)

openssl s_client -connect example.com:443 -debug

或更详细的:

openssl s_client -connect example.com:443 -state -debug
  • -state:显示握手状态
  • -debug:显示原始字节

5. 只验证证书,不建立应用连接

openssl s_client -connect example.com:443 -verify_return_error

或:

openssl s_client -connect example.com:443 -CApath /etc/ssl/certs

二、手动发送 HTTP 请求(模拟客户端)

openssl s_client -connect example.com:443 -servername example.com

连接成功后输入:

GET / HTTP/1.1
Host: example.com
Connection: close

(结尾空行不能少)


三、调试其他 TLS 服务

SMTP(STARTTLS)

openssl s_client -connect mail.example.com:25 -starttls smtp

IMAP

openssl s_client -connect imap.example.com:143 -starttls imap

POP3

openssl s_client -connect pop.example.com:110 -starttls pop3

MySQL(TLS)

openssl s_client -connect db.example.com:3306

四、验证本地证书 / 私钥

查看证书

openssl x509 -in cert.pem -text -noout

查看私钥

openssl rsa -in key.pem -check

验证证书和私钥是否匹配

openssl x509 -noout -modulus -in cert.pem | openssl md5
openssl rsa -noout -modulus -in key.pem | openssl md5

两个 md5 应一致。


五、常见问题排查思路

1. 连接超时

telnet example.com 443
# 或
nc -vz example.com 443

2. 证书不受信任

  • 检查系统 CA:
ls /etc/ssl/certs
  • 指定 CA:
openssl s_client -connect example.com:443 -CAfile ca.pem

3. SNI 问题

未加 -servername 可能导致:

  • 返回默认证书
  • 握手失败

六、结合抓包(进阶)

tcpdump -i any -w tls.pcap host example.com and port 443

再用 Wireshark 分析 TLS 握手。


如果你有具体场景(如 Nginx、MySQL、Kubernetes、自签名证书),可以告诉我,我可以给更针对性的调试命令。

0