git 配置多个SSH keys

来源:互联网 发布:安徽秉知装饰有限公司 编辑:程序博客网 时间:2024/04/27 21:48

背景

工作中公司用到了gitlab,而个人开发一些项目/工程使用的是github

配置

一般第一个SSH Keys大家都知道怎么配置,所以这里就不说第一个的配置了,主要是讲讲如何配置第二个。

步骤

生成一个自定义用的SSH-Key(这里我生成github的)

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

在~/.ssh/目录会生成github-rsa和github-rsa.pub私钥和公钥。 我们将github-rsa.pub中的内容粘帖到github服务器的SSH-key的配置中(利用命令 cat gitbub-rsa-pub 打开)

添加私钥

$ ssh-add ~/.ssh/github_rsa

如果执行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

修改配置文件

在 ~/.ssh 目录下新建一个config文件

touch config

添加内容
添加操作:用 vi file.name 打开文件,输入,然后按 esc键,输入:wq保存退出。

#gitlabHost gitlab.com    HostName gitlab.com    PreferredAuthentications publickey    IdentityFile ~/.ssh/id_rsa#githubHost github.com    HostName github.com    PreferredAuthentications publickey    IdentityFile ~/.ssh/github_rsa

测试

$ ssh -T git@github.com

输出

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

就表示成功的连上github了.也可以试试链接公司的gitlab.

原创粉丝点击