openssl是一个开源软件工具,实现了基本的加密功能,实现了SSL和TSL协议。 一般的Linux和MacOS都会自带,如果没有先安装。
生成证书
使用以下命令 MyCertificate.crt代表证书名,MyKey代表密钥名。可以改成任意的名称。
root@localhost:~ openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out MyCertificate.crt -keyout MyKey.key
- -newkey rsa:4096:创建一个4096位的RSA密钥。
- -x509:创建一个自签名证书。
- -sha256:使用256位的SHA算法生成证书。
- -days:证书的有效期限。
- -nodes:创建的证书不需要密码。如果不加这个参数,每次应用密码证书的时候都需要人为的输入密码。
- -out 生成证书的路径及名称。
- -keyout 生成密钥的路径及名称。
执行上面的命令后,会出现以下提示。依次填入信息回车即可。
Generating a 4096 bit RSA private key
..............................................................................+++
..............................................+++
writing new private key to 'MyKey.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) [AU]:CN
State or Province Name (full name) [Some-State]:HUBEI
Locality Name (eg, city) []: WUHAN
Organization Name (eg, company) : MyCompany
Organizational Unit Name (eg, section) []:Docs
Common Name (e.g. server FQDN or YOUR name) []:hostname.example.com
Email Address []:admin@example.com
Country Name:国家码 State or Province Name:省份 Locality Name:城市 Origanization Name:机构名称,一般填入公司名 Origanizational Unit Name:机构单位,一般填入行业 Common Name:需要证书的域名或ip Email Address:电子邮箱
如果嫌一个一个输入麻烦,可以使用参数-subj
openssl -subj "/C=CN/ST=HB/L=WH/O=My Company/OU=EDU/CN=hostname.example.com/emailAddress=2afef@163.com"
注意按照上面输入的顺序。
安全
安全起见,最好只让root用户才能访问,并且是只读的。
chmod 400 MyKey.key
还需要记得将证书和key备份到其他安全的地方。