This guide provides a straightforward process for creating and using a dedicated SSH key for GitLab on both Windows and Linux-based systems (including macOS and WSL).
1. Generate a New SSH Key
It's a best practice to create a new, uniquely named key for each service you use. We'll name our key id_gitlab.
For Windows (CMD / PowerShell)
-
Open Command Prompt or PowerShell.
-
Run the following command to start the key generation process:Bash
ssh-keygen -t ed25519 -C "gitlab-enlivensupport" -f %USERPROFILE%\\.ssh\\id_gitlab -
When prompted, you can enter a secure passphrase. This adds an extra layer of security. Press Enter to skip.
This creates two files in your C:\\Users\\YourName\\.ssh\\ folder:
id_gitlab???? (Your private key - never share this!)id_gitlab.pub???? (Your public key - this is what you give to GitLab)
For Linux, macOS, or WSL (Windows Subsystem for Linux)
-
Open your terminal.
-
Run this command:Bash
ssh-keygen -t ed25519 -C "gitlab-enlivensupport" -f ~/.ssh/id_gitlab -
When prompted, enter a secure passphrase or press Enter to leave it empty.
This creates two files in your /home/your-user/.ssh/ directory:
~/.ssh/id_gitlab???? (Your private key)~/.ssh/id_gitlab.pub???? (Your public key)
2. Add Your Public Key to GitLab
Now, you need to tell GitLab about your new key.
-
Copy your public key's content.
-
On Windows: Bash
type %USERPROFILE%\\.ssh\\id_gitlab.pub -
On Linux/macOS/WSL: Bash
cat ~/.ssh/id_gitlab.pub
-
-
Select and copy the entire output, which starts with
ssh-ed25519... -
In GitLab, navigate to your profile:
- Click your avatar in the top-right corner → Edit profile.
- On the left sidebar, click SSH Keys.
- Paste your key into the "Key" box, give it a "Title" (e.g., "Work Laptop"), and click Add key.
3. Configure SSH to Use Your New Key
Because you used a custom key name, you need to tell SSH when to use it. The best way is with an SSH config file.
-
Open or create the SSH config file.
-
On Windows (PowerShell): PowerShell
if (-not (Test-Path $env:USERPROFILE\\.ssh\\config)) { New-Item $env:USERPROFILE\\.ssh\\config } notepad $env:USERPROFILE\\.ssh\\config -
On Linux/macOS/WSL: Bash
touch ~/.ssh/confignano ~/.ssh/config
-
-
Add the following configuration block: This tells SSH to automatically use your
id_gitlabkey whenever you connect togitlab.com.Ini, TOMLHost gitlab.comHostName gitlab.comIdentityFile ~/.ssh/id_gitlabIdentitiesOnly yesNote: On Windows, SSH understands the
~/path correctly, so you can use the same text. -
Save the file and exit the editor.
4. Test Your Connection
Finally, verify that everything is configured correctly.
Bash
ssh -T git@gitlab.com
If successful, you will see a "Welcome..." message from GitLab. You're now ready to clone, pull, and push to your repositories using SSH!
Bonus: How to Add Your Public Key to a Server for SSH Login
To access a remote server using your new key, you need to copy your public key to it.
Method 1: The Easy Way (ssh-copy-id)
If you're on Linux, macOS, or WSL, this is the recommended method.
Bash
ssh-copy-id -i ~/.ssh/id_gitlab.pub username@server_ip
Example:
Bash
ssh-copy-id -i ~/.ssh/id_gitlab.pub root@164.68.119.179
This command automatically appends your public key to the /root/.ssh/authorized_keys file on the remote server with the correct permissions.
Method 2: The Manual Way
If ssh-copy-id isn't available (like on Windows CMD), follow these steps.
-
Get your public key's content. Bash
cat ~/.ssh/id_gitlab.pub -
Copy the entire
ssh-ed25519...line. -
SSH into your remote server using your existing password method.Bash
ssh username@server_ip -
On the remote server, run the following commands to append your key and set the correct permissions: Bash
Create the .ssh directory if it doesn't exist
mkdir -p ~/.ssh
Append your public key to the authorized_keys file
echo "PASTE_YOUR_PUBLIC_KEY_HERE" >> ~/.ssh/authorized_keys
Set correct, secure permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys -
Exit the server and try logging in again. It should now use your key instead of asking for a password.