Add your SSH key to GitLab

Referenced documentation: https://docs.gitlab.com/ce/ssh/README.html

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.

Locating an existing SSH key pair

Before generating a new SSH key pair check if your system already has one at the default location by opening a shell, or Command Prompt on Windows, and running the following command:

Windows Command Prompt:

type %userprofile%\.ssh\id_rsa.pub

Git Bash on Windows / GNU/Linux / macOS / PowerShell:

cat ~/.ssh/id_rsa.pub

If you see a string starting with ssh-rsa you already have an SSH key pair and you can skip the generate portion of the next section and skip to the copy to clipboard step. If you don't see the string or would like to generate a SSH key pair with a custom name continue onto the next step.

Your public SSH key file may also be named as follows:

  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub

Generating a new SSH key pair

  1. To generate a new SSH key pair, use the following command:

    Git Bash on Windows / GNU/Linux / macOS:

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

    Windows:

    Alternatively on Windows you can download PuttyGen and follow this documentation article to generate a SSH key pair.

  2. 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 a 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.

  3. Once you have input a file path you will be prompted to input a password to secure your SSH key pair. It is a best practice to use a password for an SSH key pair, but it is not required and you can skip creating a password by pressing enter.

    Note

    If you want to change the password of your SSH key pair, you can use ssh-keygen -p .

  4. The next step is to copy the public SSH key as we will need it afterwards.

    To copy your public SSH key to the clipboard, use the appropriate code below:

    macOS:

    bash pbcopy < ~/.ssh/id_rsa.pub

    GNU/Linux (requires the xclip package):

    bash xclip -sel clip < ~/.ssh/id_rsa.pub

    Windows Command Line:

    bash type %userprofile%\.ssh\id_rsa.pub | clip

    Git Bash on Windows / Windows PowerShell:

    bash cat ~/.ssh/id_rsa.pub | clip

  5. The final step is to add your public SSH key to GitLab.

    Navigate to the 'SSH Keys' tab in your 'Profile Settings'.

    Screenshot

    Paste your key in the 'Key' section and give it a relevant 'Title'. Use an identifiable title like 'Work Laptop - Windows 7' or 'Home MacBook Pro 15'.

    If you manually copied your public SSH key make sure you copied the entire key starting with ssh-rsa and ending with your email.

  6. Optionally you can test your setup by running ssh -T git@example.com (replacing example.com with your GitLab domain) and verifying that you receive a Welcome to GitLab message.