温馨提示×

Linux openssl如何测试HTTPS

小樊
50
2026-08-25 07:38:41
栏目: 云计算

在 Linux 上可以用 OpenSSL 来测试 HTTPS 服务是否正常、证书是否有效、TLS 握手是否成功。下面按常见使用场景给你列出方法和示例。


一、最常用:测试 HTTPS 连接(TLS 握手)

openssl s_client -connect example.com:443

✅ 如果成功,你会看到:

  • 证书信息
  • TLS 版本
  • 加密套件
  • 服务器证书链

Ctrl+C 或输入 Q 退出。


二、测试指定域名(SNI,非常重要)

现代 HTTPS 站点基本都需要 SNI

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

如果不加 -servername,很多 CDN / 虚拟主机返回错误证书。


三、只查看证书信息(不进入交互)

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

常用查看项:

openssl x509 -noout -subject
openssl x509 -noout -issuer
openssl x509 -noout -dates
openssl x509 -noout -text

四、测试特定 TLS 版本

TLS 1.2

openssl s_client -connect example.com:443 -tls1_2

TLS 1.3

openssl s_client -connect example.com:443 -tls1_3

不安全的(仅测试)

openssl s_client -connect example.com:443 -tls1
openssl s_client -connect example.com:443 -tls1_1

五、测试特定端口(非 443)

openssl s_client -connect example.com:8443

六、测试 HTTPS 服务是否返回 HTTP 响应

echo -e "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n" | \
openssl s_client -connect example.com:443 -servername example.com

✅ 适合验证:

  • HTTP 服务是否正常工作
  • 证书是否匹配
  • Header 返回情况

七、检查证书是否过期

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

示例输出:

notBefore=May 20 00:00:00 2024 GMT
notAfter=May 20 23:59:59 2025 GMT

八、验证证书链是否完整

openssl s_client -connect example.com:443 -showcerts

你可以看到:

  • 服务器证书
  • 中间证书
  • 根证书

九、测试本地服务(如 Nginx / Tomcat)

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

十、常见问题排查

1️⃣ 证书不匹配

verify error:num=62:Hostname mismatch

✅ 加 -servername

2️⃣ 连接直接失败

connect: Connection refused

✅ 检查端口、防火墙、服务是否启动

3️⃣ TLS 版本不支持

handshake failure

✅ 尝试不同 -tls1_2 / -tls1_3


十一、快速总结(最常用)

# 基本测试
openssl s_client -connect example.com:443 -servername example.com

# 查看证书
openssl s_client -connect example.com:443 -servername example.com | openssl x509 -noout -text

如果你愿意,可以告诉我:

  • 测试的是 公网域名 / 内网 IP / 本地服务
  • 用的是 Nginx / Tomcat / Spring Boot / K8s Ingress

我可以给你更精确的测试命令和排错方案。

0