来源: 沉浸编程世界
文章作者: 林山
文章链接: http://blog.ls331.com/20190708/177ad436/
本文章著作权归作者所有,任何形式的转载都请注明出处。
为了安全性,个人的 GitHub 和公司的 GitLab 需要配置不同的 SSH-Key。
1.切换到系统的 ssh 目录
cd ~/.ssh
2.为个人的 GitHub 生成 SSH-Key(若还没有)
ssh-keygen -t rsa -C "your_mail@example.com" -f github_rsa
然后,前往 GitHub 添加 SSH公钥。
3.为公司的 GitLab 生成 SSH-Key(若还没有)
ssh-keygen -t rsa -C "your_mail@company.com" -f company_rsa
然后,前往 GitLab 添加 SSH公钥。
4.添加配置文件(若还没有)
touch config
5.为配置文件 config 添加如下内容
# github.com
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_rsa
# gitlab.company.com
Host gitlab.company.com
HostName gitlab.company.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/company_rsa
6.测试
ssh -T git@github.com
输出 : Hi YK-Unit! You’ve successfully authenticated, but GitHub does not provide shell access.
以上表示成功连接到了个人的 GitHub。
然后可以用同样方式测试公司的 GitLab。
7、如果需要多个账号绑定同个github或者其他仓库管理
生成密钥和对应的公钥方法不变
修改config配置,让后克隆或者绑定仓库的时候改下方式就好了
# Account 1 后面设置了账号
Host github.com-account1
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_account1
# Account 2
Host github.com-account2
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_account2
测试链接的时候
ssh -T git@github.com-account1
ssh -T git@github.com-account2
克隆的时候要指定用哪个账号
git clone git@github.com-account1:username/project.git
git clone git@github.com-account2:username/project.git