git 配置多个SSH-Key

项目托管的仓库多了,使用的账号多了,自然用到的key就不同了,比如gitlab,bitbucket, github, 公司的code仓库等,所以管理好key很重要。

1,生成一个公司用的SSH-Key

$ ssh-keygen -t rsa -C "1email@company.com” -f ~/.ssh/id-rsa

2,生成一个github用的SSH-Key

$ ssh-keygen -t rsa -C "2email@github.com” -f ~/.ssh/github-rsa

此时,.ssh目录下应该有4个文件:id-rsa和id-rsa.pub,github-rsa和github-rsa.pub,分别将他们的公钥文件(id-rsa.pub,github-rsa.pub)内容配置到对应的code仓库上

3,添加私钥

$ ssh-add ~/.ssh/id-rsa 
$ ssh-add ~/.ssh/github-rsa

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

# 添加以下内容
# 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

# 输出
Welcome to GitLab, your name!
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值