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.
Table of Contents
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:
- Open your Terminal, then type:
shell
Remember to changessh-keygen -t rsa -C "dminhvu"
dminhvu
to the name you want to use. And press Enter. - You will be prompted to enter the directory to save the key. I will change it to
/home/dminhvu/.ssh/dminhvu
.shellEnter file in which to save the key (/home/dminhvu/.ssh/id_rsa): /home/dminhvu/.ssh/dminhvu
- 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:

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
-
Go back to your Terminal, type in:
shellsudo nano ~/.ssh/config
-
Add the following lines to the file:
~/.ssh/configHost 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:

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.
- Go back to your Terminal, type in:
shell
Changecat ~/.ssh/dminhvu.pub
dminhvu
to the name you used in the previous step. - Copy the result, it should look like this:
Figure: SSH Public Key
Alright, let's add it into your GitLab account.
- Login to your GitLab account, choose the avatar icon on the top left corner, then choose Edit profile.
- Choose SSH Keys on the left sidebar. Then Add new key.
- 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.
- Go to your repository on GitLab. In this case, I will create my repository called
test-ssh-key
. - 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
.Figure: GitLab Repository - Before continue, change the
gitlab.com
part of the above link todminhvu
(the name you set in the SSH config). For me, it will look likegit@dminhvu:dminhvu/test-ssh-key.git
. - Go to your Terminal, type in:
shell
It will says Are you sure you want to continue connecting (yes/no)?, type in yes.git clone git@dminhvu:dminhvu/test-ssh-key.git
If you get the result like this, then you are good to go:

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.