12.19 生成ssl密鑰對

生成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工具linux

  • 在虛擬上頒發一套證書,生成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是私鑰
相關文章
相關標籤/搜索