1

I set up my GitHub account about a year ago, and generated a new SSH key at the time (to easily push and pull from GitHub from Command Line). A few days ago I spun up a new ec2 instance, and downloaded the .pem file it gave me to SSH into it. I am able to easily SSH into the server through the command line using that .pem file.

I'm using Capistrano to deploy my Rails app, and running the cap staging deploy:check. In the deploy.rb file, I have the private repo git url, and in the staging.rb file I have set the set ssh_options to the location of the .pem file I downloaded from ec2 instance.

When the command starts to run, it is connecting to the server fine, but when it tries to access the git repo, GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/<APP_NAME>/git-ssh.sh /usr/bin/env git ls-remote -h git@github.com:<USERNAME>/<APP_NAME>.git, it fails with the message:

Permission denied (publickey).
fatal: Could not read from remote repository.

Been researching this issue online, and from what I've gathered it seems like I need the same SSH for both GitHub and the ec2 server. Perhaps my information is incorrect...Have been unsuccessful thus far.

Would love some advice into this issue. Thank you for your time.

Justin
  • 4,922
  • 2
  • 27
  • 69

1 Answers1

1

If the issue is to register a public key to your GitHub account, you can easily convert a pem to an ssh key:

openssl rsa -in MYFILE.pem -pubout > MYFILE.pub
ssh-keygen -f MYFILE.pub -i -m PKCS8

The first command is in "openSSL: how to extract public key?".
The second one in "Convert pem key to ssh-rsa format".

As the OP Justin confirms:

I just used PuttyGen to convert the .pem to the format GitHub wanted...it was prefixed with ssh-rsa.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the quick response. I converted the `.pem` file to `.pub` file with the first command. Even looking at each key in text editor, I can see the first one starts with the `-----BEGIN RSA PRIVATE KEY-----` and after I convert to `pub`: `-----BEGIN PUBLIC KEY-----`. However, when I run the second command, I get this response: `do_convert_from_pkcs8: MY_KEY_NAME.pub is not a recognised public key format` – Justin Jun 26 '14 at 12:20
  • @Justin let's stop at the first command, then: would register that public key on your GitHub account help with you accessing your private repo? – VonC Jun 26 '14 at 12:29
  • I will have to see what I did wrong further, because using [GitHub guidelines](https://help.github.com/articles/generating-ssh-keys#step-3-add-your-ssh-key-to-github), I get the error `Key is invalid. It must begin with 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'. Check that you're copying the public half of the key` when saving that pub key to GitHub – Justin Jun 26 '14 at 12:33
  • Then try `openssl rsa -noout -text -inform PEM -in key.pub -pubin`, as in http://stackoverflow.com/a/20352821/6309 – VonC Jun 26 '14 at 12:36
  • That does output it in this format `00:59:2c:36:d7......`...but again trying to paste that into the GitHub SSH Keys textfield throws that error. Just want to say I really appreciate your time on this. – Justin Jun 26 '14 at 12:39
  • Yea, converted it and uploaded it to GitHub successfully and confirmed via CL. Still getting that damn error `Permission denied (publickey).` but you're answer helped get the `.pem` to GitHub. – Justin Jun 26 '14 at 13:40
  • Actually just used PuttyGen to convert the `.pem` to the format GitHub wanted...it was prefixed with `ssh-rsa`. – Justin Jun 26 '14 at 14:00
  • @Justin Ok, I have included that option in the answer for more visibility. – VonC Jun 26 '14 at 14:02
  • Alright. I appreciate your time on this question. Cheers @VonC. – Justin Jun 26 '14 at 14:03