怎么给Git配置多个SSH Key?

有时候咱们可能须要在同一台电脑上配置多个SSH Key,好比公司项目使用的是GitHub,我的开发用的是码云Gitee。这个时候咱们可能须要有两个SSH Key,怎么配置呢?git

假设你以前已经生成了一个GitHubSSH Key,能够用命令cat ~/.ssh/id_rsa.pub查看已经生成的SSH Key:github

GitHub的SSH Key

复制命令ssh-keygen -t rsa -C 'xxxxx@youremail.com' -f ~/.ssh/gitee_id_rsa生成一个GiteeSSH Key,一路回车就能够了(记得把邮箱改为你本身的)。能够看到.ssh文件夹下面多了两个文件。服务器

.ssh文件夹

使用命令cat ~/.ssh/gitee_id_rsa.pub查看GiteeSSH Key,复制ssh开头的那一串公钥,添加到Gitee仓库。ssh

Gitee的SSH Key


使用命令touch ~/.ssh/config,在~/.ssh文件夹下添加config文件,能够看到文件夹下面多了一个config文件。

测试

右键使用记事本打开,复制如下信息添加到config文件保存,其中HostHostName填写git服务器的域名,IdentityFile填写私钥的路径。spa

# 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/id_rsa

使用如下命令分别测试GitHubGitee,查看SSH Key是否添加成功。3d

ssh -T git@gitee.com
ssh -T git@github.com

看到如下的提示,就表示添加成功,能够拉取、推送代码了。
code

相关文章
相关标签/搜索