Use SSH keys with GitLab
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
Use SSH keys to securely authenticate with GitLab without entering your username and password each time you push or pull code.
To use SSH keys with GitLab, you must:
- Generate an SSH key pair on your local system.
- Add your SSH key to your GitLab account.
- Verify your connection to GitLab.
For information on advanced SSH key configuration, see advanced SSH key configuration.
What are SSH keys
SSH uses two keys, a public key and a private key.
- The public key can be distributed.
- The private key should be protected.
It is not possible to reveal confidential data by uploading your public key. When you need to copy or upload your SSH public key, make sure you do not accidentally copy or upload your private key instead.
You can use your private key to sign commits, which makes your use of GitLab and your data even more secure. This signature then can be verified by anyone using your public key.
For details, see Asymmetric cryptography, also known as public-key cryptography.
Prerequisites
To use SSH to communicate with GitLab, you need:
- The OpenSSH client, which comes pre-installed on GNU/Linux, macOS, and Windows 10.
- SSH version 6.5 or later. Earlier versions used an MD5 signature, which is not secure.
To view the version of SSH installed on your system, run ssh -V.
Supported SSH key types
To communicate with GitLab, you can use the following SSH key types:
| Algorithm | Notes |
|---|---|
| ED25519 (preferred) | More secure and performant than RSA keys. Introduced in OpenSSH 6.5 (2014) and available on most operating systems. Might not be fully supported by all FIPS systems. For more information, see issue 367429. |
| ED25519_SK | Requires OpenSSH 8.2 or later on both your local client and the GitLab server. |
| ECDSA_SK | Requires OpenSSH 8.2 or later on both your local client and the GitLab server. |
| RSA | Less secure than ED25519. If used, GitLab recommends a key size of at least 2048 bits. Maximum key length is 8192 bits due to Go limitations. Default key size depends on your ssh-keygen version. |
| ECDSA | Security issues related to DSA also apply to ECDSA keys. |
Check for existing SSH key pairs
Before you create a key pair, see if a key pair already exists.
- Go to your home directory.
- Go to the
.ssh/subdirectory. If the.ssh/subdirectory doesn’t exist, you are either not in the home directory, or you haven’t usedsshbefore. In the latter case, you need to generate an SSH key pair. - See if a file with one of the following formats exists:
Algorithm Public key Private key ED25519 (preferred) id_ed25519.pubid_ed25519ED25519_SK id_ed25519_sk.pubid_ed25519_skECDSA_SK id_ecdsa_sk.pubid_ecdsa_skRSA (at least 2048-bit key size) id_rsa.pubid_rsaDSA (deprecated) id_dsa.pubid_dsaECDSA id_ecdsa.pubid_ecdsa
Generate an SSH key pair
If you do not have an existing SSH key pair, generate a new one:
Open a terminal.
Run
ssh-keygen -twith the key type and an optional comment to help identify the key later. A common option is to use your email address as the comment. The comment is included in the.pubfile.For example, for ED25519:
ssh-keygen -t ed25519 -C "<comment>"For 2048-bit RSA:
ssh-keygen -t rsa -b 2048 -C "<comment>"Press Enter. Output similar to the following is displayed:
Generating public/private ed25519 key pair. Enter file in which to save the key (/home/user/.ssh/id_ed25519):Accept the suggested filename and directory, unless you are generating a deploy key or want to save in a specific directory where you store other keys.
You can also dedicate the SSH key pair to a specific host.
Specify a passphrase:
Enter passphrase (empty for no passphrase): Enter same passphrase again:A confirmation is displayed, including information about where your files are stored.
A public and private key are generated. Add the public SSH key to your GitLab account and keep the private key secure.
Add an SSH key to your GitLab account
To use SSH with GitLab, copy your public key to your GitLab account. GitLab cannot access your private key.
When you add an SSH key, GitLab checks it against a list of known compromised keys. You cannot add compromised keys because the associated private keys are publicly known and could be used to access accounts. This restriction cannot be configured.
If your key is blocked, generate a new SSH key pair.
To add an SSH key to your GitLab account:
Copy the contents of your public key file. You can do this manually or use a script.
In these examples, replace
id_ed25519.pubwith your filename. For example, for RSA, useid_rsa.pub.tr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopyxclip -sel clip < ~/.ssh/id_ed25519.pubcat ~/.ssh/id_ed25519.pub | clipSign in to GitLab.
In the upper-right corner, select your avatar.
Select Edit profile.
On the left sidebar, select SSH Keys.
Select Add new key.
In the Key box, paste the contents of your public key. If you manually copied the key, make sure you copy the entire key, which starts with
ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,sk-ecdsa-sha2-nistp256@openssh.com, orsk-ssh-ed25519@openssh.com, and may end with a comment.In the Title box, type a description, like
Work LaptoporHome Workstation.Optional. Select the Usage type of the key. It can be used either for
AuthenticationorSigningor both.Authentication & Signingis the default value.Optional. Update Expiration date to modify the default expiration date. For more information, see SSH key expiration.
Select Add key.
Verify your SSH connection
Verify that your SSH key was added correctly, and that you can connect to the GitLab instance:
- To ensure you connect to the correct server, identify the SSH host key fingerprint:
- For GitLab.com, see the SSH host keys fingerprints documentation.
- For GitLab Self-Managed or GitLab Dedicated, see
https://gitlab.example.com/help/instance_configuration#ssh-host-keys-fingerprintswheregitlab.example.comis the GitLab instance URL.
- Open a terminal and run this command:
- For GitLab.com, use
ssh -T git@gitlab.com. - For GitLab Self-Managed or GitLab Dedicated, use
ssh -T git@gitlab.example.comwheregitlab.example.comis the GitLab instance URL.
- For GitLab.com, use
By default, connections use the git username, but GitLab Self-Managed or GitLab Dedicated administrators
can change the username.
On your first connection, you might need to verify the authenticity of the GitLab host. Follow the on-screen prompts if you see a message like:
The authenticity of host 'gitlab.example.com (35.231.145.151)' can't be established. ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw. Are you sure you want to continue connecting (yes/no)?You should receive a welcome message.
Welcome to GitLab, <username>!If the message doesn’t appear, you can troubleshoot your SSH connection.
View your SSH keys
To view the SSH keys for your account:
- In the upper-right corner, select your avatar.
- Select Edit profile.
- On the left sidebar, select SSH Keys.
Your existing SSH keys are listed at the bottom of the page. The information includes:
- The title for the key
- Public fingerprint
- Permitted usage types
- Creation date
- Last used date
- Expiry date
Remove an SSH key
You can revoke or delete your SSH key to permanently remove it from your account.
Removing your SSH key has additional implications if you sign your commits with the key. For more information, see Signed commits with removed SSH keys.
Revoke an SSH key
If your SSH key becomes compromised, revoke the key.
Prerequisites:
- The SSH key must have the
SigningorAuthentication & Signingusage type.
To revoke an SSH key:
- In the upper-right corner, select your avatar.
- Select Edit profile.
- On the left sidebar, select SSH Keys.
- Next to the SSH key you want to revoke, select Revoke.
- Select Revoke.
Delete an SSH key
To delete an SSH key:
- In the upper-right corner, select your avatar.
- Select Edit profile.
- On the left sidebar, select SSH Keys.
- Next to the key you want to delete, select Remove ( ).
- Select Delete.
SSH key expiration
You can set an expiration date when you add an SSH key to your account. This optional setting helps limit the risk of a security breach.
After your SSH key expires, you can no longer use it to authenticate or sign commits. You must generate a new SSH key and add it to your account.
On GitLab Self-Managed and GitLab Dedicated, administrators can view expiration dates and use them for guidance when deleting keys.
GitLab checks daily for expiring SSH keys and sends notifications:
- At 01:00 AM UTC, seven days before expiration.
- At 02:00 AM UTC on the expiration date.