How To Fix "Permission Denied (Publickey)" Error In GitLab?

Published August 22, 2024

Problem: GitLab SSH Access Error

The "Permission Denied (Publickey)" error in GitLab happens when you try to access a repository using SSH. This error stops you from doing Git operations like cloning, pushing, or pulling code from your GitLab repositories.

Troubleshooting Steps for GitLab SSH Access

Verifying SSH key setup

To fix GitLab SSH access issues, check if you have SSH keys on your computer. Open a terminal or Git Bash and run ls -al ~/.ssh to list SSH key files. If you see id_rsa and id_rsa.pub, you have SSH keys.

If you don't have SSH keys, create new ones with ssh-keygen -t rsa -b 4096 -C "your_email@example.com". Follow the prompts to create the keys, using the default file location and adding a passphrase if you want.

After creating SSH keys, add the public key to your GitLab account. Copy the content of your id_rsa.pub file and paste it into the SSH Keys section of your GitLab profile settings.

Tip: Backup Your SSH Keys

After creating your SSH keys, make a backup of both the public and private key files. Store them in a secure location, such as an encrypted external drive. This backup can save you time and trouble if you need to set up GitLab access on a new machine or if your current machine fails.

Confirming Git configuration

Check your Git configuration. Set up your Git username and email with these commands:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

Make sure the remote repository URL uses SSH instead of HTTPS. Use git remote -v to view the current URL. If it starts with https://, update it to the SSH URL with git remote set-url origin git@gitlab.com:username/repository.git.

Testing SSH connection to GitLab

Test your SSH connection to GitLab with ssh -T git@gitlab.com. If it works, you'll see a welcome message. If you get an error, it may show issues with your SSH key setup or GitLab account settings.

If the test fails, read the error message. It might tell you to add the GitLab server to your known hosts or point out problems with your SSH key. Fix these issues and try the test again until it works.

Step-by-Step Solution to Fix the Error

Generate a new SSH key pair

To fix the "Permission Denied (Publickey)" error, create a new SSH key pair:

  1. Open Git Bash as an administrator. Right-click on the Git Bash icon and select "Run as administrator."

  2. In the Git Bash window, type this command:

    ssh-keygen
  3. Press Enter when asked for the file location to save the key in the default directory.

  4. You can set a passphrase or leave it blank. Press Enter twice to continue without a passphrase.

Tip: Choose a Strong Passphrase

For better security, consider using a strong passphrase when generating your SSH key. A good passphrase is long, unique, and easy for you to remember but hard for others to guess. Avoid using common phrases or personal information.

Add the new SSH key to GitLab

After generating the SSH key, add it to your GitLab account:

  1. Find the public key file. It's usually in the .ssh folder in your user directory.

  2. Open the id_rsa.pub file with a text editor like Notepad.

  3. Copy all the text from the file.

  4. Go to the GitLab website and log in to your account.

  5. Go to your profile settings and find the "SSH Keys" section.

  6. Paste the copied public key into the "Key" text field.

  7. The "Title" field should fill in automatically. If not, add a title.

  8. Click "Add key" to save your new SSH key.

Update local Git configuration

To complete the process, update your local Git configuration:

  1. Open Git Bash in your project directory.

  2. Set the correct remote URL for your repository using this command:

    git remote set-url origin git@gitlab.com:your-username/your-repository.git

    Replace your-username and your-repository with your GitLab username and repository name.

  3. Verify the changes by running:

    git remote -v

    This command should show the updated SSH URL for your repository.

After following these steps, try pushing or fetching from your GitLab repository again. The "Permission Denied (Publickey)" error should now be fixed.