SSH Key生成
在通过ssh-keygen命令生成Key的时候,当提示Enter file in which to save the key时,为不同的Key添加不同的后缀名,例如:
- 用于GitHub的Key
id_rsa_github。 - 用于Bitbucket的Key
id_rsa_bitbucket。
使用密钥管理器
执行ssh-add命令将私钥添加至ssh-agent:
1 | ssh-add ~/.ssh/id_rsa_github |
这一步有可能出现Could not open a connection to your authentication agent.错误,解决方法如下:
- 执行
ps aux | grep ssh命令,查看ssh-agent的线程PID。 - 执行
kill -9 PID杀死线程。 进入
.ssh目录,执行以下命令:1
2exec ssh-agent bash
eval ssh-agent -s重新执行
ssh-add命令,将私钥添加至ssh-agent。
创建config文件
在.ssh目录下新建一个config文件,编辑文件内容:1
2
3
4
5
6
7
8
9
10
11
12
13# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
User Yourname
# bitbucket
Host bitbucket.com
HostName bitbucket.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_bitbucket
User Yourname