同一台电脑管理 多个ssh公钥和私钥

默认情况下,我们在本地电脑生成的密钥都是 id_rsa 和 id_rsa.pub ,git 默认情况下也只会读取这个私钥,所以我们需要修改一些配置来支持多个SSH Key。

本文基于Linux系统,Windows系统类似

ljh@pc:~/.ssh$ ssh-keygen -t rsa -C "xxx@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ljh/.ssh/id_rsa): id_rsa_gitlab

第二步:

ssh-agent是一个私钥的管理工具,当我们需要通过不同的私钥去连接不同的服务器时,需要我们手动输入私钥密码,ssh-agent可以免去这项工作,那么我们需要将新的key 添加到ssh-agent里面管理,可以通过ssh-add命令,首先我们看下里面有哪些key:

ljh@pc:~/.ssh$ ssh-add -l
2048 SHA256:xxx /home/ljh/.ssh/id_rsa (RSA)

默认情况下id_rsa已经存在里面,那么我们此时应该把上面新建的key也添加到里面:

ljh@pc:~$ ssh-add /home/ljh/.ssh/id_rsa_gitlab
Identity added: /home/ljh/.ssh/id_rsa_gitlab (/home/ljh/.ssh/id_rsa_gitlab)

第三步:

在.ssh/ 目录里创建一个config文件,通过配置信息告诉git什么情况下使用新的key:

ljh@pc:~/.ssh$ touch config
ljh@pc:~/.ssh$ vim config

添加下面内容:

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

# gitlab
Host gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
User ljh

User填写的是gitlab账号。

 

第四步:

测试:

ssh -T git@gitlab.com

如果提示如下 请输入 yes 后回车:

The authenticity of host 'gitlab.com (13.250.177.223)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no)?

连接成功会返回:

You've successfully authenticated, but GitHub does not provide shell access.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值