简体   繁体   English

Cygwin ssh密钥已添加,但Git权限被拒绝(公钥)

[英]Cygwin ssh key added, but Git permission denied (publickey)

I have a problem when trying to use Git with Cygwin. 尝试在Cygwin中使用Git时遇到问题。 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. 我已经生成了ssh密钥并将其添加到GitLab服务器,并且一切都可以通过MINGW64正常工作(克隆,拉取等),但是我想使用Cygwin并发现它不起作用。

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: 尽管我将生成的密钥的副本放置到〜/ user / .ssh文件夹中并手动添加了密钥,所以“ ssh-add -l”将其打印在列表中,但是当我尝试获取存储库(或任何其他服务器)时命令)我得到:

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. 更新:在Cygwin中更新OpenSSH版本后,描述的问题消失了。

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. 对我来说,原因是我将ssh密钥文件放在C:\\Users\\username\\.ssh (在cygwin中为/cygdrive/c/Users/username/.ssh )中,但实际上,您需要将ssh放入键入~/.ssh使其起作用。 They are two different directories. 它们是两个不同的目录。

Run the following command in cygwin solved my problem. 在cygwin中运行以下命令解决了我的问题。

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

Notice that you should replace username with your actual one. 请注意,您应该用实际的username替换username

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. 它存储在变量$SSH_AUTH_SOCK ,如果从一个终端为您工作,则不必从另一个终端开始。

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). 如果要在NetBeans中使用它,则需要将此变量注入到NetBeans环境变量中(但不确定如何执行该操作,以便将其从Windows环境传递到NetBeans中的Cygwin终端)。

Or inject it later into the running terminal (possibly using .bashrc or other startup scripts). 或者稍后将其注入正在运行的终端中(可能使用.bashrc或其他启动脚本)。 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. 一个简单的测试用例是在MinGW终端中echo $SSH_AUTH_SOCK ,然后将export SSH_AUTH_SOCK=/the/path/you/got/from/previous/command写入Cygwin终端。

Later on you can automate it by storing the variable into some file, that you can read in the Cygwin. 稍后,您可以通过将变量存储到某个文件中来自动化该文件,您可以在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. 然后,您应该能够从cygwin shell使用mingw代理。

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. 如果您为git SSH密钥对使用了非默认文件路径,则必须配置SSH客户端以找到用于连接到GitLab的git私有SSH密钥。

You need to run the following commands to fix: 您需要运行以下命令来修复:

eval $(ssh-agent -s)
ssh-add ~/.ssh/other_id_rsa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM