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
| Key | Location | Purpose | Share? |
|---|---|---|---|
| Private Key | ~/.ssh/id_ed25519 | Used by EC2 for authentication | โ NEVER |
| Public Key | ~/.ssh/id_ed25519.pub | Added to GitHub | โ Yes |
| known_hosts | ~/.ssh/known_hosts | Stores trusted SSH hosts | ๐ Keep on EC2 |
| authorized_keys | ~/.ssh/authorized_keys | Allows 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 --versionWhat 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 -yThen verify:
1
git --version๐ 3. Check Existing SSH Key
Before creating a new SSH key, check whether one already exists.
1
ls -la ~/.sshLook 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-keygenMeaning
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.pubExample:
1
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...... ec2-github
Copy the entire line.
โ ๏ธ DO NOT run:
1
cat ~/.ssh/id_ed25519That 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.pubThen click:
1
Add SSH key
๐งช 7. Test EC2 โ GitHub SSH Connection
Run:
1
ssh -T git@github.comThe 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.com:USERNAME/REPOSITORY.git
Example:
1
git.com:ankittripathidevs/DevOps_FullCourse.git
Clone:
1
git clone git@github.com:ankittripathidevs/DevOps_FullCourse.gitThen:
1
lsExample:
1
DevOps_FullCourse
Enter the repository:
1
cd DevOps_FullCourseCheck:
1
git statusโ ๏ธ 9. SSH URL Is NOT a Command
This:
1
git.com:ankittripathidevs/DevOps_FullCourse.git
is an SSH Repository URL.
โ Don't run it directly:
1
git.com:ankittripathidevs/DevOps_FullCourse.gitUse 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 -vExample 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.gitVerify:
1
git remote -vExpected:
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 pushBecause 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 --versionInstall Git
1
2
sudo apt update
sudo apt install git -yCheck SSH keys
1
ls -la ~/.sshCreate SSH key
1
ssh-keygen -t ed25519 -C "ec2-github"Show public key
1
cat ~/.ssh/id_ed25519.pubTest GitHub SSH
1
ssh -T git@github.comClone repository
1
git clone git@github.com:USERNAME/REPOSITORY.gitCheck remote
1
git remote -vChange HTTPS โ SSH
1
git remote set-url origin git@github.com:USERNAME/REPOSITORY.gitPull
1
git pullPush
1
git pushโ ๏ธ 16. Common Problems
Problem 1 โ Permission denied (publickey)
Error:
1
git.com: Permission denied (publickey).
Test:
1
ssh -T git@github.comIf authentication fails:
- Check SSH key:
1
ls -la ~/.ssh- Get public key:
1
cat ~/.ssh/id_ed25519.pub- Add that public key to GitHub.
- Test again:
1
ssh -T git@github.comProblem 2 โ Git asks for GitHub username
Error:
1
Username for 'https://github.com':
Check:
1
git remote -vIf you see:
1
https://github.com/...
change it to SSH:
1
git remote set-url origin git@github.com:USERNAME/REPOSITORY.gitProblem 3 โ SSH URL typed directly
Wrong:
1
git.com:USERNAME/REPOSITORY.gitCorrect:
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
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article