一台电脑如何管理多个SSH KEY
2016-03-31 16:24
309 查看
关于ssh是什么?
直接贴链接:http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html
需求:一台电脑上(Mac os)管理多个ssh key,可以任意切换,达到多用户(账号)使用不同ssh提交代码。
以下利用gerrit和github账号来做例子。
若一路回车(密码可以不写),这样只会在~/.ssh/ 目录下生成 id_rsa 和 id_rsa.pub 两个文件。为了区分,我们在第一个回车后设置路径:
由此我们分别为gerrit和github生成对应的公钥和私钥,完成后的目录:
id_rsa_gerrit
id_rsa_gerrit.pub
id_rsa_github
id_rsa_github.pub
若系统已经有ssh-key 代理 ,可以删除
当然也可以利用nano命令来创建和编辑
如此,ssh就会根据登陆的不同域,来读取对应的私钥文件
则表示成功。
若出现
请检查github的ssh管理里添加的公钥是否正确。
直接贴链接:http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html
需求:一台电脑上(Mac os)管理多个ssh key,可以任意切换,达到多用户(账号)使用不同ssh提交代码。
以下利用gerrit和github账号来做例子。
1、生成ssh key
ssh-keygen -t rsa -C "youremail@yourcompany.com"
若一路回车(密码可以不写),这样只会在~/.ssh/ 目录下生成 id_rsa 和 id_rsa.pub 两个文件。为了区分,我们在第一个回车后设置路径:
Enter file in which to save the key (/root/.ssh/id_rsa):~/.ssh/文件名
由此我们分别为gerrit和github生成对应的公钥和私钥,完成后的目录:
id_rsa_gerrit
id_rsa_gerrit.pub
id_rsa_github
id_rsa_github.pub
2、设置ssh key的代理
1、 首先查看代理
ssh-add -l若提示
Could not open a connection to your authentication agent.则系统代理里没有任何key,执行如下操作
exec ssh-agent bash
若系统已经有ssh-key 代理 ,可以删除
ssh-add -D
2、 添加私钥
ssh-add ~/.ssh/id_rsa_gerrit ssh-add ~/.ssh/id_rsa_github
3、添加公钥
在对应的gerrit和github的ssh管理页面,添加对应的公钥(.pub 文件内容),保存到代码管理服务器。4、添加和编辑配置文件config
在 ~/.ssh 目录下新建一个config文件touch ~/.ssh/config添加内容
# gerrit Host gerrit.xxxx.com HostName gerrit.xxxx.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_gerrit user gerrit # github Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_github user git
当然也可以利用nano命令来创建和编辑
nano ~/.ssh/config
如此,ssh就会根据登陆的不同域,来读取对应的私钥文件
5、测试
ssh -T git@github.com若出现
Hi XXX! You've successfully authenticated, but GitHub does not provide shell access.
则表示成功。
若出现
permission denied (publickey)
请检查github的ssh管理里添加的公钥是否正确。
6、其他
提交代码到gerrit失败,报错为remote: ERROR: In commit xxxxxxxxxxxxxxxxxxxxxxxx remote: ERROR: committer email address aaaaa@aaa.com remote: ERROR: does not match your user account. remote: ERROR: remote: ERROR: The following addresses are currently registered: remote: ERROR: bbbbbbb@bbbbbb.com remote: ERROR: remote: ERROR: To register an email address, please visit: remote: ERROR: http://xxxx/xxxx[/code]此报错并非你之前设置管理多个ssh出现问题,而是因为你当前git用户信息和你提交代码的服务器注册的用户信息不一致,即服务器检查了你的用户信息,进行了拒绝。
解决方法如下:
step1、 编辑.gitconfig
打开~/.gitconfig文件
将原来的
[user]
name = aaaaa
email = aaaaa@aaa.com
改成你当前的
name = bbbbb
email = bbbbb@ bbb.com
step2、 更改提交git commit --amend --author 'bbbbb <bbbbb@ bbb.com>'当然你也可以这样重新提交git reset HEAD^ git add -A git commit -m “xxxx”
相关文章推荐
- 第一周学习笔记
- objective-c类别和类扩展学习笔记
- nyoj 118 修路方案(最小生成树删边求多个最小生成树)
- Qos
- 再生龙u盘制作及使用 - Linux - clonezilla
- 学习OpenGL(三)绘制点
- Unix Study之--AIX安装和配置SSH
- CTF——啥
- jsp#1简单jsp开发
- TREEset的模板类
- Protocol Buffer技术详解(语言规范)
- 动态定义二维数组
- 不会做
- 排15个数
- 简单方法实现手势解锁
- iOS小知识点(非UI部分)
- hdu 2222 Keywords Search AC自动机模板题
- yum源配置
- 软考信息系统监理师,2016年3月25日作业
- 15个数排序(1)