OpenSSL 证书与加密速查表
把 OpenSSL 日常最常敲的命令按证书查看、生成、私钥处理和 TLS 排查分组整理,HTTPS 排障直接复制改参数即可。
返回 系统与运维证书查看 Inspect Certificate 6
openssl x509 -in cert.pem -text -noout查看证书完整信息(含颁发者、有效期、SAN)
openssl x509 -in cert.pem -dates -noout只看有效期(notBefore / notAfter)
openssl x509 -in cert.pem -issuer -noout只看颁发者
openssl x509 -in cert.pem -subject -noout只看主体(域名)
openssl x509 -in cert.pem -ext subjectAltName -noout查看 SAN(多域名),证书覆盖的所有域名
openssl x509 -in cert.pem -fingerprint -sha256 -noout查看 SHA-256 指纹
远程证书检查 Remote Check 5
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates查看远程服务器证书有效期
echo | openssl s_client -connect example.com:443 -showcerts 2>/dev/null查看完整证书链
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -text -noout查看远程证书完整信息
openssl s_client -connect example.com:443 -servername example.com -tls1_2指定 TLS 版本测试连接
openssl s_client -connect example.com:443 -servername example.com -tls1_3测试 TLS 1.3 连接
证书生成 Generate Certificate 6
openssl genrsa -out private.key 2048生成 2048 位 RSA 私钥
openssl ecparam -genkey -name prime256v1 -out private.key生成 EC 私钥(更小更快)
openssl req -new -key private.key -out request.csr用私钥生成 CSR(证书签名请求)
openssl req -new -newkey rsa:2048 -nodes -keyout private.key -out request.csr一步生成私钥和 CSR
openssl req -x509 -newkey rsa:2048 -nodes -keyout private.key -out cert.pem -days 365 -subj "/CN=localhost"生成自签名证书(365 天)
openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem -days 365 -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost,DNS:*.localhost"自签名证书含 SAN 多域名
私钥处理 Private Key 5
openssl rsa -in private.key -check -noout检查私钥是否有效
openssl rsa -in encrypted.key -out plain.key去除私钥密码保护(需输入原密码)
openssl rsa -in plain.key -aes256 -out encrypted.key给私钥加 AES-256 密码保护
openssl rsa -in private.key -pubout -out public.key从私钥导出公钥
openssl pkey -in private.key -noout通用私钥查看(兼容 RSA 和 EC)
格式转换 Format Conversion 4
openssl pkcs12 -export -out cert.p12 -inkey private.key -in cert.pemPEM 转 PKCS12(.p12/.pfx)
openssl pkcs12 -in cert.p12 -out cert.pem -nodesPKCS12 转 PEM(含私钥,-nodes 不加密)
openssl pkcs12 -in cert.p12 -info -noout查看 PKCS12 内容
openssl crl2pkcs7 -nocrl -certfile cert.pem -out cert.p7bPEM 转 PKCS7(.p7b)
加密与哈希 Encrypt & Hash 7
echo -n "hello" | openssl md5计算 MD5 哈希
echo -n "hello" | openssl sha256计算 SHA-256 哈希
openssl dgst -sha256 file.txt计算文件 SHA-256
openssl enc -aes-256-cbc -in file.txt -out file.enc -pass pass:secretAES-256-CBC 加密文件
openssl enc -d -aes-256-cbc -in file.enc -out file.txt -pass pass:secretAES-256-CBC 解密文件
openssl rand -hex 32生成 32 字节随机十六进制字符串
openssl rand -base64 32生成 32 字节随机 Base64 字符串
💡 提示
- 查远程证书有效期加 -servername(SNI),否则多域名证书可能返回默认证书。
- Let's Encrypt 证书到期前用 echo | openssl s_client 确认实际部署的证书已更新,不要只看文件时间戳。
- PKCS12 转 PEM 时加 -nodes 否则导出的私钥会被密码保护,Nginx/Traefik 无法直接读取。