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

如何在一台机器上面同时使用 Github 与 Gitlab 的服务?

生成GitHub/GitLab 的 SSH Key

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

检查key是否生成,执行下面的命令,有id_rsa_githubid_rsa_gitlab以及他们的公钥文件就是成功了。

$ ls ~/.ssh

添加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

修改配置文件

$ 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

上传public key 到 GitHub/GitLab

GitHub设置过程如下

登录github,点击右上方的图标,点击“Settings”

选择“SSH and GPG keys”,点击“New SSH key”,在出现的界面中填写SSH key的名称,填一个你自己喜欢的名称即可,然后将上面拷贝的 ~/.ssh/id_isa_github.pub 文件内容粘帖到 key 一栏,在点击“Add SSH key”按钮就可以了。

GitLab设置过程如下:

点击“Profile Settings”

点击“SSH Keys”,添加SSH key 

测试

添加过程github会提示你输入一次你的github密码 ,确认后即添加完毕。

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



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值