DEV Community

arafatruetbd
arafatruetbd

Posted on

🚀 How to Set Up SSH Keys for GitLab (Step by Step)

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

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

Then add your key:

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

📋 Step 4: Copy your public key

Run:

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

Copy the output (the whole line starting with ssh-ed25519).


🔗 Step 5: Add the key to GitLab

  1. Log in to GitLab.
  2. Go to User Settings → SSH Keys.
  3. Paste your public key.
  4. Add a title and click Add key.

✅ Step 6: Test the connection

Run:

ssh -T git@gitlab.com
Enter fullscreen mode Exit fullscreen mode

If successful, you’ll see:

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

📥 Step 7: Clone repositories with SSH

Now you can clone any repo without typing passwords:

git clone git@gitlab.com:groupname/reponame.git
Enter fullscreen mode Exit fullscreen mode

🎯 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)