I have Windows 10 and WSL Ubuntu both installed. I am encountering a problem where I am able to verify that my SSH key pair is correctly configured by using the ssh command but failing at cloning from all repositories.
Both Ubuntu and Windows keys give a successful response to the command
ssh -T git@gitlab.com
Ubuntu key works completely fine when performing cloning operation. But windows key is the one constantly presenting the error of
git@gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I have deleted all the keys from my GitLab profile, generated again. The two keys are not the same but I have tried with them being the same at some point.
I followed every procedure laid out in this website and still had errors.
I found the solution to this problem in the following thread and will paraphrase it here.
Git does not by default use the same SSH path as the SSH command. Therefore, verifying connection with SSH to Gitlab (or GitHub) is not guaranteed to work with cloning repositories via SSH. It needs to be added to the system environment. This is not the case on Linux since git and ssh are pre-installed. You'll need to specify the path variable for Git.
Start a command prompt window (don't use Power Shell) and use the following commands.
where ssh
This should return the location of the SSH program. My system returned the following.
C:\Windows\System32\OpenSSH\ssh.exe
Check the path that Git is using for SSH by using the command
set GIT_SSH
This will return either an empty path or the current path selected. If it differs from the one previously found, then you must set the path.
This next part will be a test run and then it will be made into a permanent solution once confirmed.
In the same command prompt window use the same set command but this time assign it the path found.
set GIT_SSH=C:\Windows\System32\OpenSSH\ssh.exe
or
set GIT_SSH=[YOUR PATH TO SSH]
Check that with this modification you can clone using SSH. If this works then we now take steps to make the correction permanent in all terminal command prompts and power shell.
Follow the next steps for Windows10 (might differ for Windows11)
Confirm this change has taken effect by closing the command prompt window or starting a new command prompt or power shell and trying again to clone the command. If the clone command works, then then it's fixed.
Enjoy!!