我们经常会遇到需要访问github仓库,又要访问gitlab仓库,但是ssh key切换太麻烦,可以通过配置config文件来执行多个仓库ssh密钥的定向指向。
1、在 ~/.ssh 目录下新建config文件
[plain] view plain copy
- touch config
2、新建gitlab和github文件目录
[plain] view plain copy
- mkdir gitlab
- mkdir github
3、在config文件中添加编辑配置
[plain] view plain copy
- sudo vim config
[plain] view plain copy
- Host gitlab
- HostName gitlab.com
- User *****@**.com
- IdentityFile ~/.ssh/gitlab/id_rsa
- Host oschina
- HostName oschina.net
- User *****@**.com
- IdentityFile ~/.ssh/oschina/id_rsa
- Host github
- HostName github.com
- User *****@**.com
- IdentityFile ~/.ssh/github/id_rsa
Host是自己的辨认标识,可以随便写
HostName是仓库的host地址
User是仓库账户邮箱
IdentityFile 是对应的密钥存储的路径
将以上信息保存。
将各自密钥放置各自的文件目录下即可。
====================
https://www.cnblogs.com/BeginMan/p/3548139.html
多github帐号的SSH key切换
我有两个github帐号,一个是个人所用,一个是为公司项目所用。如果是单用户(single-user),很方便,默认拿id_rsa与你的github服务器的公钥对比;如果是多用户(multi-user)如user1,user2,那么就不能用在user2的身上了,这个时候就要配置一下了:
1、新建user2的SSH Key
#新建SSH key:
$ cd ~/.ssh # 切换到C:\Users\Administrator\.ssh
ssh-keygen -t rsa -C "mywork@email.com" # 新建工作的SSH key
# 设置名称为id_rsa_work
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_work
2、新密钥添加到SSH agent中
因为默认只读取id_rsa,为了让SSH识别新的私钥,需将其添加到SSH agent中:
ssh-add ~/.ssh/id_rsa_work
如果出现Could not open a connection to your authentication agent的错误,就试着用以下命令:
ssh-agent bash
ssh-add ~/.ssh/id_rsa_work
3、修改config文件
在~/.ssh目录下找到config文件,如果没有就创建:
touch config # 创建config
然后修改如下:
我的config配置如下:
# 该文件用于配置私钥对应的服务器
# Default github user(first@mail.com)
Host github.com
HostName github.com
User git
IdentityFile C:/Users/Administrator/.ssh/id_rsa
# second user(second@mail.com)
# 建一个github别名,新建的帐号使用这个别名做克隆和更新
Host github2
HostName github.com
User git
IdentityFile C:/Users/Administrator/.ssh/id_rsa_work
如果存在的话,其实就是往这个config中添加一个Host:
#建一个github别名,新建的帐号使用这个别名做克隆和更新
Host github2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa2
其规则就是:从上至下读取config的内容,在每个Host下寻找对应的私钥。这里将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名如:github2,那么原地址是:git@github.com:funpeng/Mywork.git,替换后应该是:github2:funpeng/Mywork.git.
4、打开新生成的~/.ssh/id_rsa2.pub文件,将里面的内容添加到GitHub后台。
可不要忘了添加到你的另一个github帐号下的SSH Key中。
5、测试:
Administrator@FANGPENG /e/work
$ ssh -T git@github.com
Hi BeginMan! You've successfully authenticated, but GitHub does not provide shel
l access.
Administrator@FANGPENG /e/work
$ ssh -T github2
Hi funpeng! You've successfully authenticated, but GitHub does not provide shell
access.
6、应用
测试成功,那么我尝试在我的work目录下克隆我@126.com账号下的远程仓库。如下:
Administrator@FANGPENG /e/work
$ git clone github2:funpeng/Mywork.git
Cloning into 'Mywork'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done
克隆成功,大功告成了!
感谢参考:
3.git.md