搬瓦工 VPS OpenSSL 证书管理完整教程

OpenSSL 是 Linux 系统上最基础也是最强大的加密工具包,广泛用于 SSL/TLS 证书的生成、管理和诊断。无论是申请商业证书、使用 Let's Encrypt 免费证书,还是搭建内部 CA 签发自签名证书,都离不开 OpenSSL 的参与。本文将系统性地介绍 OpenSSL 在证书管理中的各种用法,帮助你在搬瓦工 VPS 上熟练处理各种证书相关操作。

一、OpenSSL 基础

1.1 查看版本和路径

# 查看 OpenSSL 版本
openssl version -a

# 查看默认配置文件路径
openssl version -d

二、私钥管理

2.1 生成 RSA 私钥

# 生成 2048 位 RSA 私钥
openssl genrsa -out server.key 2048

# 生成 4096 位 RSA 私钥(更安全)
openssl genrsa -out server.key 4096

# 生成带密码保护的私钥
openssl genrsa -aes256 -out server.key 4096

2.2 生成 ECDSA 私钥

# 使用 P-256 曲线(推荐,性能好且安全)
openssl ecparam -genkey -name prime256v1 -noout -out server-ec.key

# 使用 P-384 曲线(更高安全性)
openssl ecparam -genkey -name secp384r1 -noout -out server-ec.key

2.3 查看和管理私钥

# 查看 RSA 私钥详情
openssl rsa -in server.key -text -noout

# 查看 EC 私钥详情
openssl ec -in server-ec.key -text -noout

# 去除私钥密码
openssl rsa -in server-encrypted.key -out server.key

# 添加私钥密码
openssl rsa -aes256 -in server.key -out server-encrypted.key

三、证书签名请求(CSR)

# 使用已有私钥生成 CSR
openssl req -new -key server.key -out server.csr \
  -subj "/C=CN/ST=Shanghai/L=Shanghai/O=MyCompany/OU=IT/CN=example.com"

# 同时生成私钥和 CSR
openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr

# 生成包含 SAN(主题备用名称)的 CSR
openssl req -new -key server.key -out server.csr -config <(cat <<EOF
[req]
distinguished_name = req_dn
req_extensions = v3_req
prompt = no

[req_dn]
CN = example.com
O = MyCompany
L = Shanghai
ST = Shanghai
C = CN

[v3_req]
subjectAltName = @alt_names

[alt_names]
DNS.1 = example.com
DNS.2 = www.example.com
DNS.3 = *.example.com
IP.1 = 10.0.0.1
EOF
)

# 查看 CSR 内容
openssl req -in server.csr -text -noout

四、自签名证书

# 一步生成私钥和自签名证书(有效期 365 天)
openssl req -x509 -newkey rsa:4096 -nodes \
  -keyout server.key -out server.crt -days 365 \
  -subj "/CN=example.com"

# 使用已有私钥生成自签名证书
openssl req -x509 -key server.key -out server.crt -days 365 \
  -subj "/CN=example.com"

# 生成带 SAN 的自签名证书
openssl req -x509 -newkey rsa:4096 -nodes \
  -keyout server.key -out server.crt -days 365 \
  -subj "/CN=example.com" \
  -addext "subjectAltName=DNS:example.com,DNS:*.example.com"

五、搭建简易 CA

5.1 创建 CA 根证书

mkdir -p /opt/ca/{certs,crl,newcerts,private}
touch /opt/ca/index.txt
echo 1000 > /opt/ca/serial

# 生成 CA 私钥
openssl genrsa -aes256 -out /opt/ca/private/ca.key 4096

# 生成 CA 根证书(有效期 10 年)
openssl req -x509 -new -nodes -key /opt/ca/private/ca.key \
  -sha256 -days 3650 -out /opt/ca/certs/ca.crt \
  -subj "/C=CN/ST=Shanghai/O=MyCompany/CN=MyCompany Root CA"

5.2 使用 CA 签发证书

# 使用 CA 签发服务器证书
openssl x509 -req -in server.csr \
  -CA /opt/ca/certs/ca.crt \
  -CAkey /opt/ca/private/ca.key \
  -CAcreateserial \
  -out server.crt -days 365 -sha256 \
  -extfile <(echo "subjectAltName=DNS:example.com,DNS:www.example.com")

六、证书格式转换

# PEM 转 DER
openssl x509 -in cert.pem -outform DER -out cert.der

# DER 转 PEM
openssl x509 -in cert.der -inform DER -outform PEM -out cert.pem

# PEM 转 PKCS12(包含私钥和证书)
openssl pkcs12 -export -out cert.pfx -inkey server.key -in server.crt -certfile ca.crt

# PKCS12 提取证书和私钥
openssl pkcs12 -in cert.pfx -clcerts -nokeys -out server.crt
openssl pkcs12 -in cert.pfx -nocerts -nodes -out server.key

# 合并证书链
cat server.crt intermediate.crt root.crt > fullchain.pem

七、证书诊断命令

# 查看证书详细信息
openssl x509 -in server.crt -text -noout

# 查看证书有效期
openssl x509 -in server.crt -noout -dates

# 查看证书指纹
openssl x509 -in server.crt -noout -fingerprint -sha256

# 验证证书链
openssl verify -CAfile ca.crt server.crt

# 测试远程服务器的 SSL 证书
openssl s_client -connect example.com:443 -servername example.com

# 检查证书与私钥是否匹配
openssl x509 -in server.crt -noout -modulus | md5sum
openssl rsa -in server.key -noout -modulus | md5sum
# 两者输出应相同

八、常见应用场景

  • 为 Nginx 配置 HTTPS:使用 Let's Encrypt 或自签名证书。
  • 为内部服务创建 mTLS 双向认证。
  • Docker Registry 配置 TLS。
  • LDAP 服务启用 LDAPS 加密。

总结

OpenSSL 是每个 Linux 管理员必须掌握的基础工具。掌握本文介绍的命令后,你可以自如地处理各种证书相关操作。如果需要更完整的 PKI 基础设施,可以参考 CFSSL 私有 CAStep CAHashiCorp Vault PKI 的教程。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,通过 bwh81.net 进入官网购买。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。