
让我们进入 OpenSSL 的世界,它是您保护在线数据安全的可靠盟友! 无论您是初学者还是有一定经验者,本文都将为您提供最常用 OpenSSL 命令的简明指南
我将介绍 CSR 和密钥生成、证书管理、证书格式转换等内容。 跟上我们的步伐,很快你就能像专家一样使用 OpenSSL!

OpenSSL 常规命令
确保在系统中安装OpenSSL并了解其版本,开启OpenSSL之旅。 这些基本命令是您掌握 OpenSSL 的第一步。
检查 Linux 上是否安装了 OpenSSL
适用于使用 rpm 软件包的 GNU/Linux 发行版:
rpm -qa | grep -i openssl
适用于使用 deb 软件包的 GNU/Linux 发行版:
dpkg -l | grep -i openssl
对于 Arch Linux,请使用
pacman -Q openssl
检查 OpenSSL 版本
openssl 版本 -a
企业社会责任管理
创建和管理证书签名请求(CSR)在 SSL 流程中至关重要。 了解如何生成 CSR、处理无提示 CSR 生成、验证其详细信息等。
为现有私钥生成 CSR
openssl req -new -key yourKey.key -out yourCSR.csr
执行命令后,系统将向您提出一系列问题。 您提供的答复将纳入《企业社会责任报告》。
生成 CSR 时禁用问题提示
使用该命令生成 CSR,无需提示信息和具体细节。
openssl req -new -key yourKey.key -out yourCSR.csr \
-subj "/C=US/ST=Utah/L=Lehi/O=Your Company, Inc./OU=IT/CN=yourdomain.com"
检查 CSR 信息
Verify that the CSR contains the correct information with this OpenSSL command before sending it to a Certificate Authority.
openssl req -text -in yourCSR.csr -noout -verify
No OpenSSL to hand, or working from a machine where you would rather not install it? Our CSR decoder returns the same subject, key size, and signature algorithm in the browser.
根据现有证书生成 CSR
openssl x509 -x509toreq -in yourCertificate.crt -out yourCSR.csr -signkey yourPrivateKey.key
向 CA 发送 CSR
使用此命令显示并复制 CSR,以提交给证书颁发机构。
cat yourCSR.csr
管理私人和公用密钥
了解 OpenSSL 中处理私钥的来龙去脉。 本节涵盖所有与密钥相关的基本要素,从生成、解码到管理口令。
使用 OpenSSL 生成私钥
Create a new private key using the RSA algorithm and specify the key size with this command.
openssl genrsa -out yourPrivateKey.key 2048
To generate your private key, specify the key algorithm, the key size, and an optional passphrase. The standard key algorithm is RSA, but you can also select ECDSA for specific situations.
使用 RSA 密钥算法时,密钥大小应选择 2048 位;使用 ECDSA 算法时,密钥大小应选择 256 位。 任何小于 2048 的密钥大小都不安全,而更大的值可能会降低性能。

最后,你应该决定是否需要为私钥设置口令。 请注意,某些服务器不接受带密码的私人密钥。
准备好生成私钥(使用 RSA 算法)后,运行下面的命令:
查看私人密钥信息
cat yourPrivateKey.key
解码私人密钥
使用此命令解码并以文本格式显示私人密钥。
openssl rsa -text -in yourPrivateKey.key -noout
从私钥中提取公钥
openssl rsa -in yourPrivateKey.key -pubout -out yourPublicKey.key
一次性创建私钥和 CSR
openssl req -out yourCSR.csr -new -newkey rsa:2048 -nodes -keyout yourPrivateKey.key
该命令生成不含口令的私钥(-keyout yourPrivateKey.key)和 CSR 代码(-out yourCSR.csr)。
从私钥中移除口令
openssl rsa -in yourPrivateKey.pem -out yourNewPrivateKey.pem
检查私人密钥
使用此命令验证私钥的完整性和正确性。
openssl rsa -in yourPrivateKey.key -check
使用 OpenSSL 验证证书、私钥和 CSR 之间的一致性
验证证书是否与私钥或 CSR 匹配:
openssl x509 -noout -modulus -in certificate.crt | openssl md5
确保私钥与特定 SSL 证书或 CSR 正确对应:
openssl rsa -noout -modulus -in privateKey.key | openssl md5
确认 CSR 与私钥匹配,并适用于特定证书:
openssl req -noout -modulus -in CSR.csr | openssl md5
All three commands print an MD5 hash, and the files share a key pair only when the hashes are identical. One difference means a mismatch. Keep in mind that the modulus check is RSA only: run it against an ECDSA key and OpenSSL returns an error instead of a hash. For ECC keys, or when you would rather verify the key pair without OpenSSL, the Certificate Key Matcher compares the public key fingerprint of all three files inside your browser.
管理证书
了解如何使用 OpenSSL 命令检查、生成和验证 SSL/TLS 证书,包括检查 SSL 连接以确保通信渠道安全。
在 OpenSSL 中检查证书
使用此命令检查 SSL 证书的详细信息。
openssl x509 -text -in yourCertificate.crt -noout
生成自签名证书
req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout yourPrivateKey.key -out yourdomain.crt
检查 SSL 连接
使用此命令可测试和诊断与服务器的 SSL 连接。
openssl s_client -connect www.yoursite.com:443
Prefer a point-and-click version? Our SSL Checker runs the same diagnosis, plus certificate chain and cipher details, so you can check your SSL certificate online instead.
转换 SSL 文件
探索 OpenSSL 在转换SSL 文件格式方面的灵活性。 本节将向您介绍在DER、PEM 和 PKCS#12 等不同文件类型之间进行转换的 OpenSSL 命令。
For a one-off job on a machine without OpenSSL, the SSL Converter handles the same DER, PEM, PKCS#7 and PKCS#12 conversions in your browser.
DER 至 PEM
openssl x509 -inform der -in yourCertificate.cer -out yourCertificate.pem
PEM 至 DER
openssl x509 -outform der -in yourCertificate.pem -out yourCertificate.der
PKCS#12 转 PEM
openssl pkcs12 -in yourKeyStore.pfx -out yourKeyStore.pem -nodes
PEM 转 PKCS#12
openssl pkcs12 -export -out yourCertificate.pfx -inkey yourPrivateKey.key -in yourCertificate.crt -certfile yourCACert.crt
底线
掌握最常用的 OpenSSL 命令是提高数字安全技能的基础。
从生成 CSR、管理密钥到转换证书,我们所介绍的命令将使您掌握必要的知识,自信地驾驭 OpenSSL 的复杂性。






