How to Fix Permission Denied (public key) With Gitlab?

  • 7,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 7
  • Tình trạng: Còn hàng

Last Updated : 05 Sep, 2024

The "Permission denied (public key)" error is a common issue encountered when using Git with GitLab on Windows. This error occurs when GitLab is unable to lớn authenticate your system using SSH (Secure Shell). This usually happens due to lớn a misconfiguration of SSH keys, where either the key is missing, not added to lớn the SSH agent, or not linked to lớn your GitLab trương mục.

In this article, we will walk you through the steps to lớn resolve this issue, ensuring that your SSH keys are properly configured and recognized by GitLab. By the kết thúc of this guide, you should be able to lớn fix the "Permission denied (public key)" error and work smoothly with GitLab on your Windows machine.

Prerequisites

Before we begin, ensure you have the following:

  • Git Installed
  • GitLab Account
  • Basic Git Command

Understanding the Error: "Permission denied (public key)"

The error "Permission denied (public key)" occurs when your SSH client (Git) attempts to lớn connect to lớn GitLab but fails to lớn authenticate because GitLab does not recognize your SSH key. This can happen due to:

  • The SSH key not being generated or missing.
  • The SSH key not being added to lớn the SSH agent.
  • The SSH key not being registered with your GitLab trương mục.
  • Incorrect tệp tin permissions on the SSH keys.

Understanding these potential causes will help you systematically resolve the error.

Verifying SSH Connection:

ssh -T [email protected]

If you found this error means that your GitLab Account does not recognizes your SSH key.

Screenshot-2024-09-04-234655
Permission denied (public key)

Steps To Fix the "Permission denied (public key)" Error

Step 1: Verify SSH Key Pair

Check for Existing SSH Keys: Open Git Bash and execute the following command to lớn kiểm tra if you already have an SSH key:

ls -al ~/.ssh
jpeg-optimizer_Screenshot-2024-09-04-232714
Verify SSH Key Pair
  • This will list the contents of your .ssh directory. If you see files lượt thích id_rsa and id_rsa.pub, you already have an SSH key pair.
  • If you see files named id_rsa.pub and id_rsa, you already have an SSH key. If not, proceed to lớn the next step to lớn generate one.

Or Open Command Prompt or PowerShell and run:

dir %userprofile%\.ssh 
Screenshot-2024-09-04-232510
Verify SSH Key

Generate a New SSH Key (if needed):

  • If the .ssh directory does not contain id_rsa or id_rsa.pub, generate a new SSH key pair:
  • Open Git Bash, and use the following command to lớn generate a new SSH key:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
  • Press Enter to lớn accept the mặc định tệp tin location (/c/Users/YourName/.ssh/id_rsa).
  • Optionally, enter a passphrase for added security.
Screenshot-2024-09-04-233332
Generate New SSH Key

Step 2: Add the SSH Key to lớn the SSH Agent

Start the SSH Agent: Run the following command to lớn start the SSH agent:

 eval $(ssh-agent -s)

This will start the agent and display a process ID, indicating that it’s running.

Screenshot-2024-09-04-233614
Add the SSH Key to lớn the SSH Agent

Add Your SSH Key to lớn the Agent:

Now, add your SSH private key to lớn the ssh-agent. In Git Bash, use:

ssh-add ~/.ssh/id_rsa
Screenshot-2024-09-04-233855
Add Your SSH Key to lớn the Agent

Step 3: Add SSH Key to lớn GitLab

Copy Your SSH Key to lớn Clipboard: First, copy your SSH public key to lớn the clipboard. In Git Bash, you can use:

clip < ~/.ssh/id_rsa.pub

Add SSH Key to lớn GitLab:

  • Log in to lớn your GitLab trương mục.
  • Navigate to lớn your GitLab trương mục settings
  • Click on “SSH Keys” under “User Settings”
  • Give it a title (e.g., "Fix Permission denied (public key)") and click Add Key.
Screenshot-2024-09-04-235313
Our SSH key has been added successfully

You should now be able to lớn push to lớn your GitLab repository without the “Permission denied (publickey)” error.

Step 4: Verify SSH Connection to lớn GitLab

To confirm that your SSH key is correctly mix up, test the SSH connection to lớn GitLab:

ssh -T [email protected]

If your setup is correct, you should see a message like:

Screenshot-2024-09-04-235503
This message confirms that GitLab recognizes your SSH key.
  • This above output shows that we have successfully FIX the error Fix Permission denied (public key).
  • You should now be able to lớn push to lớn your GitLab repository without the “Permission denied (publickey)” error.

Improve