1

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: git@bitbucket.org: Permission denied (publickey).

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

[remote "origin"]
    url = git@bitbucket.org-personal: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 git@bitbucket.org-personal, 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.

zeropsi
  • 682
  • 9
  • 24
  • `git remote set-orl origin personal@bitbucket.org-personal:personal/my-personal-project.git` The key part is the host name `bitbucket.org-personal` — it must be the name from `.ssh/config`. – phd Aug 15 '22 at 17:15
  • Thanks for the look @phd. I tried to change the `Host` for my personal account back to just `bitbucket.org` because that was the actual URL of remote origin, but I am getting the same error. `Git: personal@bitbucket.org: Permission denied (publickey).` – zeropsi Aug 15 '22 at 19:53
  • 1. It's exactly the other way around — host in remote URL must be from SSH, not Host in SSH should be from Git; test with `ssh -Tv personal@bitbucket.org-personal`. 2. The order of Hosts in `.ssh/config` should be from the most specific to generic; i.e. first `Host bitbucket.org-personal`, last `Host *` – phd Aug 15 '22 at 20:06
  • Oh - so the URL of the remote origin doesn't need to be an actual URL that resolves to anything? I will give that a try and report back. Thanks @phd! – zeropsi Aug 15 '22 at 22:16
  • @phd - I changed the config around and I am still unable to push any commits. I am getting the same error. Is there something different I need to generate the ssh key for that account? – zeropsi Aug 16 '22 at 01:24

1 Answers1

1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I've updated my original post to reflect some of these changes. When I check the SSH connection for that entry I am told I am authenticated with that key, however, I am still unable to commit any changes to the repo. I still get this error: `Git: git@bitbucket.org: Permission denied (publickey).` – zeropsi Aug 16 '22 at 11:30
  • @zeropsi What does `git remote -v` return, when executed in your repository folder? – VonC Aug 16 '22 at 11:45
  • ```origin git@bitbucket.org-personal:personal/my-personal-project.git (fetch) origin git@bitbucket.org-personal:personal/my-personal-project.git (push)``` – zeropsi Aug 16 '22 at 13:38
  • @zeropsi nno need for the `git@` part: it is included in your `~/.ssh/config` file, the `User git` line. Beside that, do you get the error when pushing (`git push`)? Can you, for testing, *clone* in a separate empty local folder the repository, using the `bitbucket.org-personal:personal/my-personal-project.git` URL? – VonC Aug 16 '22 at 13:48
  • yes, I was able to clone the repo using that URL. – zeropsi Aug 16 '22 at 14:07
  • And when I attempt a `git push` from the original working directory, this is the error I get: ```Enter passphrase for key '/Users/zeropsi/.ssh/id_personal_rsa': Unauthorized fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.``` – zeropsi Aug 16 '22 at 14:12
  • @zeropsi But can you push from the new cloned repository though? – VonC Aug 16 '22 at 14:21
  • No, I was not able to push from the cloned repo. Same error message. However, it asked me to a configure a username and email for the repo, so I edited the `config` file in my projects `.git` folder to add: ```[user] name = personal email = personal@gmail.com```. – zeropsi Aug 16 '22 at 14:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247311/discussion-between-vonc-and-zeropsi). – VonC Aug 16 '22 at 14:49