Generate CSR on Linux Server

当选择服务器的时候往往我们需要考虑使用什么类型的服务器。由于Windows系统应用比较广泛,因此选择Windows服务器的用户有很多,优点当然是上手容易;缺点来说价格比Linux要贵不少,而且相对没有Linux稳定,占用的资源也更多。今天这一篇我会讲解如何在Linux的服务器上用OpenSSL来生成私钥和CSR。

Linux系统比较多见的是Apache和Nginx两种服务器类型。这两种服务器由于都是需要x509格式的证书,因此生成私钥和CSR的方法都是一样的。安装上略有不同,我们可以下一篇来谈。

按顺序生成私钥和CSR(理解命令行背后的故事……)

生成私钥

这一步生成的是RSA算法2048位的私钥,而且我没有选择用密码来保护这个私钥。如果需要用密码的可以加**-des3**在这个command里面

1
2
3
4
5
openssl genrsa -out aufommPrivate.key 2048
Generating RSA private key, 2048 bit long modulus
........+++
.......................................................+++
e is 65537 (0x10001)
生成CSR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
openssl req -new -key aufommPrivate.key -out aufommRequest.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) []:AU
State or Province Name (full name) []:Victoria
Locality Name (eg, city) []:Melbourne
Organization Name (eg, company) []:aufomm
Organizational Unit Name (eg, section) []:Blog
Common Name (eg, fully qualified host name) []:www.aufomm.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:

有些CSR上面用不着的信息就不要加上去了。例如Email Address和下面Extra Attributes的部分我就直接回车跳过。CSR里多余的信息可能会造成某些CA读不出来的情况。

用在线工具获取OpenSSL的命令,一次性生成私钥和CSR

我们这里用来演示的是来自CA Digicert的OpenSSL CSR Wizard

在网页上我们可以看到需要填入你想要在证书上显示的信息,点击Generate之后在右边就会显示出你需要的openssl 的命令。

Key Size的部分大部分人都应该选择RSA 2048,如果有时间我之后会写一片ECC和RSA的对比文章以及ECC证书的应用

OpenSSLWizard

把生成的Command Copy到Terminal里面去运行

1
2
3
4
5
6
openssl req -new -newkey rsa:2048 -nodes -out www_aufomm_com.csr -keyout www_aufomm_com.key -subj "/C=AU/ST=Victoria/L=Melbourne/O=aufomm/OU=Blog/CN=www.aufomm.com"
Generating a 2048 bit RSA private key
...+++
.....................................................................................................+++
writing new private key to 'www_aufomm_com.key'
-----

就这么简单,私钥和CSR就生成好了。接下来我们只需要把这个CSR提交给CA,然后等待CA验证过后证书即可签发。