⚡ This is the AMP version of this page. View full version

Clone a Private Repo ( for EC2 or digitalOcean or any other VM)

Published: 7/7/2025 | Updated: 6/7/2025

A comprehensive guide on how to clone organization-owned private repositories on AWS EC2 instances using deploy keys. This method is secure and doesn’t require storing personal access tokens on the server.

Prerequisites

Step 1: Create Deploy Key on EC2 Instance

Generate SSH Key Pair

  1. SSH into your EC2 instance
  2. Generate a new SSH key pair:
ssh-keygen -t ed25519 -a 100 -C "your-github-email@example.com"

Note

Replace your-github-email@example.com with your actual GitHub account email

  1. Navigate to the SSH directory (usually /home/ec2-user/.ssh or /home/ubuntu/.ssh)
  2. Display the public key:
cat ~/.ssh/id_ed25519.pub

Copy this public key - you’ll need it for the next step.

Step 2: Add Deploy Key to GitHub Repository

  1. Navigate to your private repository on GitHub
  2. Click on Settings in the top navigation bar
  3. Select Deploy Keys from the left sidebar
  4. Click Add deploy key button
  5. Enter:
    • Title: A descriptive name for this deploy key
    • Key: Paste the public key from Step 1
  6. Click Add key to save

Warning

Do not modify the public key content when pasting it into GitHub

Step 3: Test GitHub Connection

Verify that your EC2 instance can authenticate with GitHub:

ssh -T git@github.com

You might see a confirmation prompt:

Are you sure you want to continue connecting (yes/no/[fingerprint])?

Type yes to proceed.

Expected Success Response

Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi YourORG/YourRepo! You've successfully authenticated, but GitHub does not provide shell access.

Step 4: Install Git (if not already installed)

Update package manager and install Git:

# For Amazon Linux/CentOS/RHEL
sudo yum update
sudo yum install git
 
# For Ubuntu/Debian
sudo apt update
sudo apt install git

Step 5: Clone the Private Repository

Now you can clone your private repository using SSH:

git clone git@github.com:OrgName/your-repo.git

Replace OrgName/your-repo.git with your actual organization and repository name.

or just

Debug SSH Connection

For detailed debugging information:

ssh -vT git@github.com

Tip

This method is preferred over using personal access tokens as it provides repository-specific access and doesn’t expose your personal GitHub credentials.