git push origin master: permission denied (public key) error

One step you seem to have missed (or didn't include in your description) is the publication of the public key on the server side.
Upload your public ssh key and add it to the ~username/.ssh/authorized_keys file.

Also, try it (for testing) first with a private key without passphrase (no need to ssh-add your key to an ssh agent)

Finally, make sure your ssh keys are with standards names (id_rsa and id_rsa.pub), with the right protection:

  • on the local side
  • in the remote side

Finally, an ssh -Tvvv [email protected] should tell you more, if the previous steps didn't solve the issue.


The complete procedure being:

  1. Add the EC2 public key to your ssh list with the following command

    ssh-add /path/to/myEC2publickey
    
  2. Create a git repository on the EC2 instance with the following commands

    mkdir /path/my_project.git
    cd /path/my_project.git
    git init --bare
    
  3. Connect the local files on your system to your repository with the commands

    cd the_project 
    git init
    git add . 
    git commit -m "Initial git commit message" 
    git remote add origin [email protected]:the_project.git 
    git config --global remote.origin.receivepack "git receive-pack" 
    git push origin master
    
  4. Create a public key as the user and add it to the server's authorized keys

    You can do this step by just copying the file id_rsa.pub from the localhost to the servers ~/.ssh/authorized_keys file, as suggested in the previous answer.

After following these steps if you try the git push, you should not get a "permission denied" error.

Tags:

Git

Amazon Ec2