---
title: "How to Generate and Use GitLab SSH Keys"
description: "Do you want to securely authenticate with Gitlab and get rid of pesky password prompts? Switch to using Gitlab SSH keys! Learn hands-on in this tutorial."
canonical: "https://adamtheautomator.com/gitlab-ssh/"
---

# How to Generate and Use GitLab SSH Keys

> Do you want to securely authenticate with Gitlab and get rid of pesky password prompts? Switch to using Gitlab SSH keys! Learn hands-on in this tutorial.

Source: https://adamtheautomator.com/gitlab-ssh/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Generate and Use GitLab SSH Keys](https://adamtheautomator.com/wp-content/uploads/2021/11/How-to-Generate-and-Use-GitLab-SSH-Keys.jpg)

# How to Generate and Use GitLab SSH Keys

[![](https://secure.gravatar.com/avatar/5f3686e92dbfdc7017ac4ab5cb9abb7de6bdce69362126caf723caffb405f14c?s=192&d=mm&r=g)Kelvin Kipkorir](https://adamtheautomator.com/author/kelvin-kipkorir/)23 November 20216 min. read

Categories: [DevOps](/category/devops/)

Tags:[Git](/tag/git/)[GitLab](/tag/gitlab/)[SSH](/tag/ssh/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Generating a Gitlab SSH Key Pair](#generating-a-gitlab-ssh-key-pair)
*   [Adding the Gitlab SSH Key to Your Profile](#adding-the-gitlab-ssh-key-to-your-profile)
*   [Using Your Gitlab SSH Key](#using-your-gitlab-ssh-key)
*   [Signing In](#signing-in)
*   [Publishing a New Project](#publishing-a-new-project)
*   [Conclusion](#conclusion)

Repeated username and password prompts are annoying and time-wasting. If you use password authentication with your Gitlab account, each action requires you to send over your credentials, whether as part of the command or through an interactive prompt.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

Especially when automating the [CI/CD pipelines](https://www.ibm.com/cloud/blog/ci-cd-pipeline) in GitLab, password authentication can be inefficient and a possible security risk. Why not end your suffering and switch to [Secure Shell (SSH)](https://www.techtarget.com/searchsecurity/definition/Secure-Shell) authentication using Gitlab SSH keys? Read on and learn how!

By the end of this tutorial, you will have generated an SSH key pair, used the key to authenticate and publish a new Gitlab project.

## Prerequisites

This tutorial is a hands-on demonstration. To follow along, be sure to prepare the following requirements.

*   Your [GitLab](https://about.gitlab.com/) account. If you don’t have one yet, [sign up for a free](https://about.gitlab.com/users/sign_up) account first.
*   A computer to use as your Gitlab SSH client. This tutorial will be using an Ubuntu 20.04 LTS computer but should work with Windows and macOS too.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   Update or Install [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git). The latest version as of this writing is 2.25.1.

[Visual Studio Code](https://code.visualstudio.com/) (VS Code). The example in this tutorial will be using VS Code 1.62.3.

Related:[A Beginner’s Guide to Visual Studio Code and Git](https://adamtheautomator.com/visual-studio-code-git/)

## Generating a Gitlab SSH Key Pair

SSH keys or a key pair is consists of [private and public keys](https://www.ssh.com/academy/ssh/public-key-authentication#key-pair---public-and-private). In a nutshell, the private key is what the client (your computer) presents to the server (Gitlab) for authentication, and the public key is what the server uses to decode your private key. And if both keys match, the authentication passes.

GitLab [supports](https://docs.gitlab.com/ee/ssh/#supported-ssh-key-types) two types of SSH key pairs, the **RSA** and **ED25519** SSH keys. But in this example, you’ll generate the ED25519 key pair as it is [considered more secure](https://ed25519.cr.yp.to/) than RSA keys. Follow the steps below to generate an SSH key pair.

1\. On your desktop, open a terminal session and run the command below. This command opens your home directory in VS Code.

```bash
code ~
```

2\. On the VS Code, click **Terminal —> New Terminal**.

![Opening a new terminal instance in VSCode](https://adamtheautomator.com/wp-content/uploads/2021/11/image-326.png)

Opening a new terminal instance in VSCode

3\. On the VS Code terminal, run the `ssh-keygen -t` below to generate the `ED25519` SSH key type. The comment tag `-C` is optional but recommended to make your keys more identifiable.

```bash
ssh-keygen -t ed25519 -C "Gitlab SSH Key"
```

4\. At the next prompt asking where to save the key, press Enter to accept the default filename. The filename should be similar to _/home/<username>/.ssh/id\_ed25519_, where <username> is your username.

![Accepting the default filename](https://adamtheautomator.com/wp-content/uploads/2021/11/image-327.png)

Accepting the default filename

5\. Next, leave the passphrase empty, and press Enter twice. As a result, you’ll be creating a passwordless SSH key pair.

![Leaving the passphrase empty](https://adamtheautomator.com/wp-content/uploads/2021/11/image-328.png)

Leaving the passphrase empty

You should see a similar result to the screenshot below showing the private key (id\_ed25519) and public key (id\_ed25519.pub) locations.

> _The private and public keys have the same base filename. But the public key has a .pub extension, while the private key does not have an extension._

![Generating the Gitlab SSH key pair](https://adamtheautomator.com/wp-content/uploads/2021/11/image-329.png)

Generating the Gitlab SSH key pair

## Adding the Gitlab SSH Key to Your Profile

Remember that the private key stays on your computer, while the public key should be on the Gitlab server. So after generating the SSH Keys, your next step is to upload the public key to your Gitlab account. To do so, proceed as follows.

1\. Open your public key file in VSCode. On the Explorer pane, expand the **.ssh** folder and click **id\_es25519.pub**. Next, select the public key and copy it to the clipboard.

![Copying the public key](https://adamtheautomator.com/wp-content/uploads/2021/11/image-330.png)

Copying the public key

2\. Now, open a web browser, navigate to _[](https://gitlab.com)https://gitlab.com_, and log in to your Gitlab account.

3\. After login, navigate to the **SSH Keys** profile settings at https://gitlab.com/-/profile/keys.

*   Paste the public key that you copied earlier into the **Key** field.
*   The **Title** field automatically populates with the comment portion of your public key. Leave the title value as-is or enter your preferred value.
*   Optionally, specify an expiration date on the **Expires at** field.
*   Lastly, click **Add key**.

![Adding the Gitlab SSH key to your profile](https://adamtheautomator.com/wp-content/uploads/2021/11/image-331.png)

Adding the Gitlab SSH key to your profile

## Using Your Gitlab SSH Key

So far, you’ve generated the SSH key and uploaded it to your Gitlab account. What’s left is to test whether your SSH key works by using it to connect and authenticate to Gitlab.

### Signing In

On the VS Code terminal, connect to your Gitlab account by running the command below.

```bash
ssh -T git@gitlab.com
```

Assuming you’re connecting for the first time, the command prompts you to verify the host’s authenticity and confirm the connection. Type `yes` at the prompt and press `Enter` to confirm.

Notice that you did not have to enter a username and password to log in? Instead, the ssh command automatically uses your SSH keys for authentication. After a successful login, you should see a welcome message saying, “_Welcome to Gitlab @username!_“.

![Logging in to Gitlab via SSH](https://adamtheautomator.com/wp-content/uploads/2021/11/image-332.png)

Logging in to Gitlab via SSH

### Publishing a New Project

You’ve confirmed that your Gitlab SSH key works and lets you authenticate successfully. But does it also work when you interact with Gitlab through Git? Why not test by publishing a new Gitlab repository?

1\. First, initialize the Git configuration and set it up with your Gitlab account’s username and email address. On the VS Code terminal, run the [`git config`](https://git-scm.com/docs/git-config) commands below to specify your Gitlab username and email address, respectively.

```javascript
git config --global user.name "your_username"
git config --global user.email "your_email_address@example.com"
```

The command you ran should update or create the _~/.gitconfig_ file with the information you provided.

![Initializing the Git configuration](https://adamtheautomator.com/wp-content/uploads/2021/11/image-333.png)

Initializing the Git configuration

2\. Next, create a folder under your home directory for your new repository. Name the new folder as _my-first-project_.

> _The folder name will also become your Gitlab repository project name_.

```bash
# Create a new folder
mkdir ~/my-first-project
# Change the working directory to the new folder
cd ~/my-first-project
```

3\. Initialize the repository by running the command below. Make sure to change the `<username>` with your Gitlab username.

```bash
git init
```

You should see a similar confirmation message to the screenshot below.

![Initializing a new repository](https://adamtheautomator.com/wp-content/uploads/2021/11/image-334.png)

Initializing a new repository

4\. Next, specify the remote Git repository address for your project. This repository address determines whether you’ll authenticate with SSH keys or with username and password.

Find the syntax below, where `<username>` is your Gitlab username and `<project-name>` is the Gitlab repository name. Notice that the repository address begins with `git@gitlab.com`, which indicates that you’ll be authenticating with the SSH key instead of a username and password.

```powershell
git remote add origin git@gitlab.com:<username>/<project-name>.git
```

Suppose the Gitlab username is `kevin`, and the repository name is `my-first-project`. Now run the command in the terminal as follows.

```powershell
git remote add origin git@gitlab.com:kevin/my-first-project.git
```

5\. Run the command below to create an empty file in your repository called [README.md](http://README.md).

```bash
touch README.md
```

6\. Open the [README.md](http://README.md) file for editing and paste the following content into the file. Save the file after editing. `My first project to demonstrate Gitlab SSH keys.`

```powershell
My first project to demonstrate Gitlab SSH keys.
```

![Creating a file and adding content](https://adamtheautomator.com/wp-content/uploads/2021/11/image-335.png)

Creating a file and adding content

7\. Now, tell Git to add the new [README.md](http://README.md) file to the repository and commit the changes.

```powershell
git add README.md
git commit -m "My first commit"
```

You should see a similar output as shown below.

![Adding a file and committing changes to the repository](https://adamtheautomator.com/wp-content/uploads/2021/11/image-336.png)

Adding a file and committing changes to the repository

8\. Finally, time to publish your new repository. Run the command below to push the repository from your computer to your Gitlab account.

```powershell
git push --all origin
```

As you can see below, the project creation was successful, and there were no credential prompts! Git used your SSH keys to authenticate with Gitlab.

![Creating a repository in Gitlab](https://adamtheautomator.com/wp-content/uploads/2021/11/image-337.png)

Creating a repository in Gitlab

9\. Finally, confirm that your new Gitlab project exists online. Using your browser, navigate to https://gitlab.com/dashboard/projects to view a list of your existing projects. You should then see the **my-first-project** name on the list.

![Viewing the Projects list in Gitlab](https://adamtheautomator.com/wp-content/uploads/2021/11/image-338.png)

Viewing the Projects list in Gitlab

10\. Furthermore, click on the project name, and you should see the [README.md](http://README.md) file and contents on display.

![Viewing the README file](https://adamtheautomator.com/wp-content/uploads/2021/11/image-339.png)

Viewing the README file

## Conclusion

Congratulations on having made this far in this tutorial! Throughout this step-by-step tutorial, you’ve learned how to generate and use SSH keys to make secure version control deployments to your GitLab repository.

With this knowledge, you can now perform tasks on your Gitlab repositories without worrying about inadvertently exposing your username and password.

Have you used Gitlab SSH key authentication before? How was the experience? Do you recommend others to use SSH keys, too, or is there any reason they should stay away from it? Let us know.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fgitlab-ssh%2F&text=How%20to%20Generate%20and%20Use%20GitLab%20SSH%20Keys)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fgitlab-ssh%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fgitlab-ssh%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2024/01/gitops.jpg)

### [A Getting Started Guide to GitOps](/gitops/)

Embark on your GitOps journey—seamless, automated, and efficient. Explore the power of GitOps for streamlined workflows in this tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2021/12/How-To-Combine-Branches-With-Git-Merge.jpg)

### [Git Merge Simplified: Merging Branches Like a Pro](/git-merge/)

Master Git merge with this tutorial: Learn seamless branch combination techniques and best practices to enhance your development workflow, avoiding common errors.

![](https://adamtheautomator.com/wp-content/uploads/2023/07/gitea-docker.jpg)

### [Gitea Docker: Your Ultimate Self-Hosted Git Solution](/gitea-docker/)

Learn how to install a Gitea Docker instance and self-host Git repositories securely in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
