git 配置多个SSH-Key实现示例
目录
git 配置多个SSH-Key
项目托管的仓库多了,使用的账号多了,自然用到的key就不同了,比如gitlab,gitee, 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-rsa和gitlab-rsa.pub,github-rsa和github-rsa.pub,分别将他们的公钥文件(gitlab-rsa.pub,github-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!
以上就是git 配置多个SSH-Key实现示例的详细内容,更多关于git 配置多个SSH Key的资料请关注代码部落其它相关文章!
本文章来源于网络,作者是:这个骑士不炸街,由代码部落进行采编,如涉及侵权请联系删除!转载请注明出处:https://daimabuluo.cc/xiangguanjiqiao/2063.html
