How to Generate and Use GitLab SSH Keys

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

GitLab offers two types of SSH keys, one for public and one private. The difference between the two is that your private key will only work on GitLab’s servers and not anywhere else in the world. To generate a new SSH key pair from scratch, you’ll need to create an RSA or DSA fingerprint called “ssh-rsa” first which can be found by running ssh-keygen -l . Once this has been generated, add it to your ~/.ssh/config file with something like: Host gitlab.com IdentityFile /home/ahmad/.ssh/id_rsa

GitLab is a popular open source project management and code collaboration platform. To set up SSH keys, you will need to generate them. This can be done by following the steps below:

How to Generate and Use GitLab SSH Keys

Username and password prompts are inconvenient and time-consuming. If your Gitlab account uses password authentication, each action needs you to provide your credentials, whether as part of the command or via an interactive prompt.

Password authentication may be inefficient and sometimes dangerous, especially when automating GitLab’s CI/CD processes. Why not put an end to your misery and use Gitlab SSH keys for Secure Shell (SSH) authentication? Continue reading to find out how!

You will have produced an SSH key pair, used the key to authenticate, and published a new Gitlab project by the Conclusion of this lesson.

Prerequisites

This is an interactive presentation. Prepare the following criteria in order to follow along.

  • Your GitLab profile. Create a free account first if you don’t already have one.
  • A PC that will serve as your Gitlab SSH client This lesson will be performed on a PC running Ubuntu 20.04 LTS, but it should also work on Windows and Mac.

Related: [Step-by-Step] How to Install Ubuntu 20.04

  • Install or update Git. As of this writing, the most recent version is 2.25.1.

Code in Visual Studio (VS Code). This lesson will use VS Code 1.62.3 as an example.

A Beginner’s Guide to Git and Visual Studio Code

Creating an SSH Key Pair for Gitlab

A key pair, or SSH keys, is made up of private and public keys. In a word, the client (your computer) provides the private key to the server (Gitlab) for authentication, and the server uses the public key to decode your private key. The authentication is successful if both keys match.

GitLab supports two different kinds of SSH key pairs: RSA and ED25519. However, you will produce the ED25519 key pair in this example since it is regarded more secure than RSA keys. To create an SSH key pair, follow the instructions below.

1. Open a terminal session on your desktop and type the command below. In VS Code, this command opens your home directory.

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

In VSCode, create a new terminal instance. In VSCode, create a new terminal instance.

3. Run ssh-keygen -t in the VS Code console to produce the ED25519 SSH key type. The -C comment tag is optional, however it is advised to help identify your keys.

ssh-keygen -C ed25519 -t ed25519 “SSH Key for Gitlab”

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 filename as is Accepting the filename as is

5. Finally, keep the password field blank and hit Enter twice. As a consequence, a passwordless SSH key pair will be created.

Leaving the pass unattended Leaving the pass unattended

The private key (id ed25519) and public key (id ed25519.pub) locations should look similar to the screenshot below.

The base filename for both the private and public keys is the same. However, the public key has a.pub suffix, but the private key does not.

Creating the SSH key pair for Gitlab Creating the SSH key pair for Gitlab

Your Gitlab SSH Key in Your Profile

The private key should be kept on your machine, while the public key should be kept on the Gitlab server. Following the creation of the SSH keys, you must submit the public key to your Gitlab account. To do so, follow these steps.

1. In VSCode, open the public key file. Expand the.ssh folder in the Explorer window and choose id es25519.pub. After that, copy the public key to the clipboard.

The public key is copied. The public key is copied.

2. Open a web browser and go to https://gitlab.com to sign into your Gitlab account.

3. Go to https://gitlab.com/-/profile/keys after logging in to the SSH Keys profile settings.

  • In the Key area, paste the public key you copied previously.
  • The comment section of your public key is automatically filled in the Title form. Leave the title value blank or change it to anything else.
  • Specify an expiry date in the Expires at field if desired.
  • Finally, choose Add key.

Your Gitlab SSH Key in Your Profile Your Gitlab SSH Key in Your Profile

Making Use of Your Gitlab SSH Key

You’ve created an SSH key and submitted it to your Gitlab account so far. The last step is to test your SSH key by connecting to Gitlab and authenticating with it.

Logging On

Connect to your Gitlab account using the command below in the VS Code console.

If you’re connecting for the first time, the command will ask you to authenticate the connection and check the host’s legitimacy. To confirm, type yes at the popup and hit Enter.

You may have noticed that you did not need to provide a username or password to log in. Instead, the ssh program utilizes your SSH keys to authenticate automatically. After logging in successfully, you should get a message that says, “Welcome to Gitlab @username!”

Using SSH to access Gitlab Using SSH to access Gitlab

Introducing a New Project

You’ve verified that your Gitlab SSH key works and that you can successfully authenticate. Does it, however, function when you interface with Gitlab through Git? Why not put it to the test by creating a new Gitlab repository?

1. First, set up your Gitlab account’s username and email address in the Git settings. Run the git config instructions below in the VS Code console to enter your Gitlab login and email address, accordingly.

git config —global user.email “[email protected]” git config —global user.name “your username”

With the information you supplied, the command you executed should update or create the /.gitconfig file.

Getting the Git setup started Getting the Git setup started

2. Next, create a folder for your new repository in your home directory. My-first-project should be the name of the new folder.

Your Gitlab repository project will be named after the folder name.

# Make a new folder using the command mkdir /my-first-project # Change to the new folder cd /my-first-project as the working directory.

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

A confirmation notice similar to the one shown below should appear.

Setting up a new repository Setting up a new repository

4. Next, enter the address of your project’s remote Git repository. This repository address affects whether you’ll need SSH keys or username and password authentication.

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 [email protected], which indicates that you’ll be authenticating with the SSH key instead of a username and password.

Assume that your Gitlab login is kevin and your repository is called my-first-project. Run the following command on the terminal.

5. Run the command below to generate an empty README.md file in your repository.

6. To modify the README.md file, open it and put the following text into it. After you’ve finished modifying, save the file. My initial endeavor was to show how to use Gitlab’s SSH keys.

My initial endeavor was to show how to use Gitlab’s SSH keys.

Making a file and filling it with information Making a file and filling it with information

7. Tell Git to commit the changes and add the new README.md file to the repository.

add README.md to git “My first commit,” git commit -m

You should get something similar to what’s seen below.

Adding a file to the repository and committing modifications Adding a file to the repository and committing modifications

Finally, it’s time to make your new repository public. To push the repository from your PC to your Gitlab account, use the command below.

The project creation was successful, as you can see below, and there were no credential prompts! Gitlab was authenticated using your SSH keys.

Creating a Gitlab repository Creating a Gitlab repository

9. Finally, make sure your new Gitlab project is visible on the internet. To see a list of your existing projects, go to https://gitlab.com/dashboard/projects on your browser. The name my-first-project should now appear in the list.

Viewing the Gitlab Projects list Viewing the Gitlab Projects list

Additionally, click on the project name, and the README.md file and contents should appear.

Looking at the README file Looking at the README file

Conclusion

Congratulations for getting this far in the tutorial! You learnt how to produce and utilize SSH keys to conduct secure version control deployments to your GitLab repository in this step-by-step guide.

With this information, you may conduct actions on your Gitlab repositories without fear of accidentally disclosing your login and password.

Have you utilized SSH key authentication with Gitlab before? How did you find the experience? Do you think others should use SSH keys as well, or is there any reason they shouldn’t? Please let us know.

The “github generate ssh key” is a command that generates SSH keys for GitLab. The “github generate ssh key” can be used to create and use SSH keys for GitLab.

Related Tags

  • gitlab ssh key windows
  • gitlab clone with ssh
  • how to generate ssh key
  • generate ssh key windows
  • generate ssh key ubuntu

Table of Content