The Complete Guide to Setting Up SSH Keys on Ubuntu, Windows, and macOS (With GitLab Integration)
Step 1: Understand What SSH Is
SSH stands for Secure Shell.
It is a secure protocol used to:
- Connect to remote servers
- Clone Git repositories
- Push and pull code securely
- Deploy applications
Instead of using passwords, SSH uses cryptographic key pairs.
Step 2: Understand SSH Key Pair
When you create an SSH key, two files are generated:
| File | Purpose |
|---|---|
id_ed25519 | Private key (Keep secret ❌ Never share) |
id_ed25519.pub | Public key (Safe to share ✅) |
How it works:
- You give your public key to GitLab.
- GitLab stores it.
- When you connect, GitLab verifies you using your private key.
- If matched → Access granted.
No password needed.
Step 3: Why We Use SSH in GitLab
GitLab is used for:
- Hosting repositories
- CI/CD
- Version control
- Collaboration
Without SSH (using HTTPS):
git clone https://gitlab.com/username/project.git
Every push asks for username & password.
With SSH:git clone git@gitlab.com:username/project.git
Push → Done instantly 🚀
That’s why developers prefer SSH.
Step 4: Generate SSH Key on Ubuntu
1️⃣ Open Terminal
Press:Ctrl + Alt + T
2️⃣ Check If SSH Already Existsls -al ~/.ssh
If folder doesn’t exist → no problem.
3️⃣ Generate New SSH Key
Recommended modern algorithm:ssh-keygen -t ed25519 -C "your_email@example.com"
If not supported:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Press Enter to accept default location:/home/username/.ssh/id_ed25519
Set passphrase (optional but recommended).
4️⃣ Start SSH Agenteval "$(ssh-agent -s)"
5️⃣ Add Key to SSH Agent
ssh-add ~/.ssh/id_ed25519
6️⃣ Copy Public Keycat ~/.ssh/id_ed25519.pub
Copy the entire output.
Step 5: Generate SSH Key on Windows
Option 1: Using Git Bash (Recommended)
- Install Git for Windows
- Open Git Bash
Run:ssh-keygen -t ed25519 -C "your_email@example.com"
Keys are stored in:C:\Users\YourUsername\.ssh\
View public key:cat ~/.ssh/id_ed25519.pub
Option 2: Using PowerShell
Open PowerShell and run:
ssh-keygen -t ed25519 -C "your_email@example.com"
Same process..
Step 6: Generate SSH Key on macOS
1️⃣ Open Terminal
Cmd + Space → Terminal
2️⃣ Generate Key
ssh-keygen -t ed25519 -C "your_email@example.com"
3️⃣ Start SSH Agent
eval "$(ssh-agent -s)"
4️⃣ Add Key
ssh-add ~/.ssh/id_ed25519
5️⃣ Copy Public Key
pbcopy < ~/.ssh/id_ed25519.pub
Step 7: Add SSH Key to GitLab
- Login to GitLab
- Click Profile → Preferences
- Click SSH Keys
- Paste copied public key
- Click Add Key
Now GitLab trusts your machine.
Step 8: Test SSH Connection
Run: ssh -T git@gitlab.com
If successful, you’ll see:Welcome to GitLab!
Step 9: Clone Using SSH
Instead of HTTPS:git clone git@gitlab.com:username/project.git
Now: git push
No password required.
Step 10: Security Best Practices
✔ Use ed25519
✔ Protect private key
✔ Add passphrase
✔ Never share private key
✔ Set correct permissions (Linux/macOS):
chmod 700 ~/.sshchmod 600 ~/.ssh/id_ed25519
Bonus: How SSH Works Internally
- Client connects to GitLab
- GitLab checks public key
- GitLab sends encrypted challenge
- Your private key decrypts it
- Verified → Access granted
This uses asymmetric cryptography — extremely secure.
Final Conclusion
Setting up SSH keys:
- Improves security
- Removes password dependency
- Enables automation
- Essential for DevOps
- Industry best practice
If you are serious about professional development, SSH is not optional — it’s foundational