git如何配置多个ssh-key

我们在日常工作中会遇到公司有个git,还有些自己的一些项目放在github上。这样就导致我们要配置不同的ssh-key对应不同的环境。下面我们来看看具体的操作:

一、生成一个公司用的SSH-Key

$ ssh-keygen -t rsa -C “[email protected]” -f ~/.ssh/id_rsa

在~/.ssh/目录会生成id-rsa和id-rsa.pub私钥和公钥。 我们将id-rsa.pub中的内容粘帖到公司gitlab服务器的SSH-key的配置中。

二、生成一个github用的SSH-Key

$ ssh-keygen -t rsa -C “[email protected]” -f ~/.ssh/github_id_rsa

在~/.ssh/目录会生成github-rsa和github-rsa.pub私钥和公钥。 我们将github_id_rsa.pub中的内容粘帖到github服务器的SSH-key的配置中。

三、添加私钥

$ ssh-add ~/.ssh/id_rsa

$ ssh-add ~/.ssh/github_id_rsa

如果执行ssh-add时提示”Could not open a connection to your authentication agent”,可以现执行命令:

$ ssh-agent bash

扫描二维码关注公众号,回复: 3782746 查看本文章

然后再运行ssh-add命令。

  • 可以通过 ssh-add -l 来确私钥列表
    $ ssh-add -l

  • 可以通过 ssh-add -D 来清空私钥列表
    $ ssh-add -D

四、修改配置文件

在 ~/.ssh 目录下新建一个config文件

touch config

添加内容:

# company
Host
HostName
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

五、目录结构

这里写图片描述

六、测试

$ ssh -T [email protected]

输出
Hi David-Kuper! You’ve successfully authenticated, but GitHub does not provide shell access.

就表示成功的连上github了.也可以试试链接公司的git.

原文出处:git 配置多个SSH-Key

猜你喜欢

转载自blog.csdn.net/woshimalingyi/article/details/62050440