1. 生成一个公司(gitlab)用的SSH-Key
ssh-keygen -t rsa -C "youremail@yourcompany.com" -f ~/.ssh/id_rsa
2. 生成一个github用的SSH-Key
ssh-keygen -t rsa -C "youremail@yourcompany.com" -f ~/.ssh/id_rsa_github
在~/.ssh/目录会生成id_rsa和id_rsa_github私钥和公钥(pub是公钥)。
将id_rsa.pub的内容粘贴到公司gitlab的SSH-key的配置中。
我们将id_rsa_github.pub中的内容粘帖到github服务器的SSH-key的配置中。
3. 添加私钥
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_github
如果执行ssh-add时提示"Could not open a connection to your authentication agent",可以现执行命令:
ssh-agent bash
然后再运行ssh-add命令。
# 可以通过 ssh-add -l 来确私钥列表
$ ssh-add -l
# 可以通过 ssh-add -D 来清空私钥列表
$ ssh-add -D
4. 修改配置文件
在 ~/.ssh 目录下新建一个config文件
touch config
vim编辑,添加内容
# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_rsa
5. 测试
ssh -T git@github.com
ssh -T git@gitlab.com
成功的话会有提示信息
测试github出现超时的情况时,参考 gist.github.com/Tamal/1cc77…