Skip to content

[SSH]生成密钥

查询

验证本地是否已存在密钥

$ cd ~/.ssh/

查看是否存在id_dsaid_rsa命名的文件,其中.pub文件是公钥

生成

在本地生成密钥,使用ssh-keygen进行,默认生成id_rsa文件在~/.ssh文件夹内

$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/zj/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/zj/.ssh/id_rsa.
Your public key has been saved in /home/zj/.ssh/id_rsa.pub.
The key fingerprint is:
...
...

其中会要求你输入两次密码,也可以为空

$ ls
id_rsa  id_rsa.pub  ...

自定义

指定密钥加密算法、密钥长度、密钥标签、密钥文件名

$ ssh-keygen -t rsa -b 4096 -C "github.com" -f ~/.ssh/github_id_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/zj/.ssh/github_id_rsa.
Your public key has been saved in /home/zj/.ssh/github_id_rsa.pub.
The key fingerprint is:
SHA256:WRZktNSVoY+a5hAgNwou
...
...
  • -t指定加密算法,当前设置为rsa加密算法
  • -b指定密钥长度
  • -C指定了标签
  • -f指定生成文件名

当你已经存在github_id_rsa/github_id_rsa.pub文件时,会提示你是否重载

重设密钥

重新设置密钥密码

$ ssh-keygen -p
Enter file in which the key is (/home/zj/.ssh/id_rsa):  # 默认是id_rsa文件,点击Enter键;否则,输入正确文件地址
Enter old passphrase:                                   # 输入旧密码
Enter new passphrase (empty for no passphrase):         # 输入新密码
Enter same passphrase again:                            # 重复新密码
Your identification has been saved with the new passphrase.

或者加上参数-f指定要修改的文件

$ ssh-keygen -p -f /home/zj/.ssh/github_id_rsa
Enter old passphrase: 
Enter new passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved with the new passphrase.

或者加上参数-P指定旧密码、-N指定新密码

$ ssh-keygen -p -P zhujian -N 123456 -f ~/.ssh/github_id_rsa
Your identification has been saved with the new passphrase.

相关阅读