How to Add SSH Key in GitLab

Minh Vu

By Minh Vu

Updated Dec 28, 2023

Figure: How to Add SSH Key in GitLab

Disclaimer: All content on this website is derived directly from my own expertise and experiences. No AI-generated text or automated content creation tools are used.

SSH (Secure Shell) keys are used to establish a secure connection from your computer to another server.

For example, we can configure SSH keys to communicate from your computer to GitLab server.

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

Contents

What is an SSH Key?

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

It includes a pair of keys, one is public and another 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.

So, when you try to push to or pull from GitLab, your computer will use the private key to authenticate with the public key on GitLab.

Create SSH Key on Your Computer

I assume you are using Linux or MacOS. For Windows users, please read the tutorial about how to create SSH keys on Windows.

Let's get started:

  1. Open your Terminal, then type:

    console
    ssh-keygen -t rsa -C "dminhvu"

    Remember to change dminhvu to the name you want to use. Press Enter.

  2. You will be prompted to enter the directory to save the key. I will change it to /home/dminhvu/.ssh/dminhvu.

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

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

You will get the result like this:

Create SSH Key on Your Computer
Figure: Create SSH Key on Your Computer

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:

    console
    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:

    console
    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 to your GitLab account.

  1. Log in 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 continuing, 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:

    console
    git clone git@dminhvu:dminhvu/test-ssh-key.git

    It will say Are you sure you want to continue connecting (yes/no)?, type in yes.

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

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

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

Conclusion

That's it. In this tutorial, we have learned how to create and add SSH keys in GitLab.

Leave a comment below if you have any questions.

Minh Vu

Minh Vu

Software Engineer

Hi guys 👋, I'm a developer specializing in Elastic Stack and Next.js. My blog shares practical tutorials and insights based on 3+ years of hands-on experience. Open to freelance opportunities — let's get in touch!

Comments

Be the first to comment!

Leave a Comment

Receive Latest Updates 📬

Get every new post, special offers, and more via email. No fee required.