Managing Multiple SSH Keys and Accounts for Github and Bitbucket

综合编程 myers.io (源链)

This is an error that gets me every time and I always forget what the solution is. The easiest way for me to remember how to solve it is to write about it.

Scenario:

You have a personal account on either Github or Bitbucket (I’ll just refer to both as Bitbucket from here on; partially because it’s easier, but also because I’ve only really tested this on Bitbucket, but I assume Github will be the same.) and you land yourself a nice new job. For this job, you will be using Github as well, but since you don’t want to mix your professional and personal life, you create two separate Github accounts. Now, for authentication, you prefer to use SSH keys. The problem is that when you go and paste in the contents of id_rsa.pub
into the new public key text box, you get an error telling you that it is already in use with a different account.

“No problem” you think to yourself. You go and generate a new key, remembering to call ssh-add
and you paste this into the text field. Github accepts the new key and everything is good in the world.

Then you go and clone a repo in your new team, but when you try you get an error:

conq: repository access denied.

What’s the deal? You assume it’s some key issue so you start to debug, running things like:

ssh -T -v git@bitbucket.org

That works quite happily, so it’s not a problem with the keys then…

Hang on, do we really know that it’s not a problem with the keys? Well, no not really. It works for calling ssh
, but does git
know what to do with multiple keys? The answer appears to be no. I’m not sure entirely why, but it seems to just offer up the first key and then stop.

Solution:

Fortunately, there is a solution, even though it is a little annoying to deal with.

First up, ensure you have a file called ~/.ssh/config
. If it doesn’t exist, create it. In here you are going to add entries for the service where you have conflicting accounts. For example:

Host personal_account
  HostName bitbucket.org
  IdentityFile ~/.ssh/personal_private_key
Host work_account
 HostName bitbucket.org
 IdentityFile ~/.ssh/work_private_key

Those names personal_account
and work_account
can be substituted for anything that makes sense to you. Personally, I just use the usernames for the accounts.

Now, when you are setting up the remotes, for instance when cloning, you are going to replace the hostname with the alias you chose. e.g.

git clone git@bitbucket.org:work_username/work_project.git

will become

git clone git@work_account:work_username/work_project.git

What this does is change the hostname so that the keys have to be looked up. Since the keys are specified in the config
file, it always looks up the correct key.

If you already have the repositories set up and are now failing because of multiple accounts, you just need to change the remote values with:

git remote remove origin
git remote add origin git@work_account:work_username/project.git

您可能感兴趣的

吸引开发者:苹果在GitHub上公开XNU内核源码... 据外媒neowin报道,虽然苹果声称过去是各种开源项目的主要贡献者,但其中的很多技术仍是封闭的。 不过最新,这家公司在GitHub上发布了其旗舰操作系统的XNU内核源代码。 XNU是“XNU is Not Unix”的缩写,由苹果电脑发展的操作系统内核,被使用于Mac OS X中,包括i...
An open source GitHub-like platform as an alternat... 1Backend https://circleci.com/gh/1backend/1backend https://codecov.io/gh/1backend/1backend/branch/master 1Backend is a platform designed to make d...
GitHub Introduces Code Owners GitHub Introduces Code Owners Written by Kay Ewbank Monday, 10 July 2017 ...
SSH key rotation SSH Key rotation! More Saturday morning fun. In some circles ssh key rotation is terrifying and considered a massive headache. In...
Git gives ‘ERROR: Repository not found.̵... I had a fun problem that made me spin my wheels an hour or so today. I was having no issue cloning a remote repository a number of times in the mornin...
责编内容来自:myers.io (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Managing Multiple SSH Keys and Accounts for Github and Bitbucket



专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录