85

I am trying to follow this instruction. I have a local git repo and when I do a git push, I need the repo to be pushed to my EC2 instance.

But, in the above tutorial, when I do a git push origin master, I get Permission denied (publickey) error because I did not specify the identity file.

Say, I login to EC2 like this: ssh -i my_key.pem [email protected]

So, can I do something similar here to: git -i my_key.pem push origin master or set the identity file in .git/config

So, how can I set it up?

Update: Output of git config -l

user.name=my name [email protected] github.user=userid core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true core.ignorecase=true remote.origin.url=ec2_id@my_e2_ip_address:express_app remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* 

Update (from @Jon's comment):

If you have your key in an odd path just run ssh-add /private/key/path. This worked for me.

5
  • 16
    ssh-add /private/key/path worked! Commented Mar 12, 2011 at 8:07
  • 1
    When you say it worked, can you add instructions as to what you actually did step by step? Commented May 25, 2012 at 9:52
  • Which machine do you run that on, local or EC2 instance? What is the express_app in your config? Commented May 30, 2012 at 15:51
  • @Designermonkey its on ec2 instance. Its the name of the git repo, which is a node's express framework app. Commented Jul 13, 2012 at 21:07
  • this guide works well for me: jeffhoefs.com/2012/09/… Commented Jul 28, 2014 at 11:43

12 Answers 12

58

To copy your local ssh key to amazon try this

cat ~/.ssh/id_?sa.pub | ssh -i amazon-generated-key.pem ec2-user@amazon-instance-public-dns "cat >> .ssh/authorized_keys" 

replacing the names of the key and amazon ec2 public dns, of course.

you will then be able to setup your remote on amazon

Sign up to request clarification or add additional context in comments.

2 Comments

I followed this, but used an rsa key instead of a dsa key. Also, I added a space between cat and >>, like: "cat >> .ssh/authorized_keys"
this does not answer the question of how we specify the key when executing a git push command
31

The instructions listed here were more useful to me.

From the link:

Adjust your ~/.ssh/config and add:

Host example Hostname example.com User myuser IdentityFile ~/.ssh/other_id_rsa 

Now use the ssh host alias as your repository:

$ git remote add origin example:repository.git $ git pull origin master 

And it should use the other_id_rsa key!

4 Comments

I also found useful from that to git remote add ec2 ssh://[email protected]:zivot. I didn't know one could prefix addresses with ssh:// before that.
Great answer, was hoping to take advantage of the ssh config that I'm already using.
Awesome. This is better than figuring out the complete URL.
Still saving hours of internet digging 10 years later. Thank you!
24

On your local machine, edit your ~/.ssh/config and add:

Host example Hostname example.com User myuser IdentityFile ~/.ssh/YOURPRIVATEKEY 

You should be able to login to your instance with "ssh example". Remember your private key should be chmod 400. Once you can ssh in without using "ssh -i mykey.pem username@host", do the following.

On your EC2 instance, initialize a bare repository, which is used to push to exclusively. The convention is to add the extention ".git" to the folder name. This may appear different than your local repo that normally has as .git folder inside of your "project" folder. Bare repositories (by definition) don't have a working tree attached to them, so you can't easily add files to them as you would in a normal non-bare repository. This is just they way it is done. On your ec2 instance:

mkdir project_folder.git cd project_folder.git git init --bare 

Now, back on your local machine, use the ssh host alias when setting up your remote.

git remote add ec2 EXAMPLEHOSTFROMSSHCONFIG:/path/to/project_folder.git 

Now, you should be able to do:

git push ec2 master 

Now your code is being pushed to the server with no problems. But the problem at this point, is that your www folder on the ec2 instance does not contain the actual "working files" your web-server needs to execute. So, you need to setup a "hook" script that will execute when you push to ec2. This script will populate the appropriate folder on your ec2 instance with your actual project files.

So, on your ec2 instance, go into your project_folder.git/hooks directory. Then create a file called "post-receive" and chmod 775 it (it must be executable). Then insert this bash script:

#!/bin/bash while read oldrev newrev ref do branch=`echo $ref | cut -d/ -f3` if [ "ec2" == "$branch" -o "master" == "$branch" ]; then git --work-tree=/var/www/example.com/public_html/ checkout -f $branch echo 'Changes pushed to Amazon EC2 PROD.' fi done 

Now, on your local machine, do a "git push ec2 master" and it should push the code to your bare repo, and then the post-receive hook script will checkout your files into the appropriate folder that your webserver is configured to read.

3 Comments

this works for me. the chmods are very important. @devdrc you may need to edit it further and make the command line statements emphasized.
the part before bash script worked great, but bash script didnt work for me. This answer stackoverflow.com/a/24027870/847954 worked great for me. Thanks devdrc for this post and @blamb for posting the script.
@jeffmusk you need to make sure that post-receive file is executable
5

You need to generate and upload a SSH key onto the EC2 instance. Follow this tutorial: http://alestic.com/2010/10/ec2-ssh-keys

6 Comments

but I already have the key-value pair private key with me, which I used to login to EC2.
Try some of the solutions in this thread: serverfault.com/questions/39733/…
i understand that part, but this is a Git configuration issue.
I don't see anything wrong with your configuration, so I believe that it is something wrong with your SSH keys, either misplaced or what not - it most likely would be that and not your configuration.
If you have your key in an odd path just run ssh-add /private/key/path.
|
4

I found this was the quickest way: https://gist.github.com/matthewoden/b29353e266c554e04be8ea2058bcc2a0

Basically:

ssh-add /path/to/keypair.pem (the"-add" needs to be RIGHT AFTER the ssh)

check to see if it worked by: ssh ubuntu@crazylongAWSIP (maybe your username is not ubuntu)

After that you can set up a git repo on your ec2 and push to it:

git remote add origin [email protected]:/path/to/your/repo-name.git git config --global remote.origin.receivepack "git receive-pack" # needed for aws ec2 stuff. git push origin master 

Your options are to set up a 'bare' git repo on your ec2 (which means other git repos can pull from it and push to it, but it won't hold any files), or you can set up a NORMAL repo and push to it directly (my preference if you want to push local changes to your ec2 without having to constantly ssh into your ec2).

If you want to set up a NORMAL repo on the ec2, ssh in to the ec2, do a git init where you want, and then do this:

git config receive.denyCurrentBranch updateInstead 

See: cannot push into git repository for explanation of "recieve deny current branch"

Comments

2
  1. Run ssh-keygen locally
  2. In your local ~/.ssh/ directory you should now see a public key file called id_rsa.pub - copy the contens of this file to the /etc/ssh/authorized_keys file, which is located on your remote server.

You can either copy and paste the contents, or upload the file to your remote server first and use the following command:

cat id_rsa.pub >> /etc/ssh/authorized_keys

2 Comments

Are steps 2 and 3 the same?
No, @JoeTidee - step 2 is getting the key onto the remote server and step 3 is adding it to the right place. :)
1

I'm not posting anything new here, I think, but I had to dig through the above answers to address my particular case. I have an Ubuntu instance on EC2.

To login to my instance, I needed to do:

ssh -i "pemfile.pem" ubuntu@very-long-amazon-address 

the key file "pemfile.pem" had to be in quotes.

I added the remote:

remote add origin ubuntu@very-long-amazon-address/home/ubuntu/git/REPO/gitfile.git 

But when I tried to push:

git push origin master 

I got:

Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 

To fix, I did:

/<path to pemfile>/pemfile.pem 

Which gave me a response,

Identity added: /<path to pemfile>/pemfile.pem (/<path to pemfile>/pemfile.pem ) 

After which the push went through fine.

2 Comments

I don't understand when you say "To fix I did: /path to pemfile/ " my pemfile.pem doesn't execute anything.... what command do you use to add the identity?
please specify and conclude your answer that which command you have used to add key???
0

I was getting permission denied when deploying via source control and couldn't figure out why. I realized my user I was creating an ssh key for (named ubuntu, also the recommended login for my ec2 server) was not the user who was responsible for cap deploy (root). Running an ssh-keygen for root and uploading that ssh key as a deploy key to bitbucket solved my issues.

Comments

0

I know I'm too late for this but I just wanted to share this article which in just seconds I've successfully pushed to EC2 git repo

http://shirtdev.wordpress.com/2011/05/04/setting-up-a-git-repository-on-an-amazon-ec2-instance/

Comments

0

Here is the EASIEST way that worked great for me... I was having trouble cloning a repository... it was not recognizing the SSH Key I created... Instead of changing your config file and all that, I simply copied the REAL ssh key it was trying to connect with and I added this to bitbucket... here is the command:

 sudo vi /root/.ssh/id_rsa.pub 

Used VI to open the REAL RSA key and copied the content and pasted into bitbucket... Done!

Comments

0

maybe this isn't a popular response, but I was struggling with the same problem and finally decided to store the folders on AWS S3 Bucket, it was the fastest solution because I was dealing with very large files and +3000 archives.

Simply install Aws cli, use aws configure and aws s3 cp SOURCE_DIR s3://DEST_BUCKET/ --recursive

After that, you could download it to your computer and use GitHub like always, or make your bucket public so you can get the archives anywhere.

Comments

-2

For anyone else who might be interested, this solution proved to be the cleanest and easiest for me:

http://eric.sau.pe/accessing-a-git-repository-using-a-key-pair/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.