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
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.
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:
Remember to change
dminhvuto 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
- Next, you will be asked to enter a passphrase. I choose to leave it empty so I just press Enter.
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.
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:shell
Add the following lines to the file:~/.ssh/config
Remember to change
dminhvuto 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:
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:
dminhvuto the name you used in the previous step.
- Copy the result, it should look like this:
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:
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
- On the right side, choose Clone. Then choose Clone with SSH, you will get the SSH link like
- Before continue, change the
gitlab.compart of the above link to
dminhvu(the name you set in the SSH config). For me, it will look like
- Go to your Terminal, type in:
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:
Now your repository is cloned to
test-ssh-key folder. So just
cd test-ssh-key to go to the folder.
Cool, that's all for this tutorial.
Comment below if you have any questions.