-★出爺;3 发表于 2018-1-11 09:20:26

CentOS7上GitHub/GitLab多帐号管理SSH Key

  由于公司团队使用 GitLab 来托管代码,同时,个人在 Github 上还有一些代码仓库,可公司邮箱与个人邮箱是不同的,由此产生的 SSH key 也是不同的,这就造成了冲突 ,文章提供此类问题的解决方案:如何在一台机器上面同时使用 Github 与 Gitlab 的服务?
  由于公司不允许访问外网22端口,所以我们可以改走443端口

Using SSH over the HTTPS port
  https://help.github.com/articles/using-ssh-over-the-https-port/
  测试https端口连通性
  

# ssh -T -p 443 git@ssh.github.com  

  修改ssh配置文件
  # vi ~/.ssh/config
  

Host github.com  
Hostname
ssh.github.com  
Port
443  

  然后通过以下方式测试
  

# ssh -T git@github.com  


1. 生成GitHub/GitLab 的 SSH Key
  

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "285006386@qq.com"  
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_gitlab -C "admin@example.com"
  

  

  检查key是否生成
  

$ ls ~/.ssh  


2. 添加private key
  

$ ssh-add ~/.ssh/id_rsa_github  
$ ssh-add ~/.ssh/id_rsa_gitlab
  

  如果执行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
  


3. 修改配置文件
  $ vi ~/.ssh/config
  

# github  
Host github.com
  
HostName github.com
  
PreferredAuthentications publickey
  
IdentityFile ~/.ssh/id_rsa_github
  

  
# gitlab
  
Host gitlab.example.com
  
HostName gitlab.example.com
  
PreferredAuthentications publickey
  
IdentityFile ~/.ssh/id_rsa_gitlab
  


4. 上传public key 到 GitHub/GitLab

GitHub设置过程如下:
  登录github,点击右上方的图标,点击“Settings”
https://images2015.cnblogs.com/blog/582266/201604/582266-20160415174952238-652344910.png
  选择“SSH and GPG keys”,点击“New SSH key”,在出现的界面中填写SSH key的名称,填一个你自己喜欢的名称即可,然后将上面拷贝的 ~/.ssh/id_isa_github.pub 文件内容粘帖到 key 一栏,在点击“Add SSH key”按钮就可以了。
https://images2015.cnblogs.com/blog/582266/201604/582266-20160415175142348-178809033.png
  添加过程github会提示你输入一次你的github密码 ,确认后即添加完毕。

GitLab设置过程如下:
  点击“Profile Settings”
https://images2015.cnblogs.com/blog/582266/201604/582266-20160415180018285-253386757.png
  点击“SSH Keys”,添加SSH key
https://images2015.cnblogs.com/blog/582266/201604/582266-20160415180024301-2058717205.png

5. 测试
  

# 测试github  
$ ssh -T git@github.com
  

  
# 测试gitlab
  
$ ssh -T git@gitlab.example.com
  
页: [1]
查看完整版本: CentOS7上GitHub/GitLab多帐号管理SSH Key