Git配置多个SSH-Key

背景

当有多个git账号时,比如:

a. 一个gitee,用于公司内部的工作开发;
b. 一个github,用于自己进行一些开发活动;

解决方法

1.生成一个公司用的SSH-Key
$ ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitee_id_rsa
2.生成一个github用的SSH-Key
$ ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/github_id_rsa

注:[email protected][email protected]可以任意与git使用没必然关系。

3.在 ~/.ssh 目录下新建一个config文件(无文件后缀),添加如下内容(其中Host和HostName填写git服务器的域名,IdentityFile指定私钥的路径)
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
4.用ssh命令分别测试
$ ssh -T [email protected]
$ ssh -T [email protected]

注:如果测试出现 Permission denied (publickey)错误,是因为没有按步骤3配置。如下图

github连接出错.png

这里以github为例,成功的话会返回下图内容


github连接成功.png

你可能感兴趣的:(Git配置多个SSH-Key)