一条命令创建自签名证书?


只需要一条命令,就可以创建1个自签名证书,这个自签名证书,可以用在nginx中,作为server端证书:

 

openssl req -x509 \
        -newkey \
        rsa:4096 \
        -nodes \
        -keyout server.key \
        -out server.crt \
        -sha256 \
        -days 3650 \
        -subj "/C=CN/ST=Beijing/L=Beijing/O=Alididi/OU=Ops/CN=www.example.com"

 

参数说明:

  • -days 3650,证书有效期
  • -subj为使用者信息.
  • -nodes:为不需要密码的证书。

 

执行命令后:

 

[root@centos7 nginx]# openssl req -x509 \
>         -newkey \
>         rsa:4096 \
>         -nodes \
>         -keyout server.key \
>         -out server.crt \
>         -sha256 \
>         -days 3650 \
>         -subj "/C=CN/ST=Beijing/L=Beijing/O=Alididi/OU=Ops/CN=www.example.com"
Generating a 4096 bit RSA private key
.......++
........................................................++
writing new private key to 'server.key'
-----
[root@centos7 nginx]# ls -l
total 8
-rw-r--r--. 1 root root 2025 Aug 25 01:50 server.crt
-rw-r--r--. 1 root root 3272 Aug 25 01:50 server.key
[root@centos7 nginx]# 

 

这样,就生成了证书和key.

 

增加多个DNS(使用者可选名称)

 

使用subjectAltName中,使用备用的DNS的名字

-extensions san \
        -config <(echo '[req]'; echo 'distinguished_name=req';
        echo '[san]'; echo 'subjectAltName=DNS:example.com,DNS:example.net')

 

openssl req -x509 \
        -newkey \
        rsa:4096 \
        -nodes \
        -keyout server.key \
        -out server.crt \
        -sha256 \
        -days 3650 \
        -subj "/C=CN/ST=Beijing/L=Beijing/O=Alididi/OU=Ops/CN=www.example.com" \
        -extensions san \
        -config <(echo '[req]'; echo 'distinguished_name=req';
        echo '[san]'; echo 'subjectAltName=DNS:example.com,DNS:example.net')

 

 

执行的过程

[root@centos7 nginx]# openssl req -x509 \
>         -newkey \
>         rsa:4096 \
>         -nodes \
>         -keyout server.key \
>         -out server.crt \
>         -sha256 \
>         -days 3650 \
>         -subj "/C=CN/ST=Beijing/L=Beijing/O=Alididi/OU=Ops/CN=www.example.com" \
>         -extensions san \
>         -config <(echo '[req]'; echo 'distinguished_name=req';
>         echo '[san]'; echo 'subjectAltName=DNS:example.com,DNS:example.net')
Generating a 4096 bit RSA private key
..............................................................................++
.....................................++
writing new private key to 'server.key'
-----
[root@centos7 nginx]# ls
server.crt  server.key
[root@centos7 nginx]# ls -l
total 8
-rw-r--r--. 1 root root 1968 Aug 25 02:11 server.crt
-rw-r--r--. 1 root root 3272 Aug 25 02:11 server.key
[root@centos7 nginx]# 

 

OK,这个时候,已经生成了有多个备选名字的证书。

posted @ 2022-08-25 14:14  Zhai_David  阅读(322)  评论(0编辑  收藏  举报