可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 username@11.111.11.11
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
user.email=my_email_addreess@gmail.com
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.
回答1:
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
回答2:
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!
回答3:
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.
回答4:
You need to generate and upload a SSH key onto the EC2 instance. Follow this tutorial: http://alestic.com/2010/10/ec2-ssh-keys
回答5:
- Run
ssh-keygen
locally
- 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
回答6:
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.
回答7:
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.
回答8:
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/
回答9:
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!
回答10:
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 ec2Username@long-crazy-amazon-ip.com:/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"
回答11:
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/