git 配置多个SSH-Key

栏目: 编程工具 · 发布时间: 4年前

内容简介:项目托管的仓库多了,使用的账号多了,自然用到的key就不同了,比如此时,具体配置SSH秘钥的位置在

git 配置多个SSH-Key

项目托管的仓库多了,使用的账号多了,自然用到的key就不同了,比如 gitlabgitee , github , 公司的code仓库等,所以管理好key很重要。

1、生成一个gitlab用的SSH-Key

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

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

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

此时, .ssh 目录(一般在 C:\Users\Administrator\.ssh 目录下) 下应该有4个文件: gitlab-rsagitlab-rsa.pubgithub-rsagithub-rsa.pub ,分别将他们的公钥文件( gitlab-rsa.pubgithub-rsa.pub )内容配置到对应的 code 仓库上

具体配置SSH秘钥的位置在 github/gitlab 网站的 个人信息 ----> 设置/settings ----> SSH公钥/SSH public key 中设置。

3、添加私钥

$ ssh-add ~/.ssh/gitlab-rsa 
$ 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

4、修改配置文件

# 若.ssh目录下无config文件,那么创建
touch config

# config文件中添加以下内容
# gitee 码云
Host gitee.com ## Host 这个指明的是HOST地址,也就是项目的HostName,如:git@gitee.com:ghostgithub/xUtils.git   gitee.com就是其对应的Host(访问的项目的地址)
HostName gitee.com  ## HostName 就是访问的地址,如:https://gitee.com/   就是其HostName(IP地址,访问的码云的网页上的url地址)  (https://建议不要加上)
PreferredAuthentications publickey  ## 指明配置的是公钥
IdentityFile ~/.ssh/gitee-rsa ## 指定弓腰的位置及文件
# 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、测试

$ ssh -T git@github.com

输出

Welcome to GitLab, your name!

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Advanced Web Metrics with Google Analytics, 2nd Edition

Advanced Web Metrics with Google Analytics, 2nd Edition

Brian Clifton / Sybex / 2010-3-15 / USD 39.99

Valuable tips and tricks for using the latest version of Google Analytics Packed with insider tips and tricks, this how-to guide is fully revised to cover the latest version of Google Analytics and sh......一起来看看 《Advanced Web Metrics with Google Analytics, 2nd Edition》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器