免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1151 | 回复: 0
打印 上一主题 下一主题

[其他] Git实现多ssh key配置 [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-12-13 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-12-11 09:36 |只看该作者 |倒序浏览
我们在日常工作中会遇到公司有个gitlab,还有些自己的一些项目放在github上。这样就导致我们要配置不同的ssh-key对应不同的环境。下面我们来看看具体的操作:




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

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




在~/.ssh/目录会生成id-rsa和id-rsa.pub私钥和公钥。 我们将id-rsa.pub中的内容粘帖到公司gitlab服务器的SSH-key的配置中。




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

1  $  ssh -keygen -t rsa -C "youremail@your.com” -f ~/. ssh /github-rsa  

在~/.ssh/目录会生成github-rsa和github-rsa.pub私钥和公钥。 我们将github-rsa.pub中的内容粘帖到github服务器的SSH-key的配置中。




3,添加私钥

1
2
3
4
5
6
7
8  $  ssh -add ~/. ssh /id_rsa  $  ssh -add ~/. ssh /id_rsa_github
#如果执行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文件

1  touch  config  

添加内容:

1
2
3
4
5
6
7
8
9
10  # gitlab
Host gitlab.com
     HostName gitlab.com
     PreferredAuthentications publickey
     IdentityFile ~/. ssh /id_rsa
# github
Host github.com
     HostName github.com
     PreferredAuthentications publickey
     IdentityFile ~/. ssh /id_rsa_github  

5,目录结构




6,测试

1  $  ssh  -T git@github.com  

输出

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

就表示成功的连上github了.也可以试试链接公司的gitlab.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP