命令行创建https证书详解
命令行创建https证书详解
- 一、创建目录
- 二、进入目录
- 三、创建私钥
- 四、根据私钥创建证书
- 五、查看
重要声明:自己制作的证书是 “自签名证书”(Self-Signed Certificate),它不会被任何主流浏览器信任。因此,它绝对不能用于生产环境,否则用户会看到 “您的连接不是私密连接” 等安全警告。它的主要用途是本地开发、测试或内部网络。
制作自签名证书最常用、最标准的工具是 OpenSSL。下面是详细的步骤。
前提条件
一、创建目录
[root@hadoop103 module]# mkdir /opt/module/http-key
二、进入目录
[root@hadoop103 module]# cd http-key/
三、创建私钥
openssl genrsa -idea -out server.key 2048:生成一个使用 IDEA 算法加密的私钥。
[root@hadoop103 http-key]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus
.......................+++
......................................................................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
You have new mail in /var/spool/mail/root
[root@hadoop103 http-key]# ll
total 4
-rw-r--r-- 1 root root 1739 Oct 6 22:02 server.key
四、根据私钥创建证书
[root@hadoop103 http-key]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
Generating a 2048 bit RSA private key
............................+++
............+++
writing new private key to 'server.key'
-----
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) []:sd
Locality Name (eg, city) [Default City]:qd
Organization Name (eg, company) [Default Company Ltd]:mk
Organizational Unit Name (eg, section) []:join
Common Name (eg, your name or your server's hostname) []:bob
Email Address []:324@qq.com
You have new mail in /var/spool/mail/root
解释说明
参数 | 含义和作用 |
---|---|
openssl req | 这是用于处理证书请求(Certificate Request)的主命令。 |
-days 36500 | 设置证书有效期。这里设置的是 36500 天,大约是 100 年。这对于一个不需要频繁更新的测试证书来说非常方便。 |
-x509 | 核心参数。这个参数告诉 openssl 直接生成一个自签名的证书(X.509 格式),而跳过生成 CSR(证书签名请求)的步骤。这就是它能一步到位的关键。 |
-sha256 | 指定签名算法。使用 SHA-256 哈希算法来签署证书,这是目前非常安全和推荐的标准。 |
-nodes | (No DES)。这个参数告诉 openssl 不要对生成的私钥进行加密。这样生成的 server.key 文件就是明文的,在配置 Web 服务器时不需要每次都输入密码,非常方便。 |
-newkey rsa:2048 | 同时生成新的私钥。这个参数会在生成证书的同时,自动生成一个新的 RSA 私钥。rsa:2048 表示私钥的长度为 2048 位。 |
-keyout server.key | 指定私钥输出文件。将新生成的私钥保存到名为 server.key 的文件中 |
-out server.crt | 指定证书输出文件。将最终生成的自签名证书保存到名为 server.crt 的文件中。 |
执行这个命令会发生什么?
1、OpenSSL 会在内存中生成一个 2048 位的 RSA 私钥。
2、因为你使用了 -nodes,这个私钥将是未加密的。
3、OpenSSL 会提示你输入证书的相关信息(如国家、城市、Common Name 等),就像我们之前生成 CSR 时一样。
4、它会使用你提供的信息和刚刚生成的私钥,直接创建一个 X.509 格式的自签名证书。
5、最后,它会将未加密的私钥保存到 server.key,并将自签名证书保存到 server.crt。
优点和缺点
- 优点:
- 高效:一条命令搞定所有事情,非常适合快速上手和自动化脚本。
- 方便:生成的私钥是未加密的,配置服务器时无需输入密码。
- 缺点:
- 安全性较低:因为私钥是未加密的(明文),一旦服务器被攻破,私钥就有泄露的风险。因此,绝对不要在生产环境中使用这种方式生成的证书。
- 有效期过长:100 年的有效期在安全实践中是不被推荐的,因为它意味着一旦私钥泄露,攻击者可以在很长一段时间内滥用它。对于测试来说,这不是问题。
五、查看
[root@hadoop103 http-key]# ll
total 8
-rw-r--r-- 1 root root 1334 Oct 6 22:20 server.crt
-rw-r--r-- 1 root root 1704 Oct 6 22:20 server.key