Using SSH keys with GitLab is a secure and convenient way to authenticate without entering your username and password every time you push or pull code. By configuring SSH authentication, you streamline your workflow while improving security.
In this guide, you’ll learn how to generate and add an SSH key to your GitLab account.
🧰 Prerequisites
- Git is installed on your local machine
- A GitLab account
- A terminal (Command Prompt, Git Bash, or Terminal on Mac/Linux)
✅ Step 1: Check for Existing SSH Keys
Open your terminal and run:
ls ~/.ssh
Look for files like id_rsa
and id_rsa.pub
(or id_ed25519
and id_ed25519.pub
). If they exist, you already have an SSH key pair.
✅ Step 2: Generate a New SSH Key (If Needed)
If you don’t have an SSH key, generate one:
ssh-keygen -t ed25519 -C "yo********@ex*****.com"
Or for RSA (older method):
ssh-keygen -t rsa -b 4096 -C "yo********@ex*****.com"
When prompted:
- Press Enter to accept the default file location
- Optionally, set a passphrase for added security
This generates:
- A private key:
~/.ssh/id_ed25519
- A public key:
~/.ssh/id_ed25519.pub
✅ Step 3: Copy the Public SSH Key
Copy the contents of your public key file:
On Linux/macOS:
cat ~/.ssh/id_ed25519.pub | pbcopy # (or use xclip if on Linux)
On Windows (Git Bash):
clip < ~/.ssh/id_ed25519.pub
Or open the file and copy it manually:
cat ~/.ssh/id_ed25519.pub
✅ Step 4: Add the SSH Key to GitLab
- Log in to your GitLab account.
- Click your profile icon > Edit Profile > SSH Keys or go directly to:
https://gitlab.com/-/profile/keys
- Paste the public key into the Key field.
- (Optional) Add a title for identification (e.g., “Work Laptop”).
- Click Add Key.
✅ Step 5: Test the Connection
Run this command in your terminal:
ssh -T gi*@gi****.com
If everything is set up correctly, you’ll see:
Welcome to GitLab, @yourusername!
🧠 Tips & Best Practices
- Never share your private key—only the
.pub
file is meant to be shared. - You can generate and use multiple keys for different machines or projects.
- Use SSH config files (
~/.ssh/config
) to manage multiple identities more easily.
Example config:
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519
✅ Summary
Step | Command / Action |
---|---|
Check for SSH keys | ls ~/.ssh |
Generate SSH key | ssh-keygen -t ed25519 -C "yo********@ex*****.com" |
Copy public key | clip < ~/.ssh/id_ed25519.pub (Windows) |
Add to GitLab | Paste key at https://gitlab.com/-/profile/keys |
Test connection | ssh -T gi*@gi****.com |
🚀 Final Thoughts
Setting up SSH authentication with GitLab enhances both security and productivity. Once configured, you can push and pull code without repeatedly entering your credentials—making collaboration smoother and development faster.