Gitlab add ssh key windows 10

GitLab CE Mirror | Please open new issues in our issue tracker on GitLab.com - gitlabhq/ssh.md at master · gitlabhq/gitlabhq
stage group info

Manage

Authentication and Authorization

To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments

Use SSH keys to communicate with GitLab (FREE)

Git is a distributed version control system, which means you can work locally,
then share or push your changes to a server. In this case, the server you push to is GitLab.

GitLab uses the SSH protocol to securely communicate with Git.
When you use SSH keys to authenticate to the GitLab remote server,
you don’t need to supply your username and password each time.

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:

  • ED25519
  • ED25519_SK (Available in GitLab 14.8 and later.)
  • ECDSA_SK (Available in GitLab 14.8 and later.)
  • RSA
  • DSA (Deprecated in GitLab 11.0.)
  • ECDSA (As noted in Practical Cryptography With Go, the security issues related to DSA also apply to ECDSA.)

Administrators can restrict which keys are permitted and their minimum lengths.

ED25519 SSH keys

The book Practical Cryptography With Go
suggests that ED25519 keys are more secure and performant than RSA keys.

OpenSSH 6.5 introduced ED25519 SSH keys in 2014, and they should be available on most
operating systems.

ED25519_SK SSH keys

Introduced in GitLab 14.8.

To use ED25519_SK SSH keys on GitLab, your local client and GitLab server
must have OpenSSH 8.2 or later installed.

ECDSA_SK SSH keys

Introduced in GitLab 14.8.

To use ECDSA_SK SSH keys on GitLab, your local client and GitLab server
must have OpenSSH 8.2 or later installed.

RSA SSH keys

Available documentation suggests ED25519 is more secure than RSA.

If you use an RSA key, the US National Institute of Science and Technology in
Publication 800-57 Part 3 (PDF)
recommends a key size of at least 2048 bits. The default key size depends on your version of ssh-keygen.
Review the man page for your installed ssh-keygen command for details.

See if you have an existing SSH key pair

Before you create a key pair, see if a key pair already exists.

  1. Go to your home directory.

  2. Go to the .ssh/ subdirectory. If the .ssh/ subdirectory doesn’t exist,
    you are either not in the home directory, or you haven’t used ssh before.
    In the latter case, you need to generate an SSH key pair.

  3. See if a file with one of the following formats exists:

    Algorithm Public key Private key
    ED25519 (preferred) id_ed25519.pub id_ed25519
    ED25519_SK id_ed25519_sk.pub id_ed25519_sk
    ECDSA_SK id_ecdsa_sk.pub id_ecdsa_sk
    RSA (at least 2048-bit key size) id_rsa.pub id_rsa
    DSA (deprecated) id_dsa.pub id_dsa
    ECDSA id_ecdsa.pub id_ecdsa

Generate an SSH key pair

If you do not have an existing SSH key pair, generate a new one:

  1. Open a terminal.

  2. Run ssh-keygen -t followed by the key type and an optional comment.
    This comment is included in the .pub file that’s created.
    You may want to use an email address for the comment.

    For example, for ED25519:

    ssh-keygen -t ed25519 -C "<comment>"

    For 2048-bit RSA:

    ssh-keygen -t rsa -b 2048 -C "<comment>"
  3. 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):
    
  4. 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.

  5. 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.

Configure SSH to point to a different directory

If you did not save your SSH key pair in the default directory,
configure your SSH client to point to the directory where the private key is stored.

  1. Open a terminal and run this command:

    eval $(ssh-agent -s)
    ssh-add <directory to private SSH key>
  2. Save these settings in the ~/.ssh/config file. For example:

    # GitLab.com
    Host gitlab.com
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/gitlab_com_rsa
    
    # Private GitLab instance
    Host gitlab.company.com
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/example_com_rsa
    

For more information on these settings, see the man ssh_config page in the SSH configuration manual.

Public SSH keys must be unique to GitLab because they bind to your account.
Your SSH key is the only identifier you have when you push code with SSH.
It must uniquely map to a single user.

Update your SSH key passphrase

You can update the passphrase for your SSH key:

  1. Open a terminal and run this command:

    ssh-keygen -p -f /path/to/ssh_key
  2. At the prompts, enter the passphrase and then press Enter.

Upgrade your RSA key pair to a more secure format

If your version of OpenSSH is between 6.5 and 7.8, you can save your private
RSA SSH keys in a more secure OpenSSH format by opening a terminal and running
this command:

ssh-keygen -o -f ~/.ssh/id_rsa

Alternatively, you can generate a new RSA key with the more secure encryption format with
the following command:

ssh-keygen -o -t rsa -b 4096 -C "<comment>"

Generate an SSH key pair for a FIDO/U2F hardware security key

To generate ED25519_SK or ECDSA_SK SSH keys, you must use OpenSSH 8.2 or later:

  1. Insert a hardware security key into your computer.

  2. Open a terminal.

  3. Run ssh-keygen -t followed by the key type and an optional comment.
    This comment is included in the .pub file that’s created.
    You may want to use an email address for the comment.

    For example, for ED25519_SK:

    ssh-keygen -t ed25519-sk -C "<comment>"

    For ECDSA_SK:

    ssh-keygen -t ecdsa-sk -C "<comment>"

    If your security key supports FIDO2 resident keys, you can enable this when
    creating your SSH key:

    ssh-keygen -t ed25519-sk -O resident -C "<comment>"

    -O resident indicates that the key should be stored on the FIDO authenticator itself.
    Resident key is easier to import to a new computer because it can be loaded directly
    from the security key by ssh-add -K
    or ssh-keygen -K.

  4. Press Enter. Output similar to the following is displayed:

    Generating public/private ed25519-sk key pair.
    You may need to touch your authenticator to authorize key generation.
    
  5. Touch the button on the hardware security key.

  6. Accept the suggested filename and directory:

    Enter file in which to save the key (/home/user/.ssh/id_ed25519_sk):
    
  7. 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.

Generate an SSH key pair with a password manager

Generate an SSH key pair with 1Password

You can use 1Password and the 1Password browser extension to either:

  • Automatically generate a new SSH key.
  • Use an existing SSH in your 1Password vault to authenticate with GitLab.
  1. Sign in to GitLab.
  2. On the top bar, in the upper-right corner, select your avatar.
  3. Select Preferences.
  4. On the left sidebar, select SSH Keys.
  5. Select Key, and you should see the 1Password helper appear.
  6. Select the 1Password icon and unlock 1Password.
  7. You can then select Create SSH Key or select an existing SSH key to fill in the public key.
  8. In the Title box, type a description, like Work Laptop or
    Home Workstation.
  9. Optional. Select the Usage type of the key. It can be used either for Authentication or Signing or both. Authentication & Signing is the default value.
  10. Optional. Update Expiration date to modify the default expiration date.
  11. Select Add key.

To learn more about using 1Password with SSH keys, see 1Password’s documentation.

Add an SSH key to your GitLab account

  • Suggested default expiration date for keys introduced in GitLab 15.4.
  • Usage types for SSH keys added in GitLab 15.7.

To use SSH with GitLab, copy your public key to your GitLab account:

  1. Copy the contents of your public key file. You can do this manually or use a script.
    For example, to copy an ED25519 key to the clipboard:

    macOS

    tr -d 'n' < ~/.ssh/id_ed25519.pub | pbcopy

    Linux (requires the xclip package)

    xclip -sel clip < ~/.ssh/id_ed25519.pub

    Git Bash on Windows

    cat ~/.ssh/id_ed25519.pub | clip

    Replace id_ed25519.pub with your filename. For example, use id_rsa.pub for RSA.

  2. Sign in to GitLab.

  3. On the top bar, in the upper-right corner, select your avatar.

  4. Select Preferences.

  5. On the left sidebar, select SSH Keys.

  6. 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, or sk-ssh-ed25519@openssh.com, and may end with a comment.

  7. In the Title box, type a description, like Work Laptop or
    Home Workstation.

  8. Optional. Select the Usage type of the key. It can be used either for Authentication or Signing or both. Authentication & Signing is the default value.

  9. Optional. Update Expiration date to modify the default expiration date.
    In:

    • GitLab 13.12 and earlier, the expiration date is informational only. It doesn’t prevent
      you from using the key. Administrators can view expiration dates and use them for
      guidance when deleting keys.
    • GitLab checks all SSH keys at 02:00 AM UTC every day. It emails an expiration notice for all SSH keys that expire on the current date. (Introduced in GitLab 13.11.)
    • GitLab checks all SSH keys at 01:00 AM UTC every day. It emails an expiration notice for all SSH keys that are scheduled to expire seven days from now. (Introduced in GitLab 13.11.)
  10. Select Add key.

Verify that you can connect

Verify that your SSH key was added correctly.

The following commands use the example hostname gitlab.example.com. Replace this example hostname with your GitLab instance’s hostname, for example, git@gitlab.com.

  1. To ensure you’re connecting to the correct server, check the server’s SSH host keys fingerprint. For:

    • GitLab.com, see the SSH host keys fingerprints documentation.
    • GitLab.com or another GitLab instance, see gitlab.example.com/help/instance_configuration#ssh-host-keys-fingerprints where gitlab.example.com is gitlab.com (for
      GitLab.com) or the address of the GitLab instance.
  2. Open a terminal and run this command, replacing gitlab.example.com with your
    GitLab instance URL:

    ssh -T git@gitlab.example.com
  3. If this is the first time you connect, you should verify the
    authenticity of the GitLab host. 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)? yes
    Warning: Permanently added 'gitlab.example.com' (ECDSA) to the list of known hosts.
    

    Type yes and press Enter.

  4. Run the ssh -T git@gitlab.example.com command again. You should receive a Welcome to GitLab, @username! message.

If the welcome message doesn’t appear, you can troubleshoot by running ssh
in verbose mode:

ssh -Tvvv git@gitlab.example.com

Use different keys for different repositories

You can use a different key for each repository.

Open a terminal and run this command:

git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/private-key-filename-for-this-repository -F /dev/null"

This command does not use the SSH Agent and requires Git 2.10 or later. For more information
on ssh command options, see the man pages for both ssh and ssh_config.

View your account’s SSH keys

  1. Sign in to GitLab.
  2. On the top bar, in the upper-right corner, select your avatar.
  3. Select Preferences.
  4. On the left sidebar, select SSH Keys.

Your existing SSH keys are listed at the bottom of the page. The information includes:

  • The key’s:
    • Name.
    • Public fingerprint.
    • Expiry date.
    • Permitted usage types.
  • The time a key was last used. On GitLab.com this value is unavailable, and you are unable to see if or when an SSH key has been used. For more information, see issue 324764.

Select Delete to permanently delete an SSH key.

Use different accounts on a single GitLab instance

You can use multiple accounts to connect to a single instance of GitLab. You
can do this by using the command in the previous topic.
However, even if you set IdentitiesOnly to yes, you cannot sign in if an
IdentityFile exists outside of a Host block.

Instead, you can assign aliases to hosts in the ~/.ssh/config file.

  • For the Host, use an alias like user_1.gitlab.com and
    user_2.gitlab.com. Advanced configurations
    are more difficult to maintain, and these strings are easier to
    understand when you use tools like git remote.
  • For the IdentityFile, use the path the private key.
# User1 Account Identity
Host <user_1.gitlab.com>
  Hostname gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/<example_ssh_key1>

# User2 Account Identity
Host <user_2.gitlab.com>
  Hostname gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/<example_ssh_key2>

Now, to clone a repository for user_1, use user_1.gitlab.com in the git clone command:

git clone git@<user_1.gitlab.com>:gitlab-org/gitlab.git

To update a previously-cloned repository that is aliased as origin:

git remote set-url origin git@<user_1.gitlab.com>:gitlab-org/gitlab.git

NOTE:
Private and public keys contain sensitive data. Ensure the permissions
on the files make them readable to you but not accessible to others.

Configure two-factor authentication (2FA)

You can set up two-factor authentication (2FA) for
Git over SSH. We recommend using
ED25519_SK or ECDSA_SK SSH keys.

Use EGit on Eclipse

If you are using EGit, you can add your SSH key to Eclipse.

Use SSH on Microsoft Windows

If you’re running Windows 10, you can either use the Windows Subsystem for Linux (WSL)
with WSL 2 which
has both git and ssh preinstalled, or install Git for Windows to
use SSH through PowerShell.

The SSH key generated in WSL is not directly available for Git for Windows, and vice versa,
as both have a different home directory:

  • WSL: /home/<user>
  • Git for Windows: C:Users<user>

You can either copy over the .ssh/ directory to use the same key, or generate a key in each environment.

If you’re running Windows 11 and using OpenSSH for Windows, ensure the HOME
environment variable is set correctly. Otherwise, your private SSH key might not be found.

Alternative tools include:

  • Cygwin
  • PuttyGen

Overriding SSH settings on the GitLab server

GitLab integrates with the system-installed SSH daemon and designates a user
(typically named git) through which all access requests are handled. Users
who connect to the GitLab server over SSH are identified by their SSH key instead
of their username.

SSH client operations performed on the GitLab server are executed as this
user. You can modify this SSH configuration. For example, you can specify
a private SSH key for this user to use for authentication requests. However, this practice
is not supported and is strongly discouraged as it presents significant
security risks.

GitLab checks for this condition, and directs you
to this section if your server is configured this way. For example:

$ gitlab-rake gitlab:check

Git user has default SSH configuration? ... no
  Try fixing it:
  mkdir ~/gitlab-check-backup-1504540051
  sudo mv /var/lib/git/.ssh/id_rsa ~/gitlab-check-backup-1504540051
  sudo mv /var/lib/git/.ssh/id_rsa.pub ~/gitlab-check-backup-1504540051
  For more information see:
  doc/user/ssh.md#overriding-ssh-settings-on-the-gitlab-server
  Please fix the error above and rerun the checks.

Remove the custom configuration as soon as you can. These customizations
are explicitly not supported and may stop working at any time.

Troubleshooting

Password prompt with git clone

When you run git clone, you may be prompted for a password, like git@gitlab.example.com's password:.
This indicates that something is wrong with your SSH setup.

  • Ensure that you generated your SSH key pair correctly and added the public SSH
    key to your GitLab profile.
  • Try to manually register your private SSH key by using ssh-agent.
  • Try to debug the connection by running ssh -Tv git@example.com.
    Replace example.com with your GitLab URL.
  • Ensure you followed all the instructions in Use SSH on Microsoft Windows.

Could not resolve hostname error

You may receive the following error when verifying that you can connect:

ssh: Could not resolve hostname gitlab.example.com: nodename nor servname provided, or not known

If you receive this error, restart your terminal and try the command again.

Key enrollment failed: invalid format error

You may receive the following error when generating an SSH key pair for a FIDO/U2F hardware security key:

Key enrollment failed: invalid format

You can troubleshoot this by trying the following:

  • Run the ssh-keygen command using sudo.
  • Verify your IDO/U2F hardware security key supports
    the key type provided.
  • Verify the version of OpenSSH is 8.2 or greater by
    running ssh -V.

Use SSH keys to communicate with GitLab (FREE)

Git is a distributed version control system, which means you can work locally,
then share or push your changes to a server. In this case, the server you push to is GitLab.

GitLab uses the SSH protocol to securely communicate with Git.
When you use SSH keys to authenticate to the GitLab remote server,
you don’t need to supply your username and password each time.

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:

  • ED25519
  • ED25519_SK (Available in GitLab 14.8 and later.)
  • ECDSA_SK (Available in GitLab 14.8 and later.)
  • RSA
  • DSA (Deprecated in GitLab 11.0.)
  • ECDSA (As noted in Practical Cryptography With Go, the security issues related to DSA also apply to ECDSA.)

Administrators can restrict which keys are permitted and their minimum lengths.

ED25519 SSH keys

The book Practical Cryptography With Go
suggests that ED25519 keys are more secure and performant than RSA keys.

OpenSSH 6.5 introduced ED25519 SSH keys in 2014, and they should be available on most
operating systems.

ED25519_SK SSH keys

Introduced in GitLab 14.8.

To use ED25519_SK SSH keys on GitLab, your local client and GitLab server
must have OpenSSH 8.2 or later installed.

ECDSA_SK SSH keys

Introduced in GitLab 14.8.

To use ECDSA_SK SSH keys on GitLab, your local client and GitLab server
must have OpenSSH 8.2 or later installed.

RSA SSH keys

Available documentation suggests ED25519 is more secure than RSA.

If you use an RSA key, the US National Institute of Science and Technology in
Publication 800-57 Part 3 (PDF)
recommends a key size of at least 2048 bits. The default key size depends on your version of ssh-keygen.
Review the man page for your installed ssh-keygen command for details.

See if you have an existing SSH key pair

Before you create a key pair, see if a key pair already exists.

  1. Go to your home directory.

  2. Go to the .ssh/ subdirectory. If the .ssh/ subdirectory doesn’t exist,
    you are either not in the home directory, or you haven’t used ssh before.
    In the latter case, you need to generate an SSH key pair.

  3. See if a file with one of the following formats exists:

    Algorithm Public key Private key
    ED25519 (preferred) id_ed25519.pub id_ed25519
    ED25519_SK id_ed25519_sk.pub id_ed25519_sk
    ECDSA_SK id_ecdsa_sk.pub id_ecdsa_sk
    RSA (at least 2048-bit key size) id_rsa.pub id_rsa
    DSA (deprecated) id_dsa.pub id_dsa
    ECDSA id_ecdsa.pub id_ecdsa

Generate an SSH key pair

If you do not have an existing SSH key pair, generate a new one:

  1. Open a terminal.

  2. Run ssh-keygen -t followed by the key type and an optional comment.
    This comment is included in the .pub file that’s created.
    You may want to use an email address for the comment.

    For example, for ED25519:

    ssh-keygen -t ed25519 -C "<comment>"

    For 2048-bit RSA:

    ssh-keygen -t rsa -b 2048 -C "<comment>"
  3. 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):
  4. 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.

  5. 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.

Configure SSH to point to a different directory

If you did not save your SSH key pair in the default directory,
configure your SSH client to point to the directory where the private key is stored.

  1. Open a terminal and run this command:

    eval $(ssh-agent -s)
    ssh-add <directory to private SSH key>
  2. Save these settings in the ~/.ssh/config file. For example:

    # GitLab.com
    Host gitlab.com
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/gitlab_com_rsa
    
    # Private GitLab instance
    Host gitlab.company.com
      PreferredAuthentications publickey
      IdentityFile ~/.ssh/example_com_rsa

For more information on these settings, see the man ssh_config page in the SSH configuration manual.

Public SSH keys must be unique to GitLab because they bind to your account.
Your SSH key is the only identifier you have when you push code with SSH.
It must uniquely map to a single user.

Update your SSH key passphrase

You can update the passphrase for your SSH key:

  1. Open a terminal and run this command:

    ssh-keygen -p -f /path/to/ssh_key
  2. At the prompts, enter the passphrase and then press Enter.

Upgrade your RSA key pair to a more secure format

If your version of OpenSSH is between 6.5 and 7.8, you can save your private
RSA SSH keys in a more secure OpenSSH format by opening a terminal and running
this command:

ssh-keygen -o -f ~/.ssh/id_rsa

Alternatively, you can generate a new RSA key with the more secure encryption format with
the following command:

ssh-keygen -o -t rsa -b 4096 -C "<comment>"

Generate an SSH key pair for a FIDO/U2F hardware security key

To generate ED25519_SK or ECDSA_SK SSH keys, you must use OpenSSH 8.2 or later:

  1. Insert a hardware security key into your computer.

  2. Open a terminal.

  3. Run ssh-keygen -t followed by the key type and an optional comment.
    This comment is included in the .pub file that’s created.
    You may want to use an email address for the comment.

    For example, for ED25519_SK:

    ssh-keygen -t ed25519-sk -C "<comment>"

    For ECDSA_SK:

    ssh-keygen -t ecdsa-sk -C "<comment>"

    If your security key supports FIDO2 resident keys, you can enable this when
    creating your SSH key:

    ssh-keygen -t ed25519-sk -O resident -C "<comment>"

    -O resident indicates that the key should be stored on the FIDO authenticator itself.
    Resident key is easier to import to a new computer because it can be loaded directly
    from the security key by ssh-add -K
    or ssh-keygen -K.

  4. Press Enter. Output similar to the following is displayed:

    Generating public/private ed25519-sk key pair.
    You may need to touch your authenticator to authorize key generation.
  5. Touch the button on the hardware security key.

  6. Accept the suggested filename and directory:

    Enter file in which to save the key (/home/user/.ssh/id_ed25519_sk):
  7. 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.

Add an SSH key to your GitLab account

To use SSH with GitLab, copy your public key to your GitLab account:

  1. Copy the contents of your public key file. You can do this manually or use a script.
    For example, to copy an ED25519 key to the clipboard:

    macOS

    tr -d 'n' < ~/.ssh/id_ed25519.pub | pbcopy

    Linux (requires the xclip package)

    xclip -sel clip < ~/.ssh/id_ed25519.pub

    Git Bash on Windows

    cat ~/.ssh/id_ed25519.pub | clip

    Replace id_ed25519.pub with your filename. For example, use id_rsa.pub for RSA.

  2. Sign in to GitLab.

  3. On the top bar, in the top right corner, select your avatar.

  4. Select Preferences.

  5. On the left sidebar, select SSH Keys.

  6. 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, or sk-ssh-ed25519@openssh.com, and may end with a comment.

  7. In the Title box, type a description, like Work Laptop or
    Home Workstation.

  8. Optional. In the Expires at box, select an expiration date. (Introduced in GitLab 12.9.)
    In:

    • GitLab 13.12 and earlier, the expiration date is informational only. It doesn’t prevent
      you from using the key. Administrators can view expiration dates and use them for
      guidance when deleting keys.
    • GitLab checks all SSH keys at 02:00 AM UTC every day. It emails an expiration notice for all SSH keys that expire on the current date. (Introduced in GitLab 13.11.)
    • GitLab checks all SSH keys at 01:00 AM UTC every day. It emails an expiration notice for all SSH keys that are scheduled to expire seven days from now. (Introduced in GitLab 13.11.)
  9. Select Add key.

Verify that you can connect

Verify that your SSH key was added correctly.

The following commands use the example hostname gitlab.example.com. Replace this example hostname with your GitLab instance’s hostname, for example, git@gitlab.com.

  1. To ensure you’re connecting to the correct server, check the server’s SSH host keys fingerprint. For:

    • GitLab.com, see the SSH host keys fingerprints documentation.
    • GitLab.com or another GitLab instance, see gitlab.example.com/help/instance_configuration#ssh-host-keys-fingerprints where gitlab.example.com is gitlab.com (for
      GitLab.com) or the address of the GitLab instance.
  2. Open a terminal and run this command, replacing gitlab.example.com with your
    GitLab instance URL:

    ssh -T git@gitlab.example.com
  3. If this is the first time you connect, you should verify the
    authenticity of the GitLab host. 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)? yes
    Warning: Permanently added 'gitlab.example.com' (ECDSA) to the list of known hosts.

    Type yes and press Enter.

  4. Run the ssh -T git@gitlab.example.com command again. You should receive a Welcome to GitLab, @username! message.

If the welcome message doesn’t appear, you can troubleshoot by running ssh
in verbose mode:

ssh -Tvvv git@gitlab.example.com

Use different keys for different repositories

You can use a different key for each repository.

Open a terminal and run this command:

git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/private-key-filename-for-this-repository -F /dev/null"

This command does not use the SSH Agent and requires Git 2.10 or later. For more information
on ssh command options, see the man pages for both ssh and ssh_config.

Use different accounts on a single GitLab instance

You can use multiple accounts to connect to a single instance of GitLab. You
can do this by using the command in the previous topic.
However, even if you set IdentitiesOnly to yes, you cannot sign in if an
IdentityFile exists outside of a Host block.

Instead, you can assign aliases to hosts in the ~/.ssh/config file.

  • For the Host, use an alias like user_1.gitlab.com and
    user_2.gitlab.com. Advanced configurations
    are more difficult to maintain, and these strings are easier to
    understand when you use tools like git remote.
  • For the IdentityFile, use the path the private key.
# User1 Account Identity
Host <user_1.gitlab.com>
  Hostname gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/<example_ssh_key1>

# User2 Account Identity
Host <user_2.gitlab.com>
  Hostname gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/<example_ssh_key2>

Now, to clone a repository for user_1, use user_1.gitlab.com in the git clone command:

git clone git@<user_1.gitlab.com>:gitlab-org/gitlab.git

To update a previously-cloned repository that is aliased as origin:

git remote set-url origin git@<user_1.gitlab.com>:gitlab-org/gitlab.git

NOTE:
Private and public keys contain sensitive data. Ensure the permissions
on the files make them readable to you but not accessible to others.

Configure two-factor authentication (2FA)

You can set up two-factor authentication (2FA) for
Git over SSH. We recommend using
ED25519_SK or ECDSA_SK SSH keys.

Use EGit on Eclipse

If you are using EGit, you can add your SSH key to Eclipse.

Use SSH on Microsoft Windows

If you’re running Windows 10, you can either use the Windows Subsystem for Linux (WSL)
with WSL 2 which
has both git and ssh preinstalled, or install Git for Windows to
use SSH through PowerShell.

The SSH key generated in WSL is not directly available for Git for Windows, and vice versa,
as both have a different home directory:

  • WSL: /home/<user>
  • Git for Windows: C:Users<user>

You can either copy over the .ssh/ directory to use the same key, or generate a key in each environment.

Alternative tools include:

  • Cygwin
  • PuttyGen

Overriding SSH settings on the GitLab server

GitLab integrates with the system-installed SSH daemon and designates a user
(typically named git) through which all access requests are handled. Users
who connect to the GitLab server over SSH are identified by their SSH key instead
of their username.

SSH client operations performed on the GitLab server are executed as this
user. You can modify this SSH configuration. For example, you can specify
a private SSH key for this user to use for authentication requests. However, this practice
is not supported and is strongly discouraged as it presents significant
security risks.

GitLab checks for this condition, and directs you
to this section if your server is configured this way. For example:

$ gitlab-rake gitlab:check

Git user has default SSH configuration? ... no
  Try fixing it:
  mkdir ~/gitlab-check-backup-1504540051
  sudo mv /var/lib/git/.ssh/id_rsa ~/gitlab-check-backup-1504540051
  sudo mv /var/lib/git/.ssh/id_rsa.pub ~/gitlab-check-backup-1504540051
  For more information see:
  doc/user/ssh.md#overriding-ssh-settings-on-the-gitlab-server
  Please fix the error above and rerun the checks.

Remove the custom configuration as soon as you can. These customizations
are explicitly not supported and may stop working at any time.

Troubleshooting

Password prompt with git clone

When you run git clone, you may be prompted for a password, like git@gitlab.example.com's password:.
This indicates that something is wrong with your SSH setup.

  • Ensure that you generated your SSH key pair correctly and added the public SSH
    key to your GitLab profile.
  • Try to manually register your private SSH key by using ssh-agent.
  • Try to debug the connection by running ssh -Tv git@example.com.
    Replace example.com with your GitLab URL.

Could not resolve hostname error

You may receive the following error when verifying that you can connect:

ssh: Could not resolve hostname gitlab.example.com: nodename nor servname provided, or not known

If you receive this error, restart your terminal and try the command again.

Key enrollment failed: invalid format error

You may receive the following error when generating an SSH key pair for a FIDO/U2F hardware security key:

Key enrollment failed: invalid format

You can troubleshoot this by trying the following:

  • Run the ssh-keygen command using sudo.
  • Verify your IDO/U2F hardware security key supports
    the key type provided.
  • Verify the version of OpenSSH is 8.2 or greater by
    running ssh -v.

GitLab and SSH keys

Git is a distributed version control system, which means you can work locally
but you can also share or «push» your changes to other servers.
Before you can push your changes to a GitLab server
you need a secure communication channel for sharing information.

The SSH protocol provides this security and allows you to authenticate to the
GitLab remote server without supplying your username or password each time.

For a more detailed explanation of how the SSH protocol works, read
this nice tutorial by DigitalOcean.

Requirements

The only requirement is to have the OpenSSH client installed on your system. This
comes pre-installed on GNU/Linux and macOS, but not on Windows.

Depending on your Windows version, there are different methods to work with
SSH keys.

Installing the SSH client for Windows 10

Starting with Windows 10, you can
install the Windows Subsystem for Linux (WSL)
where you can run Linux distributions directly on Windows, without the overhead
of a virtual machine. Once installed and set up, you’ll have the Git and SSH
clients at your disposal.

Installing the SSH client for Windows 8.1 and Windows 7

The easiest way to install Git and the SSH client on Windows 8.1 and Windows 7
is Git for Windows. It provides a BASH
emulation (Git Bash) used for running Git from the command line and the
ssh-keygen command that is useful to create SSH keys as you’ll learn below.

NOTE: Alternative tools:
Although not explored in this page, you can use some alternative tools.
Cygwin is a large collection of GNU and open source
tools which provide functionality similar to a Unix distribution.
PuttyGen
provides a graphical user interface to create SSH keys.

Types of SSH keys and which to choose

GitLab supports RSA, DSA, ECDSA, and ED25519 keys. Their difference lies on
the signing algorithm, and some of them have advantages over the others. For
more information, you can read this
nice article on ArchWiki.
We’ll focus on ED25519 and RSA and here.

NOTE: Note:
As an admin, you can restrict
which keys should be permitted and their minimum length.
By default, all keys are permitted, which is also the case for
GitLab.com.

ED25519 SSH keys

Following best practices,
you should always favor ED25519 SSH keys, since they
are more secure and have better performance over the other types.

They were introduced in OpenSSH 6.5, so any modern OS should include the
option to create them. If for any reason your OS or the GitLab instance you
interact with doesn’t support this, you can fallback to RSA.

RSA SSH keys

RSA keys are the most common ones and therefore the most compatible with
servers that may have an old OpenSSH version. Use them if the GitLab server
doesn’t work with ED25519 keys.

The minimum key size is 1024 bits, defaulting to 2048. If you wish to generate a
stronger RSA key pair, specify the -b flag with a higher bit value than the
default.

The old, default password encoding for SSH private keys is
insecure;
it’s only a single round of an MD5 hash. Since OpenSSH version 6.5, you should
use the -o option to ssh-keygen to encode your private key in a new, more
secure format.

If you already have an RSA SSH key pair to use with GitLab, consider upgrading it
to use the more secure password encryption format by using the following command
on the private key:

ssh-keygen -o -f ~/.ssh/id_rsa

Generating a new SSH key pair

Before creating an SSH key pair, make sure to read about the
different types of keys to understand
their differences.

To create a new SSH key pair:

  1. Open a terminal on Linux or macOS, or Git Bash / WSL on Windows.

  2. Generate a new ED25519 SSH key pair:

    ssh-keygen -t ed25519 -C "email@example.com"

    Or, if you want to use RSA:

    ssh-keygen -o -t rsa -b 4096 -C "email@example.com"

    The -C flag adds a comment in the key in case you have multiple of them
    and want to tell which is which. It is optional.

  3. Next, you will be prompted to input a file path to save your SSH key pair to.
    If you don’t already have an SSH key pair, use the suggested path by pressing
    Enter. Using the suggested path will normally allow your SSH client
    to automatically use the SSH key pair with no additional configuration.

    If you already have an SSH key pair with the suggested file path, you will need
    to input a new file path and declare what host
    this SSH key pair will be used for in your ~/.ssh/config file.

  4. Once the path is decided, you will be prompted to input a password to
    secure your new SSH key pair. It’s a best practice to use a password,
    but it’s not required and you can skip creating it by pressing
    Enter twice.

    If, in any case, you want to add or change the password of your SSH key pair,
    you can use the -pflag:

    ssh-keygen -p -o -f <keyname>

Now, it’s time to add the newly created public key to your GitLab account.

Adding an SSH key to your GitLab account

  1. Copy your public SSH key to the clipboard by using one of the commands below
    depending on your Operating System:

    macOS:

    pbcopy < ~/.ssh/id_ed25519.pub

    WSL / GNU/Linux (requires the xclip package):

    xclip -sel clip < ~/.ssh/id_ed25519.pub

    Git Bash on Windows:

    cat ~/.ssh/id_ed25519.pub | clip

    You can also open the key in a graphical editor and copy it from there,
    but be careful not to accidentally change anything.

    NOTE: Note:
    If you opted to create an RSA key, the name might differ.

  2. Add your public SSH key to your GitLab account by clicking your avatar
    in the upper right corner and selecting Settings. From there on,
    navigate to SSH Keys and paste your public key in the «Key» section.
    If you created the key with a comment, this will appear under «Title».
    If not, give your key an identifiable title like Work Laptop or
    Home Workstation, and click Add key.

    NOTE: Note:
    If you manually copied your public SSH key make sure you copied the entire
    key starting with ssh-ed25519 (or ssh-rsa) and ending with your email.

Testing that everything is set up correctly

To test whether your SSH key was added correctly, run the following command in
your terminal (replacing gitlab.com with your GitLab’s instance domain):

ssh -T git@gitlab.com

The first time you connect to GitLab via SSH, you will be asked to verify the
authenticity of the GitLab host you are connecting to.
For example, when connecting to GitLab.com, answer yes to add GitLab.com to
the list of trusted hosts:

The authenticity of host 'gitlab.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)? yes
Warning: Permanently added 'gitlab.com' (ECDSA) to the list of known hosts.

NOTE: Note:
For GitLab.com, consult the
SSH host keys fingerprints,
to make sure you’re connecting to the correct server.

Once added to the list of known hosts, you won’t be asked to validate the
authenticity of GitLab’s host again. Run the above command once more, and
you should only receive a Welcome to GitLab, @username! message.

If the welcome message doesn’t appear, run SSH’s verbose mode by replacing -T
with -vvvT to understand where the error is.

Working with non-default SSH key pair paths

If you used a non-default file path for your GitLab SSH key pair,
you must configure your SSH client to find your GitLab private SSH key
for connections to GitLab.

Open a terminal and use the following commands
(replacing other_id_rsa with your private SSH key):

eval $(ssh-agent -s)
ssh-add ~/.ssh/other_id_rsa

To retain these settings, you’ll need to save them to a configuration file.
For OpenSSH clients this is configured in the ~/.ssh/config file. In this
file you can set up configurations for multiple hosts, like GitLab.com, your
own GitLab instance, GitHub, Bitbucket, etc.

Below are two example host configurations using their own SSH key:

# GitLab.com
Host gitlab.com
  Preferredauthentications publickey
  IdentityFile ~/.ssh/gitlab_com_rsa

# Private GitLab instance
Host gitlab.company.com
  Preferredauthentications publickey
  IdentityFile ~/.ssh/example_com_rsa

Public SSH keys need to be unique to GitLab, as they will bind to your account.
Your SSH key is the only identifier you’ll have when pushing code via SSH,
that’s why it needs to uniquely map to a single user.

Per-repository SSH keys

If you want to use different keys depending on the repository you are working
on, you can issue the following command while inside your repository:

git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/private-key-filename-for-this-repository -F /dev/null"

This will not use the SSH Agent and requires at least Git 2.10.

Deploy keys

Per-repository deploy keys

Deploy keys allow read-only or read-write (if enabled) access to one or
multiple projects with a single SSH key pair.

This is really useful for cloning repositories to your Continuous
Integration (CI) server. By using deploy keys, you don’t have to set up a
dummy user account.

If you are a project maintainer or owner, you can add a deploy key in the
project settings under the section ‘Repository’. Specify a title for the new
deploy key and paste a public SSH key. After this, the machine that uses
the corresponding private SSH key has read-only or read-write (if enabled)
access to the project.

You can’t add the same deploy key twice using the form.
If you want to add the same key to another project, please enable it in the
list that says ‘Deploy keys from projects available to you’. All the deploy
keys of all the projects you have access to are available. This project
access can happen through being a direct member of the project, or through
a group.

Deploy keys can be shared between projects, you just need to add them to each
project.

Global shared deploy keys

Global Shared Deploy keys allow read-only or read-write (if enabled) access to
be configured on any repository in the entire GitLab installation.

This is really useful for integrating repositories to secured, shared Continuous
Integration (CI) services or other shared services.
GitLab administrators can set up the Global Shared Deploy key in GitLab and
add the private key to any shared systems. Individual repositories opt into
exposing their repository using these keys when a project maintainers (or higher)
authorizes a Global Shared Deploy key to be used with their project.

Global Shared Keys can provide greater security compared to Per-Project Deploy
Keys since an administrator of the target integrated system is the only one
who needs to know and configure the private key.

GitLab administrators set up Global Deploy keys in the Admin area under the
section Deploy Keys. Ensure keys have a meaningful title as that will be
the primary way for project maintainers and owners to identify the correct Global
Deploy key to add. For instance, if the key gives access to a SaaS CI instance,
use the name of that service in the key name if that is all it is used for.
When creating Global Shared Deploy keys, give some thought to the granularity
of keys — they could be of very narrow usage such as just a specific service or
of broader usage for something like «Anywhere you need to give read access to
your repository».

Once a GitLab administrator adds the Global Deployment key, project maintainers
and owners can add it in project’s Settings > Repository section by expanding the
Deploy Key section and clicking Enable next to the appropriate key listed
under Public deploy keys available to any project.

NOTE: Note:
The heading Public deploy keys available to any project only appears
if there is at least one Global Deploy Key configured.

CAUTION: Warning:
Defining Global Deploy Keys does not expose any given repository via
the key until that repository adds the Global Deploy Key to their project.
In this way the Global Deploy Keys enable access by other systems, but do
not implicitly give any access just by setting them up.

Applications

Eclipse

How to add your SSH key to Eclipse: https://wiki.eclipse.org/EGit/User_Guide#Eclipse_SSH_Configuration

SSH on the GitLab server

GitLab integrates with the system-installed SSH daemon, designating a user
(typically named git) through which all access requests are handled. Users
connecting to the GitLab server over SSH are identified by their SSH key instead
of their username.

SSH client operations performed on the GitLab server wil be executed as this
user. Although it is possible to modify the SSH configuration for this user to,
e.g., provide a private SSH key to authenticate these requests by, this practice
is not supported and is strongly discouraged as it presents significant
security risks.

The GitLab check process includes a check for this condition, and will direct you
to this section if your server is configured like this, e.g.:

$ gitlab-rake gitlab:check
# ...
Git user has default SSH configuration? ... no
  Try fixing it:
  mkdir ~/gitlab-check-backup-1504540051
  sudo mv /var/lib/git/.ssh/id_rsa ~/gitlab-check-backup-1504540051
  sudo mv /var/lib/git/.ssh/id_rsa.pub ~/gitlab-check-backup-1504540051
  For more information see:
  doc/ssh/README.md in section "SSH on the GitLab server"
  Please fix the error above and rerun the checks.

Remove the custom configuration as soon as you’re able to. These customizations
are explicitly not supported and may stop working at any time.

Troubleshooting

If on Git clone you are prompted for a password like git@gitlab.com's password:
something is wrong with your SSH setup.

  • Ensure that you generated your SSH key pair correctly and added the public SSH
    key to your GitLab profile
  • Try manually registering your private SSH key using ssh-agent as documented
    earlier in this document
  • Try to debug the connection by running ssh -Tv git@example.com
    (replacing example.com with your GitLab domain)

Learn how to create a SSH key to work with Github and Gitlab using PuTTYGen in Windows 10.

When you work with Git either with Github or Gitlab, you will need often to authenticate yourself using your username and password. The SSH Keys are an alternative to identify yourself without providing the mentioned credentials every time you interact with your repositories. You will only need to share your public key with the service and the private key will remain stored in your computer, so when the keys match, you have full access to your stuff.

For example, if you try to clone one of your repositories through SSH like this:

git clone [email protected]:username-or-organization/repository.git

In Windows, if you don’t have any SSH key configured properly, the following message will appear:

Cloning into 'your-git-repo'...
[email protected]: Permission denied (publickey,keyboard-interactive).
fatal: Could not read from remote repository.      

Please make sure you have the correct access rights
and the repository exists.

In this article, I will explain to you how to easily generate your own SSH key to identify your device.

1. Install PuTTYGen

The first thing you gotta do to work with the SSH authentication with your repositories is to install PuTTYGen in the system. This application comes included in the installation setup of PuTTY on the official website here. However, you can download the puttygen.exe file as well if you don’t want the entire PuTTy bundle.

PuTTYGen is an RSA and DSA key generation utility that will be used to create our SSH key in the Windows environment.

2. Generate your SSH Key

Launch the PuttyGen binary and start creating the SSH key by clicking on «Generate», after that, you will be asked to move the mouse over the blank space to generate some randomness in your key:

PuTTY Key Generator, Generate SSH in Windows

After moving the mouse at this point, the SSH key will be generated and you will have to fill the following fields:

  • Key comment: the name to use for this key, usually the name of the computer that you are working on right now.
  • Key passphrase: this will be the password that you will need to provide to unlock the key every time you need it.

SSH Key fill password and comment

Now that we have the password of the SSH key and a name for it, you can now proceed to save in the required formats for Windows.

3. Create the .ssh folder in your user directory

Create the .ssh folder in the current user’s directory. For example, on my computer, it is located at C:/Users/sdkca. So I created the .ssh folder in there (you can obtain the directory with the environment variable %USERPROFILE%):

Windows .SSH directory location

You will store all the created files in the mentioned directory.

4. Store PuTTY Private Key (PPK File)

The first file to generate from our now generated SSH key is the file with .ppk extension that you can generate by clicking on «Save private key»:

Puttygen ppk file

You can provide a custom name, in our case we will use the same name as the comment «ryzen93950x.ppk» and will be stored in the .ssh directory created in step #3. The content of the .ppk file looks something like this:

PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: Ryzen 9 3950x
Public-Lines: 6
AAAAB3NzaC1yc2EAAAABJQAAAQEAhDo/PTt7Ag2q5sAeE3zKfeGGedMVKK9q8BsQ
aYCES7HiE3wzTEp+jPAXDZuQmbhozZQM2whsQuqPmcqg+vkTS86eFkTVjjEeSyB0
F1pWGq4jg/mvbZ0jSjlM5mjbY9cwPpog6HleaCDovNkcapdPm64+FN0F9it5hwY1
sdzdSsqfxqzt8JlcwB8zKw71/ThE9fynQNar+utL2LrPPWS2bR4ZlI1WlQSWAkd2
9z3vk78zxRyuL5wdQf9j7Ay42j7bwkTcjCucC0T0iDeK+KoKyMysvbQFp4SLYXQq
xsPzU/BkEZmnqLxNzp6lShT6W1PcfNeVIH1DY1BsqtghaddHlQ==
Private-Lines: 14
ksW4kvFIMD4OqwgDYbEkhiB0l1uNsfj2uXw/lch/mHmaUSS08+2RHojc4GNDD2VG
....
.....

5. Store OpenSSH Public Key for authorized keys (id_rsa.pub File)

Now you need to create the file id_rsa.pub that will contain the string in the middle of the PuTTYGen application (Public key). The string is used on the authorized_keys file of the servers so you can access them:

PuTTY OpenSSH Public Key

Copy the content and paste it into the mentioned id_rsa.pub file in the .ssh directory created in step #3. The file content will look something like this:

ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAhDo/PTt7Ag2q5sAeE3zKfeGGedMVKK9q8BsQaYCES7HiE3wzTEp+jPAXDZuQmbhozZQM2whsQuqPmcqg+vkTS86eFkTVjjEeSyB0F1pWGq4jg/mvbZ0jSjlM5mjbY9cwPpog6HleaCDovTROLOLOLOLO-DONTUSETHISKEY-USEYOUROWNKEYJlcwB8zKw71/ThE9fynQNar+utL2LrPPWS2bR4ZlI1WlQSWAkd29z3vk78zxRyuL5wdQf9j7Ay42j7bwkTcjCucC0T0iDeK+KoKyMysvbQFp4SLYXQqxsPzU/BkEZmnqLxNzp6lShT6W1PcfNeVIH1DY1BsqtghaddHlQ== Ryzen 9 3950x

6. Store OpenSSH Key (id_rsa File)

Finally, the third file will be your identity file that will be your SSH Key in Open SSH format. You will need to convert your key to this format as it’s the one you require on tools like Github and Gitlab. You can easily do this by clicking on the Conversions tab of PuTTYGen and then clicking on Export OpenSSH Key (force new file format):

Puttygen Export OpenSSH Key

Note: if you don’t use the new file format, it will work anyway, however, you will always find the warning «load pubkey «/c/Users/USERNAME/.ssh/id_rsa»: invalid format».

This will prompt you where to save the file, use id_rsa as name and store it in the .ssh directory created in step #3. The content of this file should look like this:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,920A7C839CE3CABE

qWuTR1ARsILMzEGwhmr5SYFdu/MxsLKA3XTLy1dO4RYsvLqZOmloRvtZgy9VSNpg
...............................
-----END RSA PRIVATE KEY-----

After following all the previous steps you will end up with 3 files in the .ssh directory:

.SSH Key directory Windows

7. Register SSH key in Github and Gitlab

As the final step, you need to register your OpenSSH Public Key in the platform of your choice in your SSH Keys:

Gitlab

Login to Gitlab and open your user settings. In the left side menu, choose the SSH Keys option and register the value of your id_rsa.pub file:

Gitlab SSH Keys

Github

Login to Github and open your user personal settings through the top right side dropdown. Select SSH AND GPG keys and register your key:

Github SSH Keys

8. Using SSH Authentication

Now that you have your keys registered, you can clone your repositories and make changes to your repositories:

git clone [email protected]:username/repository-name.git

Happy coding ❤️!

The Secure Socket Shell (SSH) is the preferred connection mechanism, especially when compared to HTTPS, to connect from Git to a remote server such as GitLab, GitHub or BitBucket.

In this Git tutorial, we show you how to configure SSH keys to securely connect your local Git instance with your remote repositories.

GitLab SSH key configuration steps

The steps to follow to configure GitLab SSH keys for secure Git connections are as follows:

  1. Create an SSH key pair on your personal computer
  2. Copy the value of the public SSH key
  3. Log into GitLab and navigate to your account’s Preferences tab
  4. Create a new GitLab SSH key
  5. Paste the public key in as the value and set an expiration date
  6. Copy the SSH URL of the GitLab repo you wish to clone
  7. Issue a git clone command with the SSH URL

Create GitLab SSH Keys with ssh-keygen

Both Ubuntu and Windows machines support SSH out of the box.

Updated versions of Windows 10 and Windows Server have built-in support since 2018, so there is no need to download Putty, PuttyGen or Plink to create RAS keys or to SSH in to GitLab. Just open PowerShell in Windows or a Linux terminal window on Ubuntu and issue the following command:

C:gitlabsshexample> ssh-keygen -o -t rsa -C "[email protected]"

The options provided to the ssh-keygen command force the tool to create an RSA compatible key using the latest OpenSSL standards. The -C switch simply adds a comment to the end of the public file.

Git and SSH tutorials

Need to setup SSH for GitHub, GitLab, or CodeDeploy? These SSH Key tutorials will help:

  • Find out how to build SSH keypairs for GitHub
  • Administer Git SSH Keys on Windows desktops
  • Run your first GitHub Clone over SSH
  • Network securely with BitBucket SSH Keys
  • Learn about the GitHub SSH KeyGen utility
  • Fix annoying GitHub Permission Denied Errors

These quick tutorials will get you working with Git and the secure shell in no time.

Accept all of the defaults when prompted for a passphrase or a special storage location. By default, all GitLab SSH keys must go in an .ssh folder under the user’s home directory. Furthermore, a blank passphrase is acceptable unless your organization’s compliance rules state otherwise.

This operation will create Git SSH keys named id_ras.pub and id_rsa respectively.

Copy the public SSH key

You must configure the value of the public key in GitLab. Open the public key in a text editor and copy the value to the clipboard:

GitLub SSH Key Git

Copy the .pub key produced from the GitLab SSH key generation operation and configure it into GitLab.

Configure GitLab SSH keys

Log into GitLab and click on your account preferences.

Click the SSH Keys link and paste the copied value into the text field.

Set an expiration date, and then click the blue button to persistently add the GitLab SSH key.

GitLab SSH Key Config

Configure GitLab SSH keys under your account preferences.

Clone GitLab with SSH

With the SSH key configured in GitLab, you can now perform regular Git operations such as clone, push, pull and fetch over SSH. Simply clone your repository of choice with the SSH URL, which is found on the landing page of your GitLab project. My GitLab SSH URL is structured as follows:

[email protected]:cameronmcnz/five-commits.git

Now that you’ve configured GitLab SSH and copied the SSH URL, issue a clone command in PowerShell. The GitLab SSH clone operation pauses to ask if you can confirm the authenticity of the keys being used. Type in yes and allow the operation to continue.

PS C:gitlabsshexample> git clone [email protected]:cameronmcnz/five-commits.git
Cloning into ‘five-commits’ from GitLab over SSH…
Authenticity of GitLab SSH Key can’t be established.
Do you still want to connect to GitLab over SSH (yes or no)? yes
Clone from GitLab over SSH: 100% (222/222), 2.23 MiB | 3.41 MiB/s, done.
Resolving deltas from GitLab over SSH: 100% (111/111), done.

Pull and push to GitLab over SSH

Once the clone operation is successful, continue to use Git as you normally would. All future pull and push operations to GitLab will happen over a secure SSH connection.

And that’s how easy it is to configure GitLab SSH keys to ensure all distributed Git operations happen over a secure channel.

Here is what my dashboard looks like:

enter image description here

Not really sure where to add an SSH key. Anyone have any idea?

peterh's user avatar

peterh

11.3k17 gold badges85 silver badges104 bronze badges

asked Mar 9, 2016 at 20:33

a53-416's user avatar

  1. You need to sign in. Green button top right.
  2. Click ‘profile settings’ on the left side menu.
  3. Click SSH Keys and follow the instructions on the page.

answered Mar 9, 2016 at 20:41

Oli's user avatar

OliOli

3472 silver badges6 bronze badges

Go to your GitLab account: https://gitlab.com/

Click on Settings on the top right drop-down, which will appear once you select the icon(white-fox image [specific to my profile]).
enter image description here

Click on Settings on the top right drop-down, which will appear once you select the icon(white-fox image).

Click on SSH Keys:
enter image description here

Add/Paste the SSH Key.
How to generate the ssh key: Download gitbash or putty:

After downloading gitbash/putty follow the steps:

  1. Open a terminal on Linux or macOS, or Git Bash / WSL on Windows.
  2. Generate a new ED25519 SSH key pair:

    ssh-keygen -t ed25519 -C "email@example.com"  
    

    Or, if you want to use RSA:

    ssh-keygen -t rsa -b 4096 -C "email@example.com"  
    

    It will generate the key in => C:Usersyourname.ssh directory.

  3. Copy the public key and paste in the gitlab location:

    enter image description here

Command to run on gitbash to clone the repository:

ssh-agent $(ssh-add C:Usersyouname.sshid_rsa; git clone git@gitlab.com:xyz/SpringBootStarter.git)

RobC's user avatar

RobC

21.8k20 gold badges66 silver badges75 bronze badges

answered Oct 25, 2019 at 15:39

prashant.kr.mod's user avatar

2

Just follow the official guides to Create and Add SSH keys.

answered Mar 10, 2016 at 19:06

Felipe Cabargas's user avatar

Goto https://gitlab.com/profile/keys.

If you are a new user a banner will show at the top of each project page.

You won’t be able to pull or push project code via SSH until you add
an SSH key to your profile

However, you can dismiss this warning.

answered Mar 10, 2016 at 9:58

Marco van Neerbos's user avatar

2

First, you need to do open terminal after that type

   mkdir -p ~/.ssh
   echo public_key_string >> ~/.ssh/authorized_keys
   chmod -R go= ~/.ssh
   chown -R shabeer:shabeer ~/.ssh
   ssh-keygen  or ssh-keygen -t ed25519 -C "mail@example.com"
   xclip -sel clip < ~/.ssh/id_ed25519.pub
   ~/.ssh/gitlab_rsa.pub

in this time can see your key and copy it, After that Go to the Gitlab settings and chose SSH Keys, you can see there have to option add that copied key

answered Jan 25, 2019 at 7:54

Shabeer K's user avatar

Shabeer KShabeer K

1,40116 silver badges23 bronze badges

No one wants to send their precious information over unencrypted channels. This is why most websites and web services use HTTPS to encrypt data by default.

When it comes to connecting to GitLab, many users and developers default to using HTTPS. Why? Because everyone knows how a username and password work. Albeit maybe not on a technical level but intuitively it makes sense.

ssh - I hereby present my git inspired freshly baked meme. - devRant

Things tend to get a little more complicated when sending Git commands to a server over HTTPS demands the user submit their username and password in order to perform each action. This can result in quite a lot of prompts. Moreover, when using CI/CD and automation tools, SSH keys are a much better choice for securely authenticating your GitLab account.

In this article, we’ll review what SSH keys are in the context of GitLab authentication, how to create them, and how to keep them safe.

What are SSH Keys?

In order to communicate over SSH (Secure Shell), you must have an SSH key pair. Each SSH key pair has a public and a private key.

Public Key – Can be used to encrypt data in such a way so only the holder of the corresponding private key can decrypt it.

Private Key – Can be used as proof of identity, and is used to authenticate a user’s connection to the server.

When using an SSH key pair as a method of authentication, the public key is stored on the server. It doesn’t really matter if it can be viewed as it is public information. The private key, however, is kept by the user and should not be copied or exposed in any way.

Learn SSH Keys in Minutes

Once this is set up, the server can ask the user for proof of identity using the public key and the user can prove it using the private key.

What are SSH keys in GitLab?

SSH keys are one of the choices for authentication against GitLab servers. The vast majority of actions you will be taking on GitLab via SSH will be pushing changes from your local git repository to the hosted repository on GitLab. 

Although there are other actions you can take, such as starting the GitLab CI/CD pipeline, once started you’re not going to need to interact with the GitLab runner often. Whenever you push code to the hosted repository, it will trigger the pipeline and it will be automated from there, and that action might be scheduled to run many times a day.

Types of SSH keys and options supported

It is generally recommended you use ED25519 SSH keys, which are more secure, and should be available on any system.

However, if you have a good reason to, there are also RSA SSH keys, which would work just as well on GitLab. Although it is recommended you use an SSH key of at least 2048 bits. Do note that by default, a 1024 bit key is generated, so make sure to not use the default.

Why should you use SSH keys in Gitlab?

Aside from the comfort provided by not having to submit your username and password for each action you take, SSH keys are generally much more secure than a username and password. Humans aren’t very good at remembering a large number of secure passwords so they tend to reuse passwords over multiple sites, resulting in many vulnerabilities. No system connected to the internet is truly secure, but with an SSH key you greatly reduce the chance of human error giving away your access privileges.

How to generate an SSH key pair for your GitLab account

The official documentation is fairly comprehensive, but for completeness sake, I will walk you through the process.

One thing you won’t find in the above page, since it shows up in another place in the documentation, is where to type in the commands.

I’m assuming you are using GitBash, as it is the terminal that is supplied with git. For MacOS, Windows PowerShell or Linux terminals instructions, please see the documentation.

How to generate an ED25519 SSH key

To generate ED25519 SSH key you will need to run the following terminal command:

ssh-keygen -t ed25519 -C "<comment>"

The -C and the comment in quotations is optional, but should be used if you’re going to generate more than one pair.

You should see the following response after typing the above command:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):

How to generate an RSA SSH key pair

If you wish to generate an RSA key pair, use the following command:

ssh-keygen -t rsa -b 2048 -C "email@example.com"

Note the -b 2048, which denotes the number of bits for the key. If you choose to use RSA, the  US National Institute of Science and Technology recommends 2048.

The output should look like this:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):

Where to save your SSH key pair

Unless you know you need to use a different one, use the default path. But whatever you choose, do not use a network path (more on security later). Your chosen terminal will also use the generated SSH key without further input.

After generating the pair, you will be prompted to optionally add a passphrase:

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

This is optional, but recommended if others may have access to your computer where the key is stored.

How to configure an SSH key in GitLab

The first thing you’ll need is your public ED25519  SSH key in text form, to get this, you can use this command it Git Bash:

cat ~/.ssh/id_ed25519.pub | clip

Or if you’re using RSA:

cat ~/.ssh/id_rsa.pub | clip

Which will copy the SSH key in text form to your clipboard.

While logged into your GitLab account on gitlab.com, follow these steps:

  1. Select your avatar and click on settings
  2. Click SSH Keys
  3. Paste the SSH key into the Key field
  4. Add a descriptive text in the title, something that will define you as a user or the computer it is used from
  5. Click Add Key

Keeping GitLab SSH keys safe

SSH keys are far less prone to human error, but they are not foolproof. If you’re going to use SSH keys you should have a general idea about the security risks and how to mitigate them. Why? Because not doing so may cost you. In 2019 GoDaddy, one of the largest domain registrars in the world, suffered a critical security breach due to mismanaged SSH keys.

There are many things you could do in order to make your SSH communications more secure, but most of them are done from the server side, not the client side. As an admin, you may want to set up expiration policies, so that new keys need to be generated, and old keys will no longer be valid. This greatly reduces the risk of the keys falling into the wrong hands.

As a user on GitLab though, the main thing you need to concern yourself with is that the private key generated and stored locally should never ever be copied or moved. If you feel your private key may have been compromised, remove the corresponding public key from the server. You might want to do this periodically regardless, to ensure your server’s safety.

If you ever find yourself on a different computer, simply generate a new SSH key pair and upload the public key to GitLab. If a computer is lost, simply delete the public key from your GitLab account and all is well.

Do not backup your SSH keys as the only thing that does is create a vulnerability. As a rule, creating new keys and revoking old ones is a lot less painful than dealing with the fallout of a leaked SSH key pair.

Понравилась статья? Поделить с друзьями:
  • Gnu radio на русском windows 10
  • Gigabyte uefi bios установка windows 10 с диска
  • Github desktop скачать для windows 10
  • Github com nummer destroy windows 10 spying releases
  • Gigabyte u8300 драйвер скачать для windows 10