How to set up an SSH key and use it for GitLab 
Tags: gitlab, ssh, keys, setup
Published: 2018-10-15 18:43:53

---
title: How to set up an SSH key and use it for GitLab 
published: true
description: 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/3ken82tiqhouwm725w2h.png
tags: gitlab, ssh, keys, setup
---

### Hello, everybody! ###

I got into another project about 3 months ago and almost everything was new to me (*GrapheneDB, NodeJS, React and Redux, ESLint rules, packages etc.*). The only thing I knew how to use back then was JavaScript.

Obviously, the project set up was new to me as well. I never used VS Code before, neither GitLab or Heroku, so I didn't know some of the basic rules at the beginning. I took notes but even with the notes under my nose, I couldn't figure out how am I supposed to connect to GitLab using my SSH key when my laptop died last week.

**I figured it out, in the end!**
....but I had to connect information from two or three articles.

I think that if I put it all together here, it will be easier for someone else that encounters the same issue or, hell, it would be easier for me too to remember these things.

This is how I managed to create a SSH key and connect to my GitLab account using that key:

1) Open Git Bash ([Download and Install Git Bash](http://www.techoism.com/how-to-install-git-bash-on-windows/); You can use any `*nix` based command prompt).


2) Type
<pre> cd ~/.ssh </pre>
This will take you to the root directory for Git (Likely C:\Users\[YOUR-USER-NAME]\.ssh\ on Windows).

3) In the `.ssh` folder you should find these two files: `id_rsa` and `id_rsa.pub`. These files tell the computer how to communicate with GitHub, BitBucket, or any other Git based service.

Those files were obviously missing from my computer so I had to create them.

<pre> ssh-keygen -t rsa -C "your_email@example.com" </pre>
<figcaption>Type this to create those two files</figcaption>

Once you entered that command, you will get a few more questions:
<pre>Enter file in which to save the key (/home/demo/.ssh/id_rsa):</pre>
<figcaption>You can press enter here, saving the file to the user home (in this case, my example user is called demo).</figcaption>

Then this will be shown:
<pre>Enter passphrase (empty for no passphrase):</pre>
<figcaption>The only downside, of course, to having a passphrase, is then having to type it in each time you use the key pair. I suggest you to use a passphrase though, due to security concerns.</figcaption>

In the end, the entire process looks like this:

<pre>Enter file in which to save the key (/home/demo/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/demo/.ssh/id_rsa.
Your public key has been saved in /home/demo/.ssh/id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a
The key's randomart image is:
+--[ RSA 2048]----+
|          .oo.   |
|         .  o.E  |
|        + .  o   |
|     . = = .     |
|      = S = .    |
|     o + = +     |
|      . o + o .  |
|           . o   |
|                 |
+-----------------+</pre>

The public key is now located in /home/demo/.ssh/id_rsa.pub. The private key (identification) is now located in /home/demo/.ssh/id_rsa.

4) Copying the public key to GitLab

Go and open the `id_rsa.pub` file (you can use any text editor you want).
**Copy the entire content of that file** and then open `https://gitlab.com/profile/keys`.

![How GitLab SSH Keys page looks](https://image.ibb.co/iD6OVf/Capture-For-BLog.png "GitLabSSH")

Paste the content you copied from `id_rsa.pub` in the Key input (**don't add extra spaces or characters**).

*Give your key a descriptive name* and then **Add the key**.

5) Try to `git clone` or `git push`.

I really hope this will help someone because this article is what I needed last week when I couldn't put things together and all seemed to be way harder than it actually is.