web analytics

Multiple SSH identities in Git

After a long break of nearly 2 years, I tried to brush up my knowledge and wanted to started with Git. I created a bitbucket repo under my personal account, and setup the identities but no matter what, I was unable to clone the repo via ssh. I was getting the following error while trying to do it.

git clone -b master git@bitbucket.org:myusername/magento2.3.git
Cloning into ‘magento2.3’…
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

The error clearly stated it was a problem with my ssh key. I already checked and confirmed the following

  1. I have added the SSH public key to the repo’s Access keys option.
  2. I also tried to add it under Personal Settings -> Security -> SSH keys but both didn’t work
  3. I confirmed the details are correct in git config –list

    [root@abc]# git config –list
    user.name=My Name
  4. Git clone works with https, so there is not problem with the repo, but the problem is with the ssh key
  5. Next thing to try was to ssh to bitbucket in verbose mode to find the actual error

    [root@abc]#ssh -Tv git@bitbucket.org
    debug1: Reading configuration data /home/abc/.ssh/config
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    debug1: Connecting to bitbucket.org [] port 22.
    debug1: Connection established.
    debug1: identity file /home/abc/.ssh/id_rsa type 1

    From above, I understood it is trying to connect using the default id_rsa key and not the new ssh key “my_key”  which I wanted to use.
  6. Below are the steps needed to correct this problem.

    [root@abc]# ssh-add -l
    2048 SHA256:q9/hCZVFR0gN+tV/iA0zDS4U9hth0PNRudIpd+72lnA abc@abc (RSA)

    Next add my new key to the ssh sgent
    [root@abc]# ssh-add /home/abc/.ssh/my_key.pem

    Confirm the new key is added
    [root@abc]# ssh-add -l
    2048 SHA256:ovtoXBGZVjxoS/v1wKA9K+reb7h0RkksTiHcnEsHgOE /home/abc/.ssh/my_key.pem (RSA)
    2048 SHA256:q9/hCZVFR0gN+tV/iA0zDS4U9hth0PNRudIpd+72lnA abc@abc (RSA)

  7. Once that was done, everything worked perfectly!

    [root@abc]# git clone -b master git@bitbucket.org:myusername/magento2.3.git
    Cloning into ‘magento2.3’…
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Receiving objects: 100% (3/3), done.
    Checking connectivity… done.

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>