Debian OpenSSL命令行使用技巧
导读:Debian 上 OpenSSL 命令行高效用法 一 快速上手与环境准备 安装与版本检查:在 Debian 上安装 OpenSSL 并检查版本,确保工具可用与为较新稳定版。 命令:sudo apt update && s...
Debian 上 OpenSSL 命令行高效用法
一 快速上手与环境准备
- 安装与版本检查:在 Debian 上安装 OpenSSL 并检查版本,确保工具可用与为较新稳定版。
- 命令:
sudo apt update & & sudo apt install -y openssl - 命令:
openssl version
- 命令:
- 常用目录与权限:系统信任库位于 /etc/ssl/certs,私钥建议放在 /etc/ssl/private 并设置 700 权限,避免泄露。
- 命令:
sudo mkdir -p /etc/ssl/private & & sudo chmod 700 /etc/ssl/private
- 命令:
- 更新 CA 信任库:当系统或 CA 证书更新后,刷新信任链。
- 命令:
sudo apt install --only-upgrade ca-certificates & & sudo update-ca-certificates
- 命令:
- 开发头文件:如需编译依赖 OpenSSL 的程序,安装开发包。
- 命令:
sudo apt install -y libssl-dev
- 命令:
二 密钥与证书高频操作
- 生成私钥
- RSA(通用):
openssl genpkey -algorithm RSA -out server.key 2048 - ECDSA(高效):
openssl ecparam -genkey -name secp384r1 -out server.key
- RSA(通用):
- 生成 CSR(证书签名请求)
- 命令:
openssl req -new -key server.key -out server.csr - 提示填写:C/ST/L/O/OU/CN(CN 建议填域名或 IP)
- 命令:
- 自签名证书(测试/内网)
- 命令:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
- 命令:
- 查看证书信息
- 详细信息:
openssl x509 -in server.crt -text -noout - 有效期截止:
openssl x509 -in server.crt -enddate -noout - 指纹(SHA-256):
openssl x509 -in server.crt -fingerprint -sha256 -noout
- 详细信息:
- 格式转换
- PEM 转 DER:
openssl x509 -in server.crt -outform DER -out server.der
- PEM 转 DER:
- 从私钥导出公钥
- RSA:
openssl rsa -in server.key -pubout -out server.pub - ECDSA:
openssl ec -in server.key -pubout -out server.pub
- RSA:
三 连接测试与诊断
- 基础握手与查看链
- 命令:
openssl s_client -connect www.example.com:443 -servername www.example.com - 将握手与服务器响应保存到文件:
openssl s_client -connect www.example.com:443 -servername www.example.com > handshake.log 2> & 1
- 命令:
- 指定协议与套件
- 仅用 TLS 1.3:
openssl s_client -connect www.example.com:443 -tls1_3 - 指定套件(示例):
openssl s_client -connect www.example.com:443 -cipher AES256-GCM-SHA384
- 仅用 TLS 1.3:
- 证书验证与信任
- 严格校验证书链:
openssl s_client -connect www.example.com:443 -servername www.example.com -verify_return_error - 使用自定义 CA 验证:
openssl s_client -connect www.example.com:443 -CAfile my_ca_cert.pem
- 严格校验证书链:
- 客户端证书认证
- 命令:
openssl s_client -connect www.example.com:443 -cert client.crt -key client.key
- 命令:
- 仅查看远端证书(不建链)
- 命令:
echo | openssl s_client -connect www.example.com:443 -servername www.example.com -showcerts 2> /dev/null | openssl x509 -noout -text
- 命令:
四 文件格式与兼容性要点
- 编码与容器
- PEM:Base64 可读,常见后缀 .pem/.crt/.key
- DER:二进制,常见后缀 .der
- PKCS#12:含证书与私钥的容器,常见后缀 .p12/.pfx
- 常用转换
- PEM 证书转 DER:
openssl x509 -in server.crt -outform DER -out server.der - 查看私钥信息(RSA):
openssl rsa -in server.key -text -noout - 查看公钥信息:若文件是证书或 X.509 格式,用
openssl x509 -in pub.pem -text -noout;若是“裸公钥”,用openssl pkey -in pubkey.pem -pubin -text -noout
- PEM 证书转 DER:
- 证书链顺序
- 服务器应发送:服务器证书 → 中间 CA 证书(如有) → 根 CA 证书(根 CA 通常无需发送)
五 安全与排错最佳实践
- 安全建议
- 私钥保护:设置 600/700 权限,禁止明文传输;必要时用密码加密私钥(如
openssl rsa -aes256 -in server.key -out server.key.enc) - 协议与套件:优先 TLS 1.2/1.3;避免 SSLv3/TLS1.0/1.1 与已知弱套件
- 及时更新:定期
sudo apt update & & sudo apt upgrade,并升级 ca-certificates 与 OpenSSL
- 私钥保护:设置 600/700 权限,禁止明文传输;必要时用密码加密私钥(如
- 快速排错清单
- 版本与组件:
openssl version,确认无已知漏洞的旧版本 - 证书有效性:
openssl x509 -in server.crt -enddate -noout、-fingerprint核对指纹 - 握手诊断:
openssl s_client ... -servername < 域名>并检查链、SNI、证书是否匹配 - 配置与日志:核查 /etc/ssl/openssl.cnf 与系统日志(如 /var/log/),必要时开启调试
OPENSSL_DEBUG=1 - 信任库:证书更换或新增 CA 后执行
sudo update-ca-certificates
- 版本与组件:
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Debian OpenSSL命令行使用技巧
本文地址: https://pptw.com/jishu/785937.html
