1、生成一个公司用的SSH-Key
1  | ssh-keygen -t rsa -C "youremail@yourcompany.com” -f ~/.ssh/id-rsa  | 
在~/.ssh/目录会生成id-rsa和id-rsa.pub私钥和公钥。 我们将id-rsa.pub中的内容粘帖到公司gitlab服务器的SSH-key的配置中。
2、 生成一个github用的SSH-Key
1  | ssh-keygen -t rsa -C "youremail@your.com” -f ~/.ssh/github-rsa  | 
在~/.ssh/目录会生成github-rsa和github-rsa.pub私钥和公钥。 我们将github-rsa.pub中的内容粘帖到github服务器的SSH-key的配置中。
3、添加私钥
1  | ssh-add ~/.ssh/id_rsa $ ssh-add ~/.ssh/github_rsa  | 
如果执行ssh-add时提示”Could not open a connection to your authentication agent”,可以现执行命令:1
 ssh-agent bash
然后再运行ssh-add命令。
可以通过 ssh-add -l 来确私钥列表1
 ssh-add -l
可以通过 ssh-add -D 来清空私钥列表1
 ssh-add -D
4、修改配置文件
在 ~/.ssh 目录下新建一个config文件1
touch config
添加内容:1
2
3
4
5
6
7
8
9
10 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、测试
1  | ssh -T git@github.com  | 
输出
Hi stefzhlg! You’ve successfully authenticated, but GitHub does not provide shell access.