14

Whenever I try to do a 'git pull origin master' I get (It is NOT Github):

Permission denied (publickey).

I am able to SSH into my AWS EC2 Linux sever, which has the bare repository, which I'm trying to pull from when I get the aforementioned permission error.

I have indeed copied the public key to that server, because I can login successfully via ssh, but only by doing a:

ssh -i /location/of/pemkey/mykey.pem ec2-user@ec2-12-34-56-78.us-east-compute.amazonaws.com

I need to configure Git to use my '.pem' key. How do I accomplish setting up Git to utilize my '.pem' key?

ConfusedDeer
  • 3,335
  • 8
  • 44
  • 72
  • Ok why would you configure git to use the .pem key? If you have a generated a ssh key successfully just copy the public key to the server and thats all. – ckruczek Jun 22 '15 at 05:55

4 Answers4

10

I got this from here, but it is not the main answer. The instructions listed here were more useful to me.

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!

Community
  • 1
  • 1
ConfusedDeer
  • 3,335
  • 8
  • 44
  • 72
6

From the git(1) man page:

   GIT_SSH
       If this environment variable is set then git fetch and git push
       will use this command instead of ssh when they need to connect to a
       remote system. The $GIT_SSH command will be given exactly two or
       four arguments: the username@host (or just host) from the URL and
       the shell command to execute on that remote system, optionally
       preceded by -p (literally) and the port from the URL when it
       specifies something other than the default SSH port.

       To pass options to the program that you want to list in GIT_SSH you
       will need to wrap the program and options into a shell script, then
       set GIT_SSH to refer to the shell script.

       Usually it is easier to configure any desired options through your
       personal .ssh/config file. Please consult your ssh documentation
       for further details.

In my personal experience, the one-time cost of adding host settings in .ssh/config has made a big difference, even for hosts where only the username is different.

o11c
  • 15,265
  • 4
  • 50
  • 75
4
1. touch ~/.ssh/config
2. chmod 644 ~/.ssh/config
3. vim ~/.ssh/config
    #write next codeline
    host ec2-ip.eu-west-1.compute.amazonaws.com
    IdentityFile ~/Documents/ec2-user.pem

git clone git@ec2-ip.eu-west-1.compute.amazonaws.com:/home/git/my-repo.git
dmitri
  • 460
  • 5
  • 11
0

We can use git config core.sshCommand from git version 2.10.0. -c option can change config temporally.

Command example for cloning.

git -c core.sshCommand="ssh -i some.pem -F /dev/null" clone ...

Reference: https://qiita.com/sonots/items/826b90b085f294f93acf

asukiaaa
  • 1,594
  • 17
  • 19