Generate RSA Key for Commits and PULL in Gitlab Without Authentication

Tiempo de lectura: 2 minutos

Reading time: 3 minutes

Today, I’m going to show you how to create an RSA key to use in your GITLAB projects https://gitlab.com/

First, let’s go to the Ubuntu console (you can use WSL if you’re on Windows)

ssh-keygen -t rsa -b 4096 -C "email@example.com"

Replace “email@example.com” with the email of your Gitlab account.

It will ask you for a file name and a password (you can leave the password blank if you don’t want to enter it every time you use the key).

Once generated, open the .pub file created in the same directory. Copy the content and paste it into the Gitlab page.

Go to Profile > Preferences > SSH Keys

Then add the key (Remember to copy the entire key from ssh-rsa to your email):

Now, save it in a folder where you know its location. To use it in the console, use this command:

Remember to add read permissions to the key:

chmod 400 config/git/key
  • To clone:
git clone repository(git) --config core.sshCommand="ssh -i config/git/key/"
  • To pull:
GIT_SSH_COMMAND='ssh -i ../config/git/key' git pull origin master'

You can also add the RSA key directly to the global project (https://docs.gitlab.com/ee/user/ssh.html)

RSA Key for Remote Pull in Gitlab

In order to perform a remote pull, we need an RSA key from GITLAB. To generate it, follow these steps:

Go to your user and select SSH Keys

  1. Generate a local RSA key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

When prompted for the key’s name, enter:

.ssh/id_rsa

This will generate a private key and a public key in the specified path.

  1. Add the public key to GitLab:
  • Copy the content of the public key (cat ~/.ssh/id_rsa.pub) to your clipboard.
  • Open GitLab and navigate to your profile > settings > SSH Keys.
  • Click on “Add SSH Key”.
  • Give the key a name (e.g., “My GitLab SSH Key”).
  • Paste the content of the public key into the “key” field.
  • Click “Add Key”.
  1. Add the private key to your local machine:
  • Copy the private key (cat ~/.ssh/id_rsa) to a local file (e.g., gitlab_rsa).
  • Restrict permissions on the private key (chmod 400 gitlab_rsa) to prevent other users from reading it.
  • Add the private key to your SSH agent (ssh-add gitlab_rsa).

If the ssh-add command didn’t work, you can use this command instead:

sudo ssh-agent sh -c 'ssh-add .ssh/gitlab_rsa'

You should now be able to perform a pull without entering your username and password every time.

To assign the SSH key to the GIT repository for it to work, use the following command (included in the Pipeline, but you can do it manually and skip it):

git remote set-url origin git@gitlab.com:<user-project>/<project-name>.git

If the key doesn’t work, you can rename it to ./.ssh/id_rsa

If the key works with root, you need to copy the key to the /ro

Leave a Comment