Cygwin ssh key added, but Git permission denied (publickey)
Asked Answered
K

3

13

I have a problem when trying to use Git with Cygwin. I've generated and added ssh key to the GitLab server and everything works just fine through the MINGW64 (cloning, pulling, etc), but I wanted to use Cygwin and found that it doesn't work.

Though I've put copy of my generated key to the ~/user/.ssh folder and manually added key, so "ssh-add -l" prints it in the list, but when I try to fetch repository (or any other server command) I just get:

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

It looks like a bug, but possibly I'm doing something wrong. Did anyone get this problem? Thank you.

UPDATE: After updating OpenSSH version in Cygwin described problem has gone.

Kevakevan answered 18/2, 2016 at 12:56 Comment(4)
do you have the same public key as printed by ssh-add -L added in the git server?Ashwell
Yes, I've added same key that already worked for the Git Bash to the user folder of Cygwin and added it in the same way. And I have same public key on server and in user folder too. So I'm really confused by such behavior.Kevakevan
what is the difference in MINGW64 and cygwin for you? Do you have the agent and connection to it in the cygwin shell from where you do the clone?Ashwell
I use Cygwin as the NetBeans terminal. And MinGW came with portable Git version for Windows. There is no reasons not to use MinGW, but I was trying to set up Cygwin as main terminal and get stuck on this. @Jakuje, what did you mean about agent and connection? I have running agent in Cygwin and ssh-add worked fine for me.Kevakevan
E
13

For me, the cause is that I put my ssh key files in C:\Users\username\.ssh (which is /cygdrive/c/Users/username/.ssh in cygwin), but actually, you need to put your ssh keys in ~/.ssh to make it work. They are two different directories.

Run the following command in cygwin solved my problem.

cp /cygdrive/c/Users/username/.ssh/* ~/.ssh/

Notice that you should replace username with your actual one.

Eld answered 25/8, 2016 at 6:55 Comment(1)
This helped me. git was working in the Windows command line, but not the Cygwin bash command line.Oliguria
J
3

If you used a non-default file path for your git SSH key pair, you must configure your SSH client to find your git private SSH key for connections to GitLab.

You need to run the following commands to fix:

eval $(ssh-agent -s)
ssh-add ~/.ssh/other_id_rsa
Joeyjoffre answered 15/4, 2019 at 13:42 Comment(0)
A
1

The agent does not have to be only running, but your tools have to know where is the agent listening. It is stored in variable $SSH_AUTH_SOCK and if it works for you from one terminal, it does not have to from the second one.

If you want to have it working in your NetBeans, you need to inject this variable into the NetBeans environment variables (but not sure how to do that so it would be passed from windows environment to the Cygwin terminal in NetBeans).

Or inject it later into the running terminal (possibly using .bashrc or other startup scripts). Simple test case would be to echo $SSH_AUTH_SOCK in the MinGW terminal and then write export SSH_AUTH_SOCK=/the/path/you/got/from/previous/command into the Cygwin terminal.

Later on you can automate it by storing the variable into some file, that you can read in the Cygwin.

# MinGW scriplet 
echo "export SSH_AUTH_SOCK=$SSH_AUTH_SOCK" >> ~/agent_env

# Cygwin scriplet
. ~/agent_env

Then you should be able to use your mingw agent from cygwin shell.

Ashwell answered 18/2, 2016 at 15:51 Comment(5)
Thank you for answer! I've got your point, but it is not clear for me why I should put agent from MinGW to Cygwin, as they are two different terminals with different agents running on them (different SSH_AUTH_SOCK). And they also use different .bashrc configurations (mostly the same) and separate resource folders (especially Cygwin which has it's own *nix-like infrastructure). Could you explain me this, please? I mentioned before that I've added my key in both of agents.Kevakevan
The problem is that none of them is native in Windows and both of them behave as standalone layer above the native Windows and do not know about the other (and there is no reasonable way to share environment between different standalone shells, because *NIX processes are designed to inherit environment from each other and the first one is your session which contains all you need). You might probably run one shell from the other, but probably not native windows application.Ashwell
That's why I ask you why I have to share agent between two shells. I use only one at a time. Let's imagine that I had MinGW before and it was working fine, but now I want to move fully to Cygwin and got that problem with ssh key, despite of fact that all config seem to be fine for me.Kevakevan
Well, you don't have to. But if the agent does not work in your cygwin shell? What about ssh -vvvT git@gitlaburl verifyAshwell
Ok, I found that in Cygwin a had OpenSSH 6 while in MinGW it was 7.1p1, so I've updated it and suddenly everything began to work. ssh -vvvT git@gitlaburl verify went well in both cases with no errors.Kevakevan

© 2022 - 2024 — McMap. All rights reserved.