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?
- Create SSH Key on Your Computer
- Add SSH Key to SSH Config File
- Add SSH Key to GitLab
- Pull GitLab Repository using SSH
- Conclusion
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:
-
Open your Terminal, then type:
Remember to change
dminhvu
to the name you want to use. Press Enter. -
You will be prompted to enter the directory to save the key. I will change it to
/home/dminhvu/.ssh/dminhvu
. -
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.
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:
-
Add the following lines to the file:
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:
Change
dminhvu
to the name you used in the previous step. -
Copy the result, it should look like this:
Alright, let's add it to your GitLab account.
- Log in 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
. -
Before continuing, 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:
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:
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.
Comments
Be the first to comment!