Permission denied (publickey) - Cant push or pull with Git

2.9k views Asked by At

Git was working fine with pushing to Bitbucket, until recently where there haven't been much changes, except for a change in DNS server settings.

The error:

Permission denied (publickey).
fatal: Could not read from remote repository.

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

I used keygen to create a new key, add the key with Git Bash (key was added and accepted) and then manually add the key to Bitbucket SSH. However, it's still yielding the same error.

Not sure what went wrong, does anyone have the same experience and able to help?


Other bits of info or attempts to troubleshoot

  • git remote -v, one push, one pull item listed
  • ssh -T [email protected] - error: Permission denied (publickey).
  • ssh-add -l - error: Could not open a connection to your authentication agent.
  • ssh -vT [email protected] - error below

ssh -Tv [email protected] OpenSSH_6.6.1, OpenSSL 1.0.1i 6 Aug 2014

debug1: Connecting to bitbucket.org [131.103.20.167] port 22.
debug1: Connection established.
debug1: identity file /.ssh/id_rsa type -1
debug1: identity file /.ssh/id_rsa-cert type -1
debug1: identity file /.ssh/id_dsa type -1
debug1: identity file /.ssh/id_dsa-cert type -1
debug1: identity file /.ssh/id_ecdsa type -1
debug1: identity file /.ssh/id_ecdsa-cert type -1
debug1: identity file /.ssh/id_ed25519 type -1
debug1: identity file /.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 97:8c /* rest of numbers */
debug1: Host 'bitbucket.org' is known and matches the RSA host key.
debug1: Found key in /.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /.ssh/id_rsa
debug1: Trying private key: /.ssh/id_dsa
debug1: Trying private key: /.ssh/id_ecdsa
debug1: Trying private key: /.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).


- ssh-agent -s, error:

SSH_AUTH_SOCK=/tmp/ssh-2KEE1p8SGXPg/agent.1900; export SSH_AUTH_SOCK;
SSH_AGENT_PID=2712; export SSH_AGENT_PID;
echo Agent pid 2712;
4

There are 4 answers

0
Thinkerer On BEST ANSWER

I know this answer may not be the best of answers but I managed to get it working with the following steps. (Disclaimer: I still dont know what went wrong)

  1. Kill all ssh-agent process by kill PIDnumber one by one within Git Bash. Somehow I had a list full of agent processes running. I saw this list of PID when I tried to uninstall Git from my system.
  2. Uninstall Git from the computer
  3. Remove all SSH keys from Bitbucket repo - (manually remove by browsing to BB website)
  4. Reinstall Git and in Git bash, ssh -T [email protected] + entered passphrase so user is login and confirmed
  5. Added existing rsa_pub keys (i just dragged rsa_pub file to sublime text, copied the key and pasted it back to bitbucket in the same repo)
  6. pull then push from nodejs's command line utility
3
brahmana On

You need to run ssh-agent command and then export the variables that are displayed when you run it. To do that you can do something like this :

eval `/usr/bin/ssh-agent`

The output of the ssh-agent command is a valid shell script which when executed (a.k.a evaled) will set the right env variables required for the communication between ssh-agent program and other programs that use it.

Just make sure you kill your existing ssh-agent process before you run this command.

After that use the usual ssh-add commands to list and add keys and then your ssh should work just fine.

6
michas On

The message "Could not open a connection to your authentication" implies that no agent is running (or at least not reachable).

ssh-agent bash        # start a new agent
ssh-add               # add your key
ssh-add -L            # verify that your key is the same as on the server

ssh [email protected]             # should work now 
git clone [email protected]:xyz   # (or something like this) should work, too
0
Mahmoud Zalt On

This worked for me:

  1. after you generate a new SSH key (ssh-keygen -t rsa)
  2. and add it to the bitbucket site (cat ~/.ssh/bitbucket.pub ...)
  3. if you still not able to connect then do this:

    1. eval ssh-agent -s
    2. ssh-add bitbucket                         
    3. try again now again