DEV Community

Md. Mehedi Hasan Nabil
Md. Mehedi Hasan Nabil

Posted on

🚀 How to Set Up an EC2 Instance and Clone a Private GitHub Repo Using SSH

Step 1:
Create an EC2 Instance on AWS

  • Go to the AWS EC2 Dashboard.
  • Click Launch Instance.
  • Choose the Ubuntu AMI (Amazon Machine Image).
  • Select the instance type (e.g., t2.micro for free tier).
  • Configure storage, security group (allow SSH port 22), and key pair.
  • Launch the instance.

Step 2:
Connect to Your EC2 and Generate SSH Key
After your EC2 is running:

a. Connect to your instance from your computer:
ssh -i your-key.pem ubuntu@your-ec2-public-ip

b. Inside EC2, create a new SSH key:
ssh-keygen

c. Go into the SSH folder:
cd ~/.ssh

d. List the files to see your keys:
ls
You might see something like
authorized_keys id_ed25519 id_ed25519.pub

The .pub file is your public key — that’s what you’ll add to GitHub.

e. Show your public key:
cat id_ed25519.pub

You will see something like this:
ssh-rsa AAAAB3NzaC1... ubuntu@your-ec2-public-ip

Copy the whole key!

Step 3:
Add This Key to GitHub

  • Open GitHub.
  • Go to Settings > SSH and GPG keys.
  • Click New SSH key.
  • Paste the key you copied from EC2.
  • Give it a name like "EC2 Key" and click Add.

Step 4:
Test Connection with GitHub

Go back to your EC2 terminal and run:
ssh -T git@github.com

Type yes if asked.
If everything worked, you’ll see:
Hi your-username! You’ve successfully authenticated...

Step 5:
Clone Your Private Repo

Now clone your private GitHub project:
git clone git@github.com:your-username/your-private-repo.git

🎉 That’s it! You now have your private GitHub repo on your EC2 server!

Top comments (0)