DEV Community

Hiro Ventolero
Hiro Ventolero

Posted on

🔐 How to Set Up SSH Access for a Private GitLab Repository

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
Enter fullscreen mode Exit fullscreen mode

If you see files like:

id_ed25519.pub
id_ed25519
Enter fullscreen mode Exit fullscreen mode

or

id_rsa.pub
id_rsa
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

If your system doesn’t support Ed25519, use RSA:

ssh-keygen -t rsa -b 4096 -C "you@example.com"
Enter fullscreen mode Exit fullscreen mode

When prompted:

Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Windows (Git Bash)

If you’re using Git Bash, do the same:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

📋 Step 4: Copy Your Public SSH Key

Now copy your public key to your clipboard.

macOS

pbcopy < ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Linux

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Then manually copy the output.

Windows (Git Bash)

cat ~/.ssh/id_ed25519.pub | clip
Enter fullscreen mode Exit fullscreen mode

🌐 Step 5: Add Your SSH Key to GitLab

  1. Go to your GitLab account.
  2. Navigate to User Settings → SSH Keys.
  3. Paste your public key into the Key field.
  4. Optionally, give it a title (e.g., “My Laptop”).
  5. 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
Enter fullscreen mode Exit fullscreen mode

You should see a message like:

Welcome to GitLab, @yourusername!
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then run:

git clone git@gitlab.com:username/project-name.git
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Add:

Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

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)