openssl 自签名证书
警告:以下命令会生成私钥和自签名证书。私钥文件不要提交到仓库或发送给他人;
-noenc会生成未加密私钥,只适合本地测试或有额外文件权限保护的场景。OpenSSL 3.0 中-nodes已弃用,使用-noenc。
openssl req -x509 -noenc -days 365 -newkey rsa:2048 -keyout server.key -out server.crt -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/OU=MyDivision/CN=www.mywebsite.com" -addext "subjectAltName=DNS:www.mywebsite.com,DNS:mywebsite.com"RSA 生成证书
Section titled “RSA 生成证书”警告:私钥是身份凭据,生成后应限制文件权限并妥善备份。生产环境优先使用带口令或受密钥管理系统保护的私钥。
openssl genrsa -out ca.key 2048添加密码保护:
openssl genrsa -aes256 -out ca.key 2048生成 CA 证书
Section titled “生成 CA 证书”警告:CA 私钥可以签发其他证书。不要在业务服务器上长期保存测试 CA 私钥,也不要把测试 CA 导入生产信任库。
openssl req -x509 -days 3650 -key ca.key -out ca.crt -subj "/CN=MyTestCA" -addext "basicConstraints=critical,CA:TRUE" -addext "keyUsage=critical,keyCertSign,cRLSign"openssl x509 -text -noout -in ca.crt生成域名证书
Section titled “生成域名证书”警告:下面会生成域名私钥和证书请求。
subjectAltName应写入实际 DNS 名称或 IP,浏览器和 TLS 客户端通常不再只依赖CN做主机名匹配。
openssl genrsa -out www.example.com.key 2048生成请求:
openssl req -new -key www.example.com.key -out www.example.com.csr -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/OU=MyDivision/CN=www.example.com" -addext "subjectAltName=DNS:www.example.com"查看请求文件:
openssl req -text -noout -verify -in www.example.com.csr生成证书:
openssl x509 -req -days 365 -in www.example.com.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out www.example.com.crt -extfile <(printf "subjectAltName=DNS:www.example.com")验证证书:
openssl verify -CAfile ca.crt www.example.com.crtECC 证书
Section titled “ECC 证书”警告:ECC 私钥同样是敏感凭据。示例使用
prime256v1,是否符合你的合规要求需要结合目标环境核验。
带密码保护:
openssl ecparam -genkey -noout -name prime256v1 | openssl ec -aes256 -out ca.key无密码保护:
openssl ecparam -genkey -name prime256v1 -noout -out ca.key生成 CA 证书
Section titled “生成 CA 证书”通过配置文件生成:
[ req ]default_bits = 2048default_md = sha256distinguished_name = req_distinguished_namex509_extensions = v3_caprompt = no
[ req_distinguished_name ]C = CNST = BeijingL = BeijingO = MyCompanyOU = MyDivisionCN = MyCA
[ v3_ca ]basicConstraints = critical,CA:TRUEkeyUsage = critical, digitalSignature, cRLSign, keyCertSign警告:该命令会用
ca.key创建 CA 证书。执行前确认配置文件中的CN、basicConstraints和keyUsage符合用途。
openssl req -x509 -new -noenc -key ca.key -sha256 -days 365 -out ca.crt -config openssl.cnf命令行生成:
openssl req -x509 -new -noenc -key ca.key -sha256 -days 365 -out ca.crt -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/OU=MyDivision/CN=MyCA" -addext "basicConstraints=critical,CA:TRUE" -addext "keyUsage=critical,digitalSignature,cRLSign,keyCertSign"生成证书私钥:
openssl ecparam -genkey -name prime256v1 -noout -out www.example.com.key生成证书请求:
openssl req -new -key www.example.com.key -out www.example.com.csr -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/OU=MyDivision/CN=www.example.com" -addext "subjectAltName=DNS:www.example.com"生成证书:
openssl x509 -req -days 365 -in www.example.com.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out www.example.com.crt -extfile <(printf "subjectAltName=DNS:www.example.com")查看私钥:
警告:查看私钥会在终端输出敏感材料。只在受控终端中操作,不要把输出复制到聊天、日志或工单系统。
openssl ec -text -noout -in ca.key通过已有 CA 证书和 CA 私钥,为已有 server.key 签发测试证书:
警告:该命令使用 CA 私钥签发证书,并生成未加密输出证书。执行前确认
server.key已存在且 SAN、EKU、Key Usage 与用途一致。
openssl req -x509 -new -noenc -key server.key -sha256 -days 365 -CA ca.crt -CAkey ca.key -out server.crt -subj "/CN=localhost" -addext "extendedKeyUsage=clientAuth,serverAuth" -addext "subjectAltName=IP:127.0.0.1,DNS:localhost" -addext "basicConstraints=critical,CA:FALSE" -addext "keyUsage=critical,digitalSignature,keyEncipherment"-addext 选项
Section titled “-addext 选项”basicConstraints:指定证书是否可以作为证书颁发机构,以及路径长度等限制。常见值如critical,CA:TRUE,pathlen:0或critical,CA:FALSE。keyUsage:定义证书公钥的用途。常见值包括digitalSignature、keyEncipherment、keyAgreement、keyCertSign、cRLSign。CA 证书通常需要keyCertSign和cRLSign;服务端证书不要设置CA:TRUE。extendedKeyUsage:进一步限定证书用途,例如serverAuth、clientAuth、emailProtection、codeSigning、OCSPSigning、timeStamping。
- openssl-req(1) - OpenSSL 3.0 Documentation(访问日期:2026-05-31)
- openssl-x509(1) - OpenSSL 3.0 Documentation(访问日期:2026-05-31)
- x509v3_config(5) - OpenSSL 3.0 Documentation(访问日期:2026-05-31)
- openssl-genrsa(1) - OpenSSL 3.0 Documentation(访问日期:2026-05-31)
- OpenSSL EC parameter command - OpenSSL 3.0 Documentation(访问日期:2026-05-31)