首页 服务器系统 Linux

Linux使用OpenSSL生成SSL证书


安装OpenSSL

确保你的系统上已经安装了OpenSSL。你可以在终端上输入以下命令来检查是否已安装:

openssl version

如果已安装,你将看到OpenSSL的版本号。如果未安装,需要根据你使用的Linux发行版进行安装,例如:

  • Debian/Ubuntu:sudo apt-get install openssl
  • CentOS/RHEL:sudo yum install openssl

生成私钥文件

使用以下命令生成一个私钥文件(例如 private.key):

openssl genrsa -out private.key 2048

生成一个2048位的RSA私钥。

生成证书请求文件(CSR)

使用以下命令生成一个证书请求文件(例如 server.csr):

openssl req -new -key private.key -out server.csr

在生成CSR的过程中,你将需要提供一些证书信息,比如常用名称、国家、email、组织名称等,详细如下:

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
State or Province Name (full name) []:bj
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:bj
Organizational Unit Name (eg, section) []:bj
Common Name (eg, your name or your server's hostname) []:bj
Email Address []:xxx@xxx.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:bj

生成自签名证书

使用以下命令生成自签名证书(例如 server.crt):

openssl x509 -req -days 365 -in server.csr -signkey private.key -out server.crt

生成一个有效期为365天的自签名证书,它将使用私钥文件来签名证书请求文件。

注意,可以根据需要调整证书的有效期 -days 参数。

配置服务器

至此ssl证书制作完成,将生成的私钥文件 private.key 和证书文件 server.crt 配置到Nginx或Apache等服务器上即可使用。

注意:使用OpenSSL成功生成了自签名的SSL证书,在浏览器中会被标记为不受信任,如果你需要使用受信任的SSL证书,你需要从受信任的证书颁发机构(CA)购买或获取免费的证书。这只是一个生成自签名SSL证书的简单示例,具体的步骤和命令可能会因不同的需求和环境而有所变化。建议查阅OpenSSL文档以获取更详细的指导。

相关推荐