亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

git使用多個SSH公鑰連接多個github遠程倉庫

標簽:
Git

摘要:默认情况下,单台机器只能使用一个与账户对应的SSH密钥连接github,这给多用户连接多账户带来不便。可利用config文件解决这一问题。

需求

机器A一直使用账户user1的SSH公钥连接github。现在新建账户user2,希望在机器A也能够以SSH方式连接到github

问题描述

默认情况下,即机器A一直使用账户user1。此时使用命令$ git remote -v可以查看当前的远程仓库关联如下:

origin  [email protected]:user1Name/repositorie1Name.git (fetch)
origin  [email protected]:user1Name/repositorie1Name.git (push)

如果user2新建一个名为repositorie2Name的仓库,此时想在机器A上使用命令$ git push -u origin master提交到远程仓库,会出现如下的错误。

ERROR: Permission to user2Name/repositorie2Name.git denied to user2.
fatal: Could not read from remote repository.Please make sure you have the correct access rights and the repository exists.

问题原因

机器A当前的公钥是user1的,user2没有权限使用;想在user2的账户中添加user1的公钥?也是不可能的,会提示公钥已经被使用。

解决方案

  • 在user2的项目目录中打开命令行,执行命令:ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_for_user2,生成专属user2的密钥对,再进入user2的github账户将公钥配置完成。

  • ~/.ssh/目录下新建config文件,写入以下内容:

#Default GitHubHost user1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Host user2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_for_user2
  • 回到命令行,执行命令:$ git remote set-url origin user1:user1Name/repositorie1Name.git,修改默认的关联。也可以把原有的默认关联删除,重新添加。

  • 再继续执行命令:$ git remote add origin2 user2:user2Name/repositorie2Name.git,新添加一个user2的关联。

  • 此时执行命令:$ git remote -v,应是以下结果:

origin2  user2:user2Name/repositorie2Name.git (fetch)
origin2  user2:user2Name/repositorie2Name.git (push)
origin  user1:user1Name/repositorie1Name.git (fetch)
origin  user1:user1Name/repositorie1Name.git (push)
  • 验证。可分别执行命令:$ ssh -T user1$ ssh -T user2,均出现连接成功提示,实现了多个SSH公钥连接多个github远程仓库的需求。

Hi user1! You've successfully authenticated, but GitHub does not provide shell access.
Hi user2! You've successfully authenticated, but GitHub does not provide shell access.



作者:MrDcheng
链接:https://www.jianshu.com/p/53f466acbae2


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消