多个 git ssh key 配置 Ubuntu os

1.生成ssh key:

ssh-keygen -t rsa -C “email@sss.com”

此时,在~/.ssh/文件夹下会有两个文件, id_rsa 和 id_rsa.pub。分别保存ssh 的密钥和公钥。

2.把id_rsa.pub里面的内容复制到gitlab服务器内个人账号下的ssh_key部分。

3.在本地添加密钥:

ssh-add ~/.ssh/id_rsa 

若执行ssh-add ....是出现这个错误:Could not open a connection to your authentication agent,则先执行如下命令即可:

ssh-agent bash

再执行add命令

这步在linux下似乎是必要的,不添加这步会导致识别不了账号密码。在windows下则不需要这一步。

4.从git服务器下载工程:

git clone git@ip:author/repo

如果需要从多个个gitlab上获取文档,则在~/.ssh/ 文件夹下需要有多个 公/私钥 key pair。

使用keygen命令,继续创建键值对:

ssh-keygen -f gitlabkey -t rsa -N ''

-f 新的文件名称

如果不设置-C参数,则默认名称为 用户名@主机名

之后在文件夹下会有两份文件 gitlabkey.pub 和 id_rsa.pub,这时候需要一个配置文件来告诉git使用哪个秘钥。

在.ssh目录下新建文件config,内容如下:

Host localhost
    IdentityFile /home/shida/.ssh/gitlabKey

这条记录表明,如果要访问在localhost上的gitlab,则使用gitlabKey名称的钥匙。

其他的gitlab默认使用id_rsa。

原文地址:https://www.cnblogs.com/starRebel/p/5208536.html