Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Clone EC2 Instance Permissions Error

I am trying to be able to clone my private github repo to my EC2 instance, but it appears that I might be taking the wrong steps to allowing the Github connection to the EC2 instance. Upon running git clone [email protected]:username/repo.git in a folder I created at the path /var/www/app I am told to authenticate the Githbuc.com host and presents two RSA key fingerprints (I assume the ones in my repo) and then sends me an error:

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

Please make sure you have the correct access rights
and the repository exists.

Prior to running the git clone command I copied the SSH key found in my .pem to my Github SSH keys. These EC2 Credentials are what I am using to SSH into the instance. Is this the wrong key to use? I am aware that the EC2 instance also has public keys found in .ssh/authorized_keys, but I feel like these are the same that were used to SSH into the instance.

Am I missing a step? Do I need to initialize git in my /var/www/app directory or configure anything before the git clone command?

Any help would be great. Thank you!

like image 201
cphill Avatar asked Sep 04 '25 16:09

cphill


2 Answers

Make sure you generate ssh key pairs on your local computer ~/.ssh/ and your public key has been added in your GitHub account.

To clone your ssh key pairs from your local directory to EC2 instance, follow the following:

clone your public key to your EC2 instance:

$ cat ~/.ssh/id_rsa.pub | ssh -i ~/.ssh/your_pem.pem ubuntu@your_dns "cat >> .ssh/authorized_keys"

If “.ssh/authorized_keys" doesn’t exist, log in to your EC2 instance and create the directory:

$ ssh -i ~/.ssh/***.pem ubuntu@[Your Public DNS]
$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ touch ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
$ exit  # exit EC2 terminal, this will not stop your instance

Then go ahead to secure copy our GitHub private key to the EC2 instance. The “UserKnownHostsFile” and “StrictHostKeyChecking” options removes the host verification prompt (the computer asks you to type yes or no to continue). Removing this is essential if we want to run these actions in a script.

$ scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ~/.ssh/your_pem.pem ~/.ssh/id_rsa ubuntu@your_dns:~/.ssh/

You should be able to use your ssh key pairs to connect to git.

If it doesn't fix, a more complete tutorial for "using Amazon EC2 to run script in a GitHub repo":

https://github.com/yafangy/Tutorial-using-Amazon-AWS-EC2-run-scripts-GitHub/tree/master#method-2-clone-git-to-ec2-instance-recommended

like image 173
Yafang Yang Avatar answered Sep 07 '25 07:09

Yafang Yang


You have to go to the security groups on the EC2 setup and open the access to GIT. The key might be fine, because is your key, but you have to modify the credentials and security group.

The same way, you can modify the credential to open access only to a specific IP and do stuff like that. By default is closed to only the access from inside.

Regards,

DH

like image 36
Hercules_Daniel Avatar answered Sep 07 '25 07:09

Hercules_Daniel