@kinghan
2015-02-12T07:09:47.000000Z
字数 885
阅读 2268
多个 ssh-key github/gitlab 多个管理
很多时候我们同时维护着github上自己的开源库和公司内网的gitlab的工作库, 大部分情况下两个系统的工作账号都是不同的, 为了避免在提交代码的时候每次都输入用户名和密码, 我们需要在自己的电脑上配置ssh的private key 与 public key, 如何让两对不同的ssh-key能同时生效就成了提升工作效率的最大问题, 接下来就着手解决这个问题
运行ssh-keygen -t rsa -C "your eamil"会默认在主目录下面的.ssh目录下生成id_rsa与id_rsa.pub密钥对,为了避免我们添加的第二个密钥对覆盖第一个, 在生成密钥的过程中我们输入自定义密钥的名字,如下所示:

进入.ssh目录查看,确认是否生成新的密钥对, 如下所示:

运行ssh-add ~/.ssh/id_rsa_xxx, 添加上面的生成的私钥
如果提示"Could not open a connection to your authentication agent", 需要先运行ssh-agent bash,然后再运行上面命令
运行ssh-add -L来确认私钥列表
ssh-add相关命令可以通过ssh-add --help来获取
在~/.ssh目录下创建config文件,内容如下所示:
#git.moretv.cnHost git.moretv.com.cnHostName git.moretv.com.cnPreferredAuthentications publickeyIdentityFile ~/.ssh/id_rsa#githubHost github.comHostName github.comPreferredAuthentications publickeyIdentityFile ~/.ssh/id_rsa_github
运行ssh -T git@git.moretv.cn测试连通性, 出现欢迎信息(如下)则标识配置成功

作者: king.han
邮箱: steven_hl@163.com
2015 年 02 月 12 日
