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的资料请关注代码网其它相关文章!
发表评论