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

Time:08-17

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: 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.

  • Related