github 多账号使用ssh key

简介:

一直在用github的ssh key,大家都知道使用ssh key可以不用每次都输入用户名密码,一定程度上可以提高效率。

如果是单账户,很方便,默认拿id_rsa与你的 github服务器的公钥对比;

如果是多用户如user1,user2,在同一台服务器上,那么id_rsa_pub就不能用在user2的身上了,怎么办呢?

一、新建user1、user2的SSH Key

#新建user1 SSH key:

1
2
cd  ~/. ssh    
  ssh -keygen -t rsa -C  "user1@email.com"

一路回车即可;

wKiom1esNJrhhfsQAAM8CWgcQ5A919.png-wh_50

 # 新建user2的SSH key

1
#ssh-keygen -t rsa -C "user2@email.com"

# 设置名称为

1
2
Enter  file  in  which  to save the key ( /c/Users/Administrator/ . ssh /id_rsa ):
  /c/Users/Administrator/ . ssh /id_rsa_user2


wKioL1esNKyQQtlQAAMGLgTAq9U369.png-wh_50



二、

密钥添加到SSH agent中

因为默认只读取id_rsa,为了让SSH识别新的私钥,需将其添加到SSH agent中:

1
ssh -add  ~/. ssh /id_rsa_user2

如果出现Could not open a connection to your authentication agent的错误,就试着用以下命令:

1
2
ssh -agent   bash
ssh -add  ~/. ssh /id_rsa_user2

wKioL1esNSeDYsA3AAKFBOFqlfE099.png-wh_50


三、

修改config文件

 在~/.ssh目录下找到config文件,如果没有就创建:

1
touch  config

然后修改我的config配置如下:

如果存在的话,其实就是往这个config中添加一个Host:

#建一个github别名,新建的帐号使用这个别名做克隆和更新

其规则就是:从上至下读取config的内容,在每个Host下寻找对应的私钥。

这里将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名如:github2,那么原地址 是:git@github.com:test/Mywork.git,替换后应该是:github2:test/Mywork.git.


wKiom1esNXGAd3NxAAJP0YHN-r4715.png-wh_50


四、

打开新生成的~/.ssh/id_rsa_user2.pub文件,将里面的内容添加到GitHub后台。(第二个新账号)

第一个github帐号下的SSH Key中也要记得添加。(添加到第一个账号)

添加相信大家都会,不操作了


五、

测试

$ ssh -T git@github.com

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


 $ ssh -T git@github2

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

wKioL1esNdPgWqGgAAMYtAQvVIs627.png-wh_50


六、

应用

测试成功,尝试克隆1047353200账号下的远程仓库。如下:

$ git clone git@github2:xxxxxx/git-demo.git


此时,一个服务器,可以拉去不同仓库的代码、







      本文转自crazy_charles 51CTO博客,原文链接:http://blog.51cto.com/douya/1836975,如需转载请自行联系原作者



相关文章
|
22天前
|
Shell 网络安全 开发工具
github多用户ssh配置
在Windows 10环境下,当需使用多个GitHub账号进行开发操作时,可通过生成多个SSH密钥对并配置SSH `config` 文件实现。步骤包括:1) 使用`ssh-keygen`命令为每个账号生成密钥对,保存在`C:\Users\<USERNAME>\.ssh`目录;2) 编辑`config`文件,设定每个Host对应的身份文件和用户信息,如果需要,配置代理;3) 验证通过`git clone`不同仓库来确保配置正确。
26 0
|
1月前
|
安全 Shell 网络安全
GitHub SSH 快速配置
这是一个自动化脚本,用于简化设置 Git SSH 连接的过程。功能包括:设置 Git 用户名和邮箱、生成新的 SSH 密钥、将 SSH 私钥添加到 ssh-agent、可选复制 SSH 公钥到剪贴板、提供 GitHub 配置指引以及测试 SSH 连接。用户需确保安装 Git 和 SSH 工具,然后下载脚本并赋予执行权限,按照提示操作即可。适合 Git 初学者或需要快速配置 SSH 的用户。
|
1月前
|
安全 网络安全
jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 如何处理
【5月更文挑战第24天】jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 如何处理
68 1
|
10天前
|
安全 网络安全 开发工具
【GIT】GitHub添加ssh密钥
【GIT】GitHub添加ssh密钥
20 0
|
1月前
|
算法 网络安全
Unable to negotiate with 127.0.0.1 port 29215: no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 解决
【5月更文挑战第5天】Unable to negotiate with 127.0.0.1 port 29215: no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 解决
89 7
|
1月前
|
弹性计算 运维 Shell
基于key验证多主机ssh访问
【4月更文挑战第30天】
28 1
|
1月前
|
安全 网络安全
jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha>问题处理方法
【5月更文挑战第10天】jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha>问题处理方法
130 0
|
1月前
|
算法 网络安全
no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 问题解决
【5月更文挑战第8天】no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 问题解决
40 0
|
1月前
|
存储 弹性计算 算法
SSH学习(三)- SSH协议中的Public Key Algorithm
在SSH协议中,有两个地方涉及到公钥算法,分别是: 1. 服务端认证:服务端在进行密钥协商的时候证明自己身份,防止中间人攻击,此时为SSH-TRANS协议发生的事情; 2. 客户端认证:客户端通过PublicKey方式证明自己身份,完成SSH登录认证,此时SSH-USERAUTH发生的事情; 这两种情况下的公钥算法使用的是同一个概念,接下来本文将主要基于PublicKey公钥认证方式,学习对应的内容。
120 1
SSH学习(三)- SSH协议中的Public Key Algorithm
|
1月前
|
前端开发 NoSQL 数据库
如何设计 QQ、微信、微博、Github 等等,第三方账号登陆 ?(附表设计)
如何设计 QQ、微信、微博、Github 等等,第三方账号登陆 ?(附表设计)
36 1