zeropsi
zeropsi

Reputation: 694

What is the correct way to add SSH keys for multiple accounts on a single machine?

I'm using bitbucket.org to my manage my repositories both at work and personally.

On my new MacBook Pro, I generated a new SSH key and then added that to my work account on bitbucket.org and have had no issues with committing my work. This key is saved on my machine at ~/.ssh/id_rsa.

Now, I am trying to add my personal account on the machine, and having all types of issues with committing my work.

I generated a ssh key using the following command:

ssh-keygen

I saved this key at ~/.ssh/id_personal_rsa and then copy the key into my personal bitbucket.org account.

I then created a config file at ~/.ssh/config that has the follow entries:

Host *
  IdentityFile ~/.ssh/id_rsa

Host bitbucket.org-personal
  HostName bitbucket.org
  User git
  IdentityFile ~/.ssh/id_personal_rsa
  IdentitiesOnly yes

(The personal string is just replaced with my actual account name.)

Now, for the issue, when I try and commit work, I am getting this error:

Git: [email protected]: Permission denied (publickey).

This is the config file for my .ssh connection in my project:

[remote "origin"]
    url = [email protected]:personal/my-personal-project.git
    fetch = +refs/heads/*:refs/remotes/origin/*

Any thoughts on what I may have configured incorrectly?

UPDATE: I've updated my config for my personal account to reflect some of the answers from this post and from this link: Bitbucket ssh public key is being denied but their ssh test connects with no issue.

When I run this command: ssh -T [email protected], I get this response:

authenticated via ssh key.

You can use git to connect to Bitbucket. Shell access is disabled

However, I am still getting Permission denied when trying to commit my new changes.

Upvotes: 1

Views: 980

Answers (1)

VonC
VonC

Reputation: 1324347

First, the User entry should be set to git, not "personal".
You never establish an SSH connection to a Git repositories remote hosting server with a user account, always with a technical service account (generally named git).
The authentication is done through the SSH key used.

Second, once you have set an Host entry named bitbucket.org-personal, you can check if it works with:

ssh -Tv bitbucket.org-personal

Finally, the remote URL to use would be:

bitbucket.org-personal:personal/my-personal-project.git

Note that, as noted in this BitBucket thread, you need to register the key on the account level, not the repository level.

https://community.atlassian.com/t5/image/serverpage/image-id/72754i5481CF5C1AC9558A/image-dimensions/2500?v=v2&px=-1

  1. Remove the SSH key from the repo.
    (Click on repo name > Settings > Access Keys)
  2. Add SSH key to Account settings SSH keys.
    (Click on your avatar > Bitbucket Settings > SSH Keys)

As mentioned in "Can't git push to Bitbucket: Unauthorized - fatal: Could not read from remote repository"

adding the keys under the repo only gives you a read-only access.
For read and write access, you need to add your keys under your account.

Upvotes: 1

Related Questions