1、 生成秘钥
$ cd ~/.ssh/
# 命令输入完,需自定义命名为:id_rsa_gitlab和id_rsa_github
ssh-keygen -t rsa -C "33999***@qq.com"
ssh-keygen -t rsa -C "33999***@company.com"
2、添加秘钥
# 把专用密钥添加到 ssh-agent 的高速缓存中:
ssh-add ~/.ssh/id_rsa_gitlab
ssh-add ~/.ssh/id_rsa_github
# 从ssh-agent中删除密钥:
ssh-add -d ~/.ssh/id_rsa_gitlab.pub
ssh-add -d ~/.ssh/id_rsa_github.pub
# 查看ssh-agent中的密钥:
ssh-add -l
注:如果在使用shh-add的时候提示:
Could not open a connection to your authentication agent.
则需手动开启ssh,如下;
ssh-agent bash
3、 将gitlab公钥即id_rsa.pub中的内容配置到gitlab和gitbub
4、 进入密钥生成的位置,创建一个config文件,添加配置
$ cd ~/.ssh/
# gitlab(1****64@qq.com)
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
User lvtong
# github(j****4@163.com)
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
User jackaroo2020
4、测试
$ cd ~/.ssh/
# $ ssh -T git@github.com
$ ssh -T git@gitlab.com
5. 用户级别配置
- 公司的代码使用频率较高,将git配置文件的global(用户级别)设置为公司的gitlab账号
$ git config --global user.name 'test' #公司账号名称
$ git config --global user.email 'test@companyName.com' #公司账号邮箱
- 仓库级别配置,将local(仓库级别)配置成github的账号
$ git config --local user.name 'test' #github账号名称
$ git config --local user.email 'test@test.com' #github账号邮箱
- 克隆代码
$ git clone git@github.com:test/demo.git