AWS Builder Center

Conncet Ubuntu EC2 instance to GitHub

How to Connect an Ubuntu EC2 Instance to GitHub and Clone a Repository

๐Ÿ” Connect GitHub to AWS EC2 Using SSH

Topic: GitHub SSH Authentication
Purpose: Connect an EC2 instance to GitHub so the EC2 server can clone, pull, and push repositories without repeatedly entering GitHub credentials.

๐Ÿ“Œ 1. Basic Concept

1
2
3
4
5
6
7
SSH Authentication

AWS EC2 โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ GitHub
โ”‚ โ”‚
โ”‚ โ”‚
Private Key ๐Ÿ” Public Key ๐Ÿ”‘
id_ed25519 id_ed25519.pub

SSH Key Pair

When we create an SSH key, two files are generated:
1
2
3
~/.ssh/
โ”œโ”€โ”€ id_ed25519 ๐Ÿ” Private Key
โ””โ”€โ”€ id_ed25519.pub ๐Ÿ”‘ Public Key
KeyLocationPurposeShare?
Private Key~/.ssh/id_ed25519Used by EC2 for authenticationโŒ NEVER
Public Key~/.ssh/id_ed25519.pubAdded to GitHubโœ… Yes
known_hosts~/.ssh/known_hostsStores trusted SSH hosts๐Ÿ”’ Keep on EC2
authorized_keys~/.ssh/authorized_keysAllows SSH access to EC2๐Ÿ”’ Keep on EC2

๐Ÿ” Golden Rule

1
2
3
Private Key โ†’ stays on EC2

Public Key โ†’ goes to GitHub
Never share or upload the private key.

๐Ÿš€ 2. New EC2 โ†’ GitHub Setup

Whenever a new EC2 instance is created, follow these steps.

Step 1 โ€” Check Git

1
git --version

What does it mean?

1
2
3
4
5
6
7
git
โ†“
Git program

--version
โ†“
Show installed Git version
Example:
1
git version 2.43.0

If Git is not installed

1
2
sudo apt update
sudo apt install git -y
Then verify:
1
git --version

๐Ÿ”‘ 3. Check Existing SSH Key

Before creating a new SSH key, check whether one already exists.
1
ls -la ~/.ssh
Look for:
1
2
id_ed25519
id_ed25519.pub
Example:
1
2
3
4
5
~/.ssh/
โ”œโ”€โ”€ authorized_keys
โ”œโ”€โ”€ id_ed25519
โ”œโ”€โ”€ id_ed25519.pub
โ””โ”€โ”€ known_hosts

If the keys already exist

If you already have:
1
2
id_ed25519
id_ed25519.pub
Do NOT create another key unnecessarily.
Use the existing key.

๐Ÿ†• 4. Create SSH Key If It Does Not Exist

If id_ed25519 and id_ed25519.pub do not exist:
1
2
3
ssh-keygen -t ed25519 -C "ec2-github"
or
ssh-keygen

Meaning

1
2
3
4
5
6
7
8
9
10
11
ssh-keygen
โ†“
Generate SSH key

-t ed25519
โ†“
Use Ed25519 key type

-C "ec2-github"
โ†“
Add a recognizable comment
When asked:
1
Enter file in which to save the key (/home/ubuntu/.ssh/id_ed25519):
Press:
1
Enter
For the passphrase:
1
Enter passphrase (empty for no passphrase):
Press:
1
Enter
Then:
1
Enter same passphrase again:
Press:
1
Enter
The following files will be created:
1
2
3
~/.ssh/
โ”œโ”€โ”€ id_ed25519
โ””โ”€โ”€ id_ed25519.pub

๐Ÿ” 5. Get the Public SSH Key

Run:
1
cat ~/.ssh/id_ed25519.pub
Example:
1
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...... ec2-github
Copy the entire line.

โš ๏ธ DO NOT run:

1
cat ~/.ssh/id_ed25519
That displays the private key.
Never share the private key.

๐ŸŒ 6. Add Public Key to GitHub

Go to:
1
2
3
4
5
6
7
GitHub
โ†“
Settings
โ†“
SSH and GPG keys
โ†“
New SSH key
Enter:
1
2
Title:
AWS EC2
Select:
1
2
Key type:
Authentication Key
Paste the output of:
1
cat ~/.ssh/id_ed25519.pub
Then click:
1
Add SSH key

๐Ÿงช 7. Test EC2 โ†’ GitHub SSH Connection

Run:
1
ssh -T git@github.com
The first time, you may see:
1
2
Are you sure you want to continue connecting
(yes/no/[fingerprint])?
Type:
1
yes

Successful result

You should see:
1
2
Hi YOUR_GITHUB_USERNAME! You've successfully authenticated,
but GitHub does not provide shell access.

โœ… This is NOT an error.

The important part is:
1
You've successfully authenticated
This means:
1
2
EC2 โ”€โ”€โ”€โ”€โ”€ SSH โ”€โ”€โ”€โ”€โ”€โ–บ GitHub
โœ…

๐Ÿ“ฅ 8. Clone GitHub Repository

Go to your GitHub repository:
1
2
3
4
5
Repository
โ†“
Code
โ†“
SSH
GitHub will give you an SSH URL:
1
git@github.com:USERNAME/REPOSITORY.git
Example:
1
git@github.com:ankittripathidevs/DevOps_FullCourse.git
Clone:
1
git clone git@github.com:ankittripathidevs/DevOps_FullCourse.git
Then:
1
ls
Example:
1
DevOps_FullCourse
Enter the repository:
1
cd DevOps_FullCourse
Check:
1
git status

โš ๏ธ 9. SSH URL Is NOT a Command

This:
1
git@github.com:ankittripathidevs/DevOps_FullCourse.git
is an SSH Repository URL.
โŒ Don't run it directly:
1
git@github.com:ankittripathidevs/DevOps_FullCourse.git
Use it with Git:
1
git clone git@github.com:ankittripathidevs/DevOps_FullCourse.git

๐Ÿ”— 10. Check Existing GitHub Remote

If the repository is already cloned:
1
git remote -v
Example HTTPS remote:
1
2
origin https://github.com/ankittripathidevs/DevOps_FullCourse.git (fetch)
origin https://github.com/ankittripathidevs/DevOps_FullCourse.git (push)
This means the repository is using HTTPS.

๐Ÿ”€ 11. Change HTTPS โ†’ SSH

Change the existing GitHub remote:
1
git remote set-url origin git@github.com:ankittripathidevs/DevOps_FullCourse.git
Verify:
1
git remote -v
Expected:
1
2
origin git@github.com:ankittripathidevs/DevOps_FullCourse.git (fetch)
origin git@github.com:ankittripathidevs/DevOps_FullCourse.git (push)
Now the repository uses:
1
SSH
instead of:
1
HTTPS

๐Ÿ“ค 12. Push Code to GitHub

After making changes:
1
git add .
Create a commit:
1
git commit -m "CI/CD"
Push:
1
git push
Because SSH authentication is configured, GitHub should not ask:
1
Username for 'https://github.com':

๐Ÿ“ฅ 13. Pull Latest Code

To download the latest changes from GitHub:
1
git pull

๐Ÿง  14. Complete Workflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
NEW EC2 INSTANCE
โ”‚
โ–ผ
Install Git
โ”‚
โ–ผ
Check SSH keys
โ”‚
โ–ผ
ls -la ~/.ssh
โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ โ”‚
Key exists No key exists
โ”‚ โ”‚
โ”‚ ssh-keygen
โ”‚ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
Get Public Key
โ”‚
โ–ผ
cat ~/.ssh/id_ed25519.pub
โ”‚
โ–ผ
Add Public Key
to GitHub
โ”‚
โ–ผ
ssh -T git@github.com
โ”‚
โ–ผ
Authentication โœ…
โ”‚
โ–ผ
git clone SSH_URL
โ”‚
โ–ผ
Work on Code
โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ โ”‚
โ–ผ โ–ผ
git pull git push
โ”‚ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ–ผ
GitHub

๐Ÿ“‹ 15. Quick Command Cheat Sheet

Check Git

1
git --version

Install Git

1
2
sudo apt update
sudo apt install git -y

Check SSH keys

1
ls -la ~/.ssh

Create SSH key

1
ssh-keygen -t ed25519 -C "ec2-github"

Show public key

1
cat ~/.ssh/id_ed25519.pub

Test GitHub SSH

1
ssh -T git@github.com

Clone repository

1
git clone git@github.com:USERNAME/REPOSITORY.git

Check remote

1
git remote -v

Change HTTPS โ†’ SSH

1
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

Pull

1
git pull

Push

1
git push

โš ๏ธ 16. Common Problems

Problem 1 โ€” Permission denied (publickey)

Error:
1
git@github.com: Permission denied (publickey).
Test:
1
ssh -T git@github.com
If authentication fails:
  1. Check SSH key:
1
ls -la ~/.ssh
  1. Get public key:
1
cat ~/.ssh/id_ed25519.pub
  1. Add that public key to GitHub.
  2. Test again:
1
ssh -T git@github.com

Problem 2 โ€” Git asks for GitHub username

Error:
1
Username for 'https://github.com':
Check:
1
git remote -v
If you see:
1
https://github.com/...
change it to SSH:
1
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

Problem 3 โ€” SSH URL typed directly

Wrong:
1
git@github.com:USERNAME/REPOSITORY.git
Correct:
1
git clone git@github.com:USERNAME/REPOSITORY.git

๐Ÿ” 17. EC2 SSH Key vs GitHub SSH Key

Do not confuse these two.

AWS EC2 Login

Used to connect your computer โ†’ EC2.
Example:
1
2
3
4
5
my-laptop
โ”‚
โ”‚ .pem key
โ–ผ
EC2

GitHub SSH Authentication

Used to connect EC2 โ†’ GitHub.
1
2
3
4
5
EC2
โ”‚
โ”‚ id_ed25519
โ–ผ
GitHub
They are separate authentication setups.

โญ 18. New EC2 Checklist

Use this checklist whenever you create a new EC2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[ ] Connect to EC2

[ ] Check Git
git --version

[ ] Install Git if required
sudo apt update
sudo apt install git -y

[ ] Check SSH key
ls -la ~/.ssh

[ ] If key doesn't exist, create it
ssh-keygen

[ ] Get public key
cat ~/.ssh/id_ed25519.pub

[ ] Add public key to GitHub
GitHub โ†’ Settings โ†’ SSH and GPG keys

[ ] Test connection
ssh -T git@github.com

[ ] Clone repository
git clone git@github.com:USERNAME/REPOSITORY.git

[ ] Enter project
cd REPOSITORY

[ ] Verify
git status
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article