David Williams
David Williams

Reputation: 8654

EC2: How to Clone Git Repository

I am trying to checkout a private repo from github.com onto my dev instance on EC2.

$ git clone [email protected]:Org/Product.git
Initialized empty Git repository in /home/ec2-user/Product/.git/
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

How do I do this? I tried SSH forwarding as well, but that didn't work.

Upvotes: 26

Views: 52398

Answers (3)

Bijendra
Bijendra

Reputation: 10035

When working with git or setting up ssh keys on EC2 instance, consider that you are just working on your own machine. So steps which were taken, such as generating ssh keys, adding id_rsa.pub key to accepted ssh keys on git account should all be performed. After copying the ssh key, check if the connection is established. Checkout the links in the above answers as they directly point to your solution.

Upvotes: 3

Yafang Yang
Yafang Yang

Reputation: 386

We need to generate an SSH key (two files - a public key that you share with the world and a private key you keep safe) that we will associate with our Git account. This will allow us to clone our Git repository on an EC2 instance without having to manually type in your username and password or (worse yet) put your password in cleartext when using a script.

You can generate an SSH key on your local directory and then copy to your EC2 instance. You can also do it on your EC2 instance directly, but each time you generate an SSH key pair on your new instance, you need to register the new key in GitHub every time.

  1. In your local terminal, create an SSH key, substituting your email address.

    $ ssh-keygen -t rsa -b 4096 -C [your email address]
    
  2. Save the key to the default directory, ~/.ssh

  3. Enter a pass-phrase of your choice.

  4. Check that the public and private key are in /.ssh by going to the directory and typing “ls -l id_rsa*”. You should see two files, the public key named “id_rsa.pub” and the private key named “id_rsa”

  5. From the terminal, make sure this private key is not publicly viewable.

    $ chmod 600 ~/.ssh/id_rsa
    
  6. Add your SSH private key to the ssh-agent and store your passphrase in the keychain.

    $ ssh-add -k ~/.ssh/id_rsa
    
  7. Go to the settings under your GitHub account and then click SSH keys and New SSH key

  8. In terminal copy your public key to the clipboard. Or show on the EC2 terminal:

    $ pbcopy < ~/.ssh/id_rsa.pub   # copy to clipboard
    $ cat ~/.ssh/id_rsa.pub  # If you prefer appear on screen
    
  9. Paste this into the key box on GitHub and click save. This key is available to ALL your Git repositories.

  10. Sometimes you need to move the public key to “/.ssh/authorized_keys” to make the public key to work in LINUX.

    $ mkdir ~/.ssh  # if you don't have /.ssh/ folder
    $ chmod 700 ~/.ssh
    $ touch ~/.ssh/authorized_keys
    $ chmod 600 ~/.ssh/authorized_keys
    $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
    
  11. Follow this article https://help.github.com/articles/error-permission-denied-publickey/ to see if the key works and debug.

Upvotes: 31

ianjs
ianjs

Reputation: 827

See this answer.

You just need to set up your public and private keys to authenticate with GitHub as described here: https://help.github.com/articles/generating-ssh-keys

Upvotes: 1

Related Questions