Coding

GitLab SSH Key Setup: Generate, Add and Test (Step by Step)

For a GitLab SSH key setup, run ssh-keygen -t ed25519 -C "your-comment", copy the .pub file, and paste it under Edit profile > Access > SSH keys > Add new key on GitLab. Then run ssh -T [email protected]; a message that starts with "Welcome to GitLab" means it works.

The steps below follow GitLab's own documentation and were checked against it in November 2026. If you already have a key pair, skip to step 2. If you also use GitHub, the process is nearly the same; see how to add an SSH key to GitHub for the differences in menu names.

Step 0: Check that your SSH client is new enough

GitLab requires an OpenSSH client, version 6.5 or later for the recommended key type. Check yours:

ssh -V

Windows 10 and 11 ship an OpenSSH client, and Git for Windows includes one in Git Bash. On macOS and most Linux distributions it is already installed.

Step 1: Generate an SSH key pair

GitLab's documentation lists these key types:

TypeGitLab's guidance
ED25519Preferred. More secure and faster than RSA. Needs OpenSSH 6.5 or later. May not be fully supported on some FIPS systems.
ED25519_SK, ECDSA_SKHardware security keys. Need OpenSSH 8.2 or later on the client and on the GitLab server.
RSALess secure than ED25519. Use at least 4096 bits; the maximum GitLab accepts is 8192 bits.
ECDSACarries the same security concerns that apply to DSA.
DSADeprecated.

Generate an ED25519 key. The text after -C is a comment that helps you recognise the key later, such as an email address or a device name:

ssh-keygen -t ed25519 -C "work-laptop"

If your system cannot use ED25519, for example because of FIPS restrictions, generate a 4096-bit RSA key instead:

ssh-keygen -t rsa -b 4096 -C "work-laptop"

When ssh-keygen asks where to save the file, press Enter to accept the default (~/.ssh/id_ed25519). Change the path only if you are creating a deploy key or keeping a second key, so you don't overwrite an existing one. Then set a passphrase when prompted. A passphrase protects the private key if someone copies the file.

This creates two files. id_ed25519 is the private key and must stay on this computer. id_ed25519.pub is the public key, and it is the one you give to GitLab. For more on the command and its options, see how to generate an Ed25519 SSH key.

To avoid typing the passphrase for every Git command, load the key into your SSH agent:

ssh-add ~/.ssh/id_ed25519

Step 2: Copy the public key

Copy the contents of the .pub file. GitLab's documentation gives these commands:

WhereCommand
macOStr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopy
Linux (needs xclip)xclip -sel clip < ~/.ssh/id_ed25519.pub
Git Bash on Windowscat ~/.ssh/id_ed25519.pub | clip

If none of these works, open the file in a text editor and copy the whole line. It starts with ssh-ed25519 for an ED25519 key. Use the .pub file only. GitLab's documentation is explicit that you must never paste the private key.

Step 3: Add the key to your GitLab account

  1. Select your avatar, then Edit profile.
  2. In the left sidebar, select Access > SSH keys.
  3. Select Add new key.
  4. Paste the public key into the Key box.
  5. Enter a Title, such as the name of the device. The title is for you; GitLab doesn't use it for authentication.
  6. Choose a Usage type: Authentication, Signing, or both. The default is both.
  7. Optionally change the Expiration date.
  8. Select Add key.

The same key can be used on every project and group your account can access, so you add it once per computer, not once per repository. GitLab also rejects keys that are known to be compromised. If yours is blocked, generate a new pair.

Step 4: Test the connection

Before you type yes at the first-connection prompt, compare the host key fingerprint with the one GitLab publishes. For GitLab.com, the ED25519 fingerprint in GitLab's documentation is:

SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8

Fingerprints can change, so confirm it on the GitLab.com settings page rather than trusting a copy. For a self-managed instance, the fingerprints are on the instance's own /help/instance_configuration#ssh-host-keys-fingerprints page. Then run:

ssh -T [email protected]

Use git as the user name, not your GitLab user name. On a self-managed or Dedicated instance, replace gitlab.com with your instance's host name. A working connection prints (example output):

Welcome to GitLab, @your-username!

If you don't see that message, move on to the troubleshooting section below.

Step 5: Clone or switch a repository to SSH

On a project page, select Code and copy the address under Clone with SSH. It looks like this (example):

git clone [email protected]:your-group/your-project.git

A repository you cloned over HTTPS keeps using HTTPS until you change its remote. Run this inside the project folder, with the SSH address from the Code menu:

git remote set-url origin [email protected]:your-group/your-project.git
git remote -v

The second command lists the fetch and push URLs so you can confirm the change. Once the remote is correct, pushing works the same way as on any other Git host: git push uses the key instead of a password.

Troubleshooting a GitLab SSH key

Permission denied (publickey)

GitLab's troubleshooting page lists these checks:

  • Confirm the public key is added to your GitLab account and that its type is supported, with no unrecognised headers.
  • Confirm the private key exists and that you can read it.
  • Fix permissions. The private key should be 600 and the .ssh directory 700. On Linux and macOS: chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_ed25519.
  • Load the key into ssh-agent. This is often needed after a reboot or in a new terminal session.
  • If you have several keys, SSH may offer the wrong one. By default it tries files such as ~/.ssh/id_rsa and ~/.ssh/id_ed25519, so name the key you want. See the SSH config file guide for a per-host IdentityFile setting.

A password prompt when you clone

If git clone asks for [email protected]'s password:, GitLab's documentation says this points to an SSH setup problem, not a wrong password. Check that the public key is on your profile, register the private key with ssh-agent, and look at the verbose output. ED25519 may also fail on some FIPS systems; if so, use a 4096-bit RSA key.

See what SSH is doing

Add verbosity to the test command. Replace the host with your instance if it is not GitLab.com:

ssh -Tvvv [email protected]

The output shows which key files SSH tries and why the server rejects them.

Could not resolve hostname

The full error reads ssh: Could not resolve hostname gitlab.example.com: nodename nor servname provided, or not known. If you copied a clone URL, test with only the host, for example ssh -T [email protected], not the repository path. Otherwise check the spelling of the instance URL, your VPN if the instance is private, and restart the terminal to clear a stale name cache.

Key enrollment failed: invalid format

This appears when creating a hardware-backed key. GitLab suggests running ssh-keygen with sudo, confirming that your FIDO2 security key supports the key type you asked for, and checking that ssh -V reports OpenSSH 8.2 or later.

Port 22 is blocked on your network

GitLab.com also accepts SSH on port 443 at the host name altssh.gitlab.com, which can help on networks that block port 22. GitLab's pages I checked do not show the client configuration for this, so look it up in GitLab's own documentation before relying on it.

Expiry, revoking and deleting keys

If you set an expiration date, the key stops working on that date; add a new one before then. To remove a key you no longer use, delete it from the same SSH keys page. If a key may be compromised and it has the Signing or Authentication and Signing usage type, GitLab lets you revoke it instead. Revoking and deleting are different actions: deleting removes the key permanently.

Frequently asked questions

Which key type should I use for GitLab?
GitLab's documentation lists ED25519 as the preferred type: it is more secure and faster than RSA, and needs OpenSSH 6.5 or later. If you must use RSA, use at least 4096 bits. GitLab accepts RSA keys up to 8192 bits.
Do I paste the private key or the public key into GitLab?
Paste the public key, the file that ends in .pub, such as ~/.ssh/id_ed25519.pub. The private key (the file without .pub) stays on your computer and is never pasted anywhere.
Can one SSH key be used for every GitLab project?
Yes. According to GitLab's documentation, one key works for every project and group that your account can access. You only need a separate key for a deploy key or if you want a different key for one repository.
Can a GitLab SSH key expire?
Yes. The Expiration date field when you add the key is optional. If you set one, GitLab checks daily, sends a notice 7 days before expiry (01:00 UTC) and again on the expiry date (02:00 UTC). An expired key can no longer authenticate or sign commits.
How do I set up an SSH key for a self-managed GitLab instance?
The steps are the same, but you test against your own host name, for example ssh -T [email protected]. Administrators can change the default git user name, so check your instance's clone URL if the test fails.

Sources