场景1:

1.公司有自己的gitlab,公司代码都是往这上面提 ,账号:john
2.自己平时学习的时候,代码是提交到github,账号:tom
为了同时支持这两种操作,一台电脑需要为不同的网站的用户各自创建单独的key。

解决办法

1.创建新的ssh key

$ ssh-keygen -t rsa -C “tom@gmail.com”    //github的用户
$ ssh-keygen -t rsa -C “john@gmail.com”   //自建gitlab的用户
假设得到下面两对key,创建key的时候,指定key的名字,我们这里用用户名区分:
~/.ssh/id_rsa_tom
~/.ssh/id_rsa_tom.pub
~/.ssh/id_rsa_john
~/.ssh/id_rsa_john.pub

2.添加key到ssh中

可以先删除key缓存(可选):
$ ssh-add -D
如果报错:“Could not open a connection to your authentication agent”,则执行下面的命令,指定shell为bash
$ exec ssh-agent bash
然后添加新key到ssh中:
$ ssh-add ~/.ssh/id_rsa_tom
$ ssh-add ~/.ssh/id_rsa_john
查看已经添加的key:
$ ssh-add -l
显示:
2048 SHA256:NkAp2fRsCp0S4a+I2st5Pz3f77u/NT7Bf+RlDAPYBxw tom@gmail.com (RSA)
2048 SHA256:LVYyVP7WnFhKYmAY7w2/2BQqj/jrKyuKWiyBC8Os00I join@gmail.com (RSA)
上面的输出结果,可以看到成功将两个key都添加到了ssh中。

3.将ssh公钥添加到gitlab或github

需要要新增加的公钥添加到gitlab或github中,这个步骤应该所有人都知道,这里略过。

4.配置ssh的config文件

$ cd ~/.ssh/
$ touch config
$ vi -a config
配置内容如下:
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_tom


Host gitlab.com
    #假设alex.gitlab.com是公司的gitlab域名
    HostName alex.gitlab.com    
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_john
    
    
#如果是自建的gitlab,则hostName设置为ip,Port设置为端口号 ;如果gitlab是默认ssh端口,则可以省略端口,如下:
#Host 192.168.177.231    
#   HostName 192.168.177.231
#   Port 222     
#   PreferredAuthentications publickey
#   IdentityFile ~/.ssh/id_rsa_john

5.测试

// 使用tom的SSH key
git clone git@github.com:xxxx/helloproject.git
// 使用join的SSH key
git clone git@alex.gitlab.com:qqqq/worldproject.git
如果能成功拉下代码,应该就没问题了。
原因:ssh根据host来找对应的key配置信息,因为是两个不同的域名gitlab和github,所以可以正常工作

 

场景2

1.公司gitlab,账号:john
2.自己的github账号 ,  账号:tom1
3.另外一个github账号 ,  账号:tom2
这个时候,如果使用场景1的配置,那么就会面临一个问题,都是github的域名,如何区分用哪个key呢?
我们可以修改host的区分标识,增加账号名来进一步区分同一网站的不同账号。
解决办法:

1.为两个不同的网站的用户各自创建新的ssh key

$ ssh-keygen -t rsa -C “tom1@gmail.com”    //github的tom1
$ ssh-keygen -t rsa -C “tom2@gmail.com”    //github的tom2
$ ssh-keygen -t rsa -C “john@gmail.com”   //自建gitlab的用户,john
假设得到下面三对key,创建key的时候,指定key的名字,我们这里用用户名区分:
~/.ssh/id_rsa_tom1
~/.ssh/id_rsa_tom1.pub
~/.ssh/id_rsa_tom2
~/.ssh/id_rsa_tom2.pub
~/.ssh/id_rsa_john
~/.ssh/id_rsa_john.pub

2.添加key到ssh中

可以先删除key缓存(可选):
$ ssh-add -D
如果报错:“Could not open a connection to your authentication agent”,则执行下面的命令,指定shell为bash
$ exec ssh-agent bash   //
然后添加新key到ssh中:
$ ssh-add ~/.ssh/id_rsa_tom1
$ ssh-add ~/.ssh/id_rsa_tom2
$ ssh-add ~/.ssh/id_rsa_john
查看已经添加的key:
$ ssh-add -l
显示:
2048 SHA256:NkAp2fRsCp0S4a+I2st5Pz3f77u/NT7Bf+RlDAPYBxw tom1@gmail.com (RSA)
2048 SHA256:LVYyVP7WnFhKYmAY7w3/2BQqj/jrKyuKWiyBC8Os00I tom2@gmail.com (RSA)
2048 SHA256:LVYyVP7WnFwer3eYmBY7w2/2BQqj/jrKyuKWiyBC8Os00I join@gmail.com (RSA)
上面的输出结果,可以看到成功将3个key都添加到了ssh中。

3.将ssh公钥添加到gitlab或github

需要要新增加的公钥添加到gitlab或github中,这个步骤应该所有人都知道,这里略过。

4.配置ssh的config文件

$ cd ~/.ssh/
$ touch config
$ vi -a config
配置内容如下:
Host github.com-tom1
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_tom

Host github.com-tom2
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_tom

Host gitlab.com
    HostName alex.gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_john

 

5.修改项目的git配置文件

因为我们使用ssh的host进行区分,所以,还需要修改项目的git文件,指向我们配置的host,然后ssh就可以根据host配置找到对应的key。
对于已经拉下来的项目,就需要修改项目的git配置:
假设这是tom1的项目,则将:
[remote “origin”]
 url = git@github.com:xxxx/AnalysisSpringCode.git
改为:
[remote “origin”]
 url = git@github.com-tom1:xxxx/AnalysisSpringCode.git

6.测试

如果还没拉下来,则可以直接修改ssh的host:
// 使用tom1的SSH key
git clone git@github.com-tom1:xxxx/helloproject.git
// 使用tom2的SSH key
git clone git@github.com-tom2:ffff/helloproject.git
// 使用join的SSH key
git clone git@gitlab.com:qqqq/worldproject.git
如果能成功拉下代码,应该就没问题了。
或者是用下面的ssh命令测试,更便捷:
$ ssh -T git@github.com-tom1
Hi Tom1! You’ve successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@github.com-tom2
Hi Tom2! You’ve successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@alex.gitlab.com
Hi John! You’ve successfully authenticated, but GitHub does not provide shell access.

7.修改本地项目的user

假设为tom1的项目:
$ git config user.name “tom1”
$ git config user.email “tom1@gmail.com”
参考:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.