7

I have set up a git repo on my amazon ec2 ubuntu server instance. I have been trying to push the code onto the server's repo from my local machine. The steps that I followed are:

ssh-add /path/to/myEC2publickey

On my EC2 Instance

mkdir /path/my_project.git
cd /path/my_project.git
git init --bare

Later on my localhost,

cd the_project 
git init git add . 
git commit -m "Initial git commit message" 
git remote add origin username@hostname.com:the_project.git 
git config --global remote.origin.receivepack "git receive-pack" 
git push origin master

Since I was getting a Permission Deined (public key) error while executing the last command (i.e. git push origin master), I set the public key using the steps given on a forum that included -

ssh-keygen -t rsa -C "myemail@somedomain.com"
eval 'ssh-agent -s'
ssh-add

I was able to add the public key but I am still facing the Permission Denied (public key) : Error.

I'm new to git and have been looking forward to shift all my code into a git repo.

Any help would be greatly appreciated.

2 Answers2

5

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:

Finally, an ssh -Tvvv username@hostname.com should tell you more, if the previous steps didn't solve the issue.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Here is what I have done until now: I created a public key under "myname@somedomain.com" With 'ssh-keygen -t rsa -C "myname@somedomain.com"' I even copied the file from /root/.ssh/id_rsa.pub onto the server /home/ubuntu/.ssh/authorized_keys file. But I'm still getting the same permission denied error. I don't know how to work on the last ssh statement that you have given. But I did try that and the last few lines are as follows: 'debug1: could not open key file '/home/amit/.ssh/id_rsa': Permission denied' – Amit Nandan Periyapatna Jan 17 '14 at 09:38
  • I have gone through a lot of forums for setting up git but as I have seen none of the forums suggest adding the key to the authorized_keys file and that has been keeping me on my workstation for hours. Thanks to @VonC, now I can take a break! – Amit Nandan Periyapatna Jan 17 '14 at 10:03
2

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 username@hostname.com: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.

mc01
  • 3,750
  • 19
  • 24
  • Looks correct, similar to my answer. +1. Publishing the public key to the server ssh account `authorized_keys` file is indeed a crucial step, for the ssh daemon to be able to recognize the user asking for an ssh session. – VonC Jan 17 '14 at 10:07
  • @VonC: Agreed it is similar to your answer, but with some added description. – Amit Nandan Periyapatna Jan 17 '14 at 11:32