之前只使用一个 ssh key 在github上提交代码,因为工做缘由,须要再添加一个ssh key在公司的 gitlab上提交代码,下面记录下配置过程,防止遗忘。
说明下个人环境是 Win7 + msysgit + GitBash, 相信 *nux + bash 也是一样的道理。git
第一次使用ssh生成key,默认会在用户~(根目录)下生成 id_rsa, id_rsa.pub 2个文件;因此须要添加多个ssh key时也会生成对应的私钥和公钥。github
$ ssh-keygen -t rsa -C "youremail@yourcompany.com"
在Git Bash中执行这条命令一路回车,会在 ~/.ssh/ 目录下生成 id_rsa 和 id_rsa.pub 两个文件,用文本编辑器将 id_rsa_pub 中的内容复制一下粘贴到github(gitlab)上。shell
$ ssh-keygen -t rsa -C "youremail@gmail.com"
注意不要一路回车,要给这个文件起一个名字, 好比叫 id_rsa_github, 因此相应的也会生成一个 id_rsa_github.pub 文件。bash
目录结构以下:
ssh
$ 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命令。ide
# 能够经过 ssh-add -l 来确私钥列表 $ ssh-add -l # 能够经过 ssh-add -D 来清空私钥列表 $ ssh-add -D
在 ~/.ssh 目录下新建一个config文件gitlab
touch config
添加内容:测试
# 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
$ ssh -T git@github.com
输出
Hi user! You've successfully authenticated, but GitHub does not provide shell access. 就表示成功的连上github了spa