Home//

How to Add SSH Key in GitLab

How to Add SSH Key in GitLab

Minh Vu

By Minh Vu

Updated Nov 12, 2023

SSH keys are used to establish a secure connection from your computer to another server.

In this tutorial, we will learn how to create and add SSH key in GitLab.

What is SSH key?

SSH key is used for secured connection between your computer and another server, in this case, GitLab.

It uses a pair of keys, one is public and one is private.

  • The public key, the one you will add to GitLab later, is stored on the server (GitLab).
  • The private key is stored on your computer.

Create SSH key

I assume you are using Linux and MacOS. For Windows users, please visit how to create SSH key on Windows.

Let's get started:

  1. Open your Terminal, then type:
    shell
    ssh-keygen -t rsa -C "dminhvu"
    Remember to change dminhvu to the name you want to use. And press Enter.
  2. You will be prompted to enter the directory to save the key. I will change it to /home/dminhvu/.ssh/dminhvu.
    shell
    Enter file in which to save the key (/home/dminhvu/.ssh/id_rsa): /home/dminhvu/.ssh/dminhvu
  3. Next, you will be asked to enter a passphrase. I choose to leave it empty so I just press Enter.
    shell
    Enter passphrase (empty for no passphrase): Enter same passphrase again:

You will get the result like this:

Create SSH Key
Figure: Create SSH Key

Next, I will show you an advanced optional step, which nobody will tell you, but I highly recommend you to do it from my perspective working as a developer for over 3 years.

Add SSH key to SSH config file

This step is to add your SSH key to the .ssh/config file, which is a file to store all of your SSH keys when you have multiple SSH keys for different servers, e.g. GitHub, GitLab, Bitbucket, etc.

To learn more about setting up multiple SSH keys, check out another tutorial on How to Setup Multiple GitHub Accounts with SSH Keys

  1. Go back to your Terminal, type in:

    shell
    sudo nano ~/.ssh/config
  2. Add the following lines to the file:

    ~/.ssh/config
    Host dminhvu User git HostName gitlab.com IdentityFile ~/.ssh/dminhvu

    Remember to change dminhvu to the name you used in the previous step. Press Ctrl + X to exit, then press Y to save the file.

I currently have 3 SSH keys: 2 for GitHub and 1 for GitLab. So my .ssh/config file looks like this:

SSH Config for Multiple Keys
Figure: SSH Config for Multiple Keys

Add SSH key to GitLab

Cool, let's add the SSH key to GitLab.

Before going to GitLab, you need to get the public key first.

  1. Go back to your Terminal, type in:
    shell
    cat ~/.ssh/dminhvu.pub
    Change dminhvu to the name you used in the previous step.
  2. Copy the result, it should look like this:
    SSH Public Key
    Figure: SSH Public Key

Alright, let's add it into your GitLab account.

  1. Login to your GitLab account, choose the avatar icon on the top left corner, then choose Edit profile.
  2. Choose SSH Keys on the left sidebar. Then Add new key.
  3. Enter the public key you copied from the 2nd step. And choose Add key.

Watch the video below if you are still confused:

Pull GitLab repository using SSH

Alright, we have gone so far. Let's test it out.

  1. Go to your repository on GitLab. In this case, I will create my repository called test-ssh-key.
  2. On the right side, choose Clone. Then choose Clone with SSH, you will get the SSH link like git@gitlab.com:dminhvu/test-ssh-key.git.
    GitLab Repository
    Figure: GitLab Repository
  3. Before continue, change the gitlab.com part of the above link to dminhvu (the name you set in the SSH config). For me, it will look like git@dminhvu:dminhvu/test-ssh-key.git.
  4. Go to your Terminal, type in:
    shell
    git clone git@dminhvu:dminhvu/test-ssh-key.git
    It will says Are you sure you want to continue connecting (yes/no)?, type in yes.

If you get the result like this, then you are good to go:

GitLab - Clone with SSH
Figure: GitLab - Clone with SSH

Now your repository is cloned to test-ssh-key folder. So just cd test-ssh-key to go to the folder.

Conclusion

Cool, that's all for this tutorial.

Comment below if you have any questions.

You can search for other posts at home page.
Minh Vu

Minh Vu

Software Engineer

Hi guys, I'm the author of WiseCode Blog. I mainly work with the Elastic Stack and build AI & Python projects. I also love writing technical articles, hope you guys have good experience reading my blog!