温馨提示×

linux openssl如何测试连接

小樊
46
2026-07-15 01:10:17
栏目: 云计算

在 Linux 下使用 OpenSSL 测试连接,常见用途是 测试 TCP/TLS/SSL 连通性,主要用于排查 HTTPS、IMAPS、SMTPS、数据库连接等是否可达。下面按场景给你讲常用方法。


一、测试 TLS / SSL 连接(最常见)

1️⃣ 测试 HTTPS(443)

openssl s_client -connect www.baidu.com:443

✅ 能成功说明:

  • TCP 可达
  • TLS 握手成功

常见组合:

openssl s_client -connect www.baidu.com:443 -showcerts

显示证书链。


2️⃣ 指定 TLS 版本(排查版本问题)

# TLS 1.2
openssl s_client -connect www.baidu.com:443 -tls1_2

# TLS 1.3
openssl s_client -connect www.baidu.com:443 -tls1_3

3️⃣ 测试 SNI(虚拟主机必备)

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

⚠️ 很多 HTTPS 站点 必须加 -servername,否则可能返回错误证书或连接失败。


二、测试普通 TCP 连接(不加密)

虽然 openssl 主要用于 TLS,但也可以测试 TCP:

openssl s_client -connect 192.168.1.10:80

但更推荐:

nc -vz 192.168.1.10 80
# 或
telnet 192.168.1.10 80

三、测试邮件类协议(IMAP / SMTP / POP3)

IMAPS(993)

openssl s_client -connect mail.example.com:993

SMTPS(465)

openssl s_client -connect mail.example.com:465

SMTP 启动 TLS(587)

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

同理支持:

  • -starttls imap
  • -starttls pop3

四、测试证书 / 密钥 / 双向 TLS

1️⃣ 使用客户端证书

openssl s_client \
-connect server:443 \
-cert client.crt \
-key client.key \
-CAfile ca.crt

2️⃣ 只看证书信息(不进入交互)

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

五、查看握手过程(排错神器)

openssl s_client -connect www.baidu.com:443 -debug

或:

openssl s_client -connect www.baidu.com:443 -state

六、常见错误与含义

错误 含义
connect: Connection refused 端口未开
no peer certificate available 证书问题
handshake failure TLS 版本/加密套件不匹配
certificate verify failed CA 不受信任

七、快速判断“能不能通”

能通

Verify return code: 0 (ok)

不通

  • 卡住
  • 立即断开
  • 报错

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

  • 要测试 什么服务(HTTPS / MySQL / Redis / 邮件)
  • 端口号
  • 是否 需要证书

我可以给你一条 直接可用的命令

0