When working with GitLab, using SSH is the most secure and convenient way to interact with your repositories. Instead of typing your username and password every time, SSH keys let you connect with just a command.
In this guide, I’ll show you how to set up SSH keys for GitLab from scratch.
🔑 Step 1: Check if you already have SSH keys
Open a terminal and run:
ls -al ~/.ssh
If you see files like id_rsa.pub
or id_ed25519.pub
, you may already have a key. Otherwise, let’s generate a new one.
🛠️ Step 2: Generate a new SSH key
Run this command (replace the email with yours):
ssh-keygen -t ed25519 -C "your.email@example.com"
- Press Enter to accept the default file path.
- Optionally set a passphrase for extra security.
This creates two files:
-
id_ed25519
→ your private key (keep it secret!) -
id_ed25519.pub
→ your public key (this goes to GitLab).
⚙️ Step 3: Add your SSH key to the ssh-agent
Start the agent:
eval "$(ssh-agent -s)"
Then add your key:
ssh-add ~/.ssh/id_ed25519
📋 Step 4: Copy your public key
Run:
cat ~/.ssh/id_ed25519.pub
Copy the output (the whole line starting with ssh-ed25519
).
🔗 Step 5: Add the key to GitLab
- Log in to GitLab.
- Go to User Settings → SSH Keys.
- Paste your public key.
- Add a title and click Add key.
✅ Step 6: Test the connection
Run:
ssh -T git@gitlab.com
If successful, you’ll see:
Welcome to GitLab, @yourusername!
📥 Step 7: Clone repositories with SSH
Now you can clone any repo without typing passwords:
git clone git@gitlab.com:groupname/reponame.git
🎯 Final Notes
- Always keep your private key safe and never share it.
- If you work on multiple machines, you’ll need to add a key for each one.
- You can use SSH for both GitLab.com and self-hosted GitLab instances.
🔥 That’s it! You’re ready to use SSH with GitLab.
I’d love to hear your thoughts—drop a comment if you tried this or faced any issues.
Top comments (0)