12.19 生成ssl密钥对

本文详细介绍如何使用openssl工具在Linux环境下生成SSL证书。包括私钥创建、密码移除、证书请求文件生成及公钥生成等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

生成ssl密钥对目录概要

  • cd /usr/local/nginx/conf
  • openssl genrsa -des3 -out tmp.key 2048//key文件为私钥
  • openssl rsa -in tmp.key -out aminglinux.key //转换key,取消密码
  • rm -f tmp.key
  • openssl req -new -key aminglinux.key -out aminglinux.csr//生成证书请求文件,需要拿这个文件和私钥一起生产公钥文件
  • openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt 这里的aminglinux.crt为公钥

生成ssl密钥对

在自己的虚拟机生成ssl 需要用到openssl工具

  • 在虚拟上颁发一套证书,生成ssl
  1. 首先得有一个openssl工具
  2. 切换到/usr/local/nginx/conf/目录下
[root@hf-01 ~]# cd /usr/local/nginx/conf/
[root@hf-01 conf]# 
  1. 若是没有openssl工具,可以安装下
  2. 查看openssl工具是由哪个安装包安装的
[root@hf-01 conf]# rpm -qf `which openssl`
openssl-1.0.2k-8.el7.x86_64
[root@hf-01 conf]# 

  1. 生成一个私钥,命令openssl genrsa -des3 -out tmp.key 2048
[root@hf-01 conf]# openssl genrsa -des3 -out tmp.key 2048
Generating RSA private key, 2048 bit long modulus
.......+++
......................................................................+++
e is 65537 (0x10001)
Enter pass phrase for tmp.key:        //输入密码
Verifying - Enter pass phrase for tmp.key:        //再次输入密码
[root@hf-01 conf]# 

  • openssl genrsa -des3 -out tmp.key 2048
    • genrsa ,表示生成rsa的私钥
    • 2048 ,2048长度
    • 名字为 tmp.key
  • 生成这个秘钥必须要有密码
  1. 在生成这个秘钥后比较麻烦,在nginx的配置文件里指定密码,每次访问浏览器,在https这个网址输入这个密码会很不方便,所以还需要去除这个密码
  2. 转换key,取消密码,命令 openssl rsa -in tmp.key -out gurui.key
  • -in 表示指定哪一个秘钥要被转换
  • -out 表示指定输出的
[root@hf-01 conf]# openssl rsa -in tmp.key -out gurui.key
Enter pass phrase for tmp.key:    //输入tmp.key的密码
writing RSA key
[root@hf-01 conf]# 
  1. 这时候tmp.key和gurui.key是属于同一个
  • tmp.key,有密码
  • gurui.key,没有密码
  1. 删除tmp.key
[root@hf-01 conf]# rm -f tmp.key
[root@hf-01 conf]# 
  1. 生成证书请求文件,需要拿这个请求文件和私钥一起生产公钥文件
[root@hf-01 conf]# openssl req -new -key gurui.key -out gurui.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn              //国家,2个字母
State or Province Name (full name) []:JiangSu      //省或州
Locality Name (eg, city) [Default City]:YanCheng   //城市
Organization Name (eg, company) [Default Company Ltd]:han  //公司
Organizational Unit Name (eg, section) []:han   //组织
Common Name (eg, your name or your server’s hostname) []:hanfeng  //您的主机名
Email Address []:han1118feng@163.com   //邮箱
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:hanfeng  //设置密码
An optional company name []:  //一个可选的公司名称
用请求证书文件和私钥文件,生成一个公钥
[root@hf-01 conf]# 
  • 这里的信息可以不用填写,直接回车也行
  1. 因为这是自己给自己颁发的证书,可以随意填写,若是购买那些正式的证书,那证书的信息就需要填写相对应的信息
  2. 生成公钥,命令openssl x509 -req -days 365 -in gurui.csr -signkey gurui.key -out gurui.crt
[root@hf-01 conf]# openssl x509 -req -days 365 -in gurui.csr -signkey gurui.key -out gurui.crt
Signature ok
subject=/C=11/ST=BEIJING/L=BeiJing/O=hanfeng/OU=hanfeng/CN=hanfeng/emailAddress=han1118fem\x08
Getting Private key
[root@hf-01 conf]# 

  • -days 365 证书的日期是一年
  1. gui.crt是公钥,gurui.key是私钥

转载于:https://my.oschina.net/u/3707314/blog/1603922

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值