11

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.

mmelnik
  • 583
  • 5
  • 12
  • do you have the same public key as printed by `ssh-add -L` added in the git server? – Jakuje Feb 18 '16 at 13:06
  • 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. – mmelnik Feb 18 '16 at 14:33
  • 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? – Jakuje Feb 18 '16 at 14:36
  • 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. – mmelnik Feb 18 '16 at 15:13

3 Answers3

11

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.

Searene
  • 25,920
  • 39
  • 129
  • 186
  • This helped me. git was working in the Windows command line, but not the Cygwin bash command line. – JackAce May 17 '18 at 16:34
2

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
Til
  • 5,150
  • 13
  • 26
  • 34
Ajay
  • 21
  • 1
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.

Jakuje
  • 24,773
  • 12
  • 69
  • 75
  • 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. – mmelnik Feb 18 '16 at 16:59
  • 1
    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. – Jakuje Feb 18 '16 at 17:26
  • 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. – mmelnik Feb 18 '16 at 17:49
  • 1
    Well, you don't have to. But if the agent does not work in your cygwin shell? What about `ssh -vvvT git@gitlaburl verify` – Jakuje Feb 18 '16 at 18:16
  • 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. – mmelnik Feb 19 '16 at 12:22