One - One Code All

Blog Content

Git配置多个SSH key

Linux-Mac   2018-02-21 19:43:10

Git配置多个SSH key


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

$ ssh-keygen -t rsa -C "1email@company.com” -f ~/.ssh/id_rsa


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

$ ssh-keygen -t rsa -C "2email@github.com” -f ~/.ssh/github_rsa


此时,.ssh目录下应该有4个文件:id_rsa和id_rsa.pub,github_rsa和github_rsa.pub,分别将他们的公钥文件(id_rsa.pub,github_rsa.pub)内容配置到对应的code仓库上



配置公钥:登录github或你的代码托管平台。右上角你的账号登录个人信息处,点击settings,点击SSH-keys,最后点击Add key 保存即可。


3,添加私钥

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

如果执行ssh-add时提示”Could not open a connection to your authentication agent”,


可以先执行命令:

$ ssh-agent bash


然后再重新运行ssh-add命令:

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



添加后我们可以通过 ssh-add -l 来确私钥列表

$ ssh-add -l


如果想删除私钥列表,可以通过 ssh-add -D 来清空私钥列表

$ ssh-add -D


4,修改配置文件


若.ssh目录(就是私钥所在的文件夹)下无config文件,那么创建

touch config


在config文件添加以下内容



# gitlab

Host github.com

Port 22

HostName github.com

PreferredAuthentications publickey

IdentityFile C:/Users/xiaohaozi/.ssh/github-rsa

User xiaohaozi

 

# smartgit

Host smartgit

HostName smartgit

PreferredAuthentications publickey

IdentityFile C:/Users/xiaohaozi/.ssh/id_rsa

User xiaohaozi

# 配置文件参数

# Host : Host可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和ssh文件(可以直接填写ip地址)

# HostName : 要登录主机的主机名(建议与Host一致)

# User : 登录名(如gitlab的username)

# IdentityFile : 指明上面User对应的identityFile路径

# Port: 端口号(如果不是默认22号端口则需要指定)


5.测试

$ ssh -T git@github.com

 输出

Welcome to GitLab, your name!

则说明成功了。


也可以在自己的github上创建一个project之后,再git bash中git clone 一下进行测试。



上一篇:Selenium+Headless Firefox/chrome
下一篇:springboot 中手动向pom.xml加入依赖时 如何让Maven 自动更新包

The minute you think of giving up, think of the reason why you held on so long.