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)