If you’re working with private GitLab repositories, you’ll often need to authenticate before cloning or pushing code.
Instead of entering your username and password every time, you can securely connect using SSH keys.
This guide will walk you through how to generate SSH keys, add them to GitLab, and clone private repositories using SSH.
🧠 What is SSH?
SSH (Secure Shell) allows secure communication between your computer and GitLab’s servers.
Instead of using your password for every Git action, GitLab uses your SSH key pair — a public key (stored on GitLab) and a private key (stored safely on your machine).
🧩 Step 1: Check for Existing SSH Keys
Before generating a new SSH key, check if you already have one:
ls -al ~/.ssh
If you see files like:
id_ed25519.pub
id_ed25519
or
id_rsa.pub
id_rsa
you already have SSH keys.
You can use them — or generate new ones for GitLab.
⚙️ Step 2: Generate a New SSH Key
Generate a new SSH key using the Ed25519 algorithm (recommended):
ssh-keygen -t ed25519 -C "you@example.com"
If your system doesn’t support Ed25519, use RSA:
ssh-keygen -t rsa -b 4096 -C "you@example.com"
When prompted:
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Press Enter to accept the default location.
You can also set a passphrase for extra security (optional).
💾 Step 3: Start the SSH Agent and Add Your Key
macOS / Linux
Run the following:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Windows (Git Bash)
If you’re using Git Bash, do the same:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
📋 Step 4: Copy Your Public SSH Key
Now copy your public key to your clipboard.
macOS
pbcopy < ~/.ssh/id_ed25519.pub
Linux
cat ~/.ssh/id_ed25519.pub
Then manually copy the output.
Windows (Git Bash)
cat ~/.ssh/id_ed25519.pub | clip
🌐 Step 5: Add Your SSH Key to GitLab
- Go to your GitLab account.
- Navigate to User Settings → SSH Keys.
- Paste your public key into the Key field.
- Optionally, give it a title (e.g., “My Laptop”).
- Click Add key.
✅ Your SSH key is now linked to your GitLab account.
🧪 Step 6: Test Your SSH Connection
Run this command to confirm the connection:
ssh -T git@gitlab.com
You should see a message like:
Welcome to GitLab, @yourusername!
That means everything is working 🎉
💻 Step 7: Clone a Private Repository Using SSH
Now you can clone private repositories securely.
Find your SSH clone URL in GitLab:
It looks like:
git@gitlab.com:username/project-name.git
Then run:
git clone git@gitlab.com:username/project-name.git
You’ll no longer need to enter your GitLab credentials every time you push or pull code.
🔧 Optional: Manage Multiple SSH Keys (If You Have Multiple Accounts)
If you use different GitLab or GitHub accounts, create a custom SSH config file:
Edit your config:
nano ~/.ssh/config
Add:
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519
Save and exit.
Now Git will automatically use the right SSH key when connecting to GitLab.
✅ Final Thoughts
You’ve now configured SSH authentication for GitLab — no more typing credentials every time you push or clone.
This setup is:
- 🔒 More secure than HTTPS authentication
- ⚡ Faster and easier for private repos
- 💼 Ideal for teams and CI/CD pipelines
Now go ahead and clone your project the secure way! 🚀
Top comments (0)