git 配置多个SSH-Key

     咱们在平常工做中会遇到公司有个gitlab,还有些本身的一些项目放在github上。这样就致使咱们要配置不一样的ssh-key对应不一样的环境。下面咱们来看看具体的操做:git

 

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

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

 

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

 

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

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

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

 

3,添加私钥

$ ssh-add ~/.ssh/id_rsa $ ssh-add ~/.ssh/github_rsa

若是执行ssh-add时提示"Could not open a connection to your authentication agent",能够现执行命令:bash

$ ssh-agent bash

而后再运行ssh-add命令。服务器

# 能够经过 ssh-add -l 来确私钥列表
$ ssh-add -l
# 能够经过 ssh-add -D 来清空私钥列表
$ ssh-add -D

 

 

4,修改配置文件

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

touch config

添加内容:ide

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

5,目录结构

6,测试

$ ssh -T git@github.com

输出gitlab

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

就表示成功的连上github了.也能够试试连接公司的gitlab.spa

相关文章
相关标签/搜索