我的问题是我不能从GitLab中push或fetch。但是,我可以克隆(通过HTTP或SSH)。当我试图推的时候,我得到这个错误:

权限被拒绝(publickey)致命:无法从远程存储库读取

从我看过的所有线索来看,以下是我所做的:

在我的电脑上设置一个SSH密钥,并将公钥添加到GitLab 完成了用户名和电子邮件的config -global 通过SSH和HTTP克隆,以检查它是否能解决问题 执行ssh -T git@gitlab.com命令

如果您对如何解决我的问题有任何见解,我将不胜感激。


当前回答

在我的案例中,它在WSL (Linux的Windows子系统)中不起作用。

当我开始WSL时,我必须

启动ssh-agent_ eval $(ssh-agent -s) 将密钥添加到ssh-agent: ssh-add ~/.ssh/id_rsa 如果有提示,请输入密码

现在连接起作用了。 我们可以使用ssh -T git@github.com进行测试

注:

weasel-pageant允许我们在WSL中重用在PuTTY pageant中加载的ssh密钥 详细解释:Git通过SSH从Windows返回Permission Denied

其他回答

我是这样解决的。

使用此命令生成Windows密钥:

ssh-keygen -t rsa -C "your.email@example.com" -b 4096

但问题是,在运行这个命令后,它弹出了一行: 输入保存密钥的文件(/c/Users/xxx/.ssh/id_rsa): " 在这里,我只给文件名,因为我的密钥被保存在我的pwd,而不是在给定的位置。当我做“git克隆”时,它假设键位于“/c/Users/xxx/”。Ssh /id_rsa”的位置,但没有找到,因此抛出错误。

在密钥生成时,生成了2个文件,即“file1”和“file1.pub”。我将这两个文件重命名为

file1 -> id_rsa 

and

file1.pub -> id_rsa.pub

并将它们放在“/c/Users/xxx/.ssh/”的位置。

我在gitlab help中找到了解决方案。

To create a new SSH key pair: 
 1. Open a terminal on Linux or macOS, or Git Bash / WSL on Windows.
 2. Generate a new ED25519 SSH key pair: ssh-keygen -t ed25519 -C "email@example.com"
 2.1 Or, if you want to use RSA: ssh-keygen -o -t rsa -b 4096 -C "email@example.com"
 3. Next, you will be prompted to input a file path to save your SSH key pair to... use the suggested path by pressing Enter
 4. Once the path is decided, you will be prompted to input a password to secure your new SSH key pair. It's a best practice to use a password, but it's not required and you can skip creating it by pressing Enter twice.
 5. Copy your public SSH key to the clipboard by using one of the commands below depending on your Operating System:
        macOS:        pbcopy < ~/.ssh/id_ed25519.pub
        WSL / GNU/Linux (requires the xclip package):      xclip -sel clip < ~/.ssh/id_ed25519.pub
        Git Bash on Windows:      cat ~/.ssh/id_ed25519.pub | clip
 6. Navigating to SSH Keys and pasting your public key in the Key field
 7. Click the Add key button

我希望它能帮助到你们中的一些人!

另一个可能导致这种行为的问题是当你有2个可能的%HOME%-locations的设置时。

我使用的是个人电脑,其中一些文档存储在本地,另一些存储在网络驱动器上。一些应用程序认为C:\Users\<MyUserName>\是my %home%,另一些应用程序认为U:\是home。

原来ssh-keygen把我的私钥放在C:\users\<MyUserName>\下,ssh -T和ssh -v也在那里查找。

所以一切似乎都很好,除了git克隆,git推和其他人在U:\寻找一个键。 它失败了,所以我得到了前面提到的错误。

我花了一个小时才找到答案,但最终解决方案很简单:我复制了C:\Users\<MyUserName>\中的所有内容。ssh到U:\.ssh

步骤1: 在~/中添加了一个配置文件。Ssh /config文件,看起来像

   User git
   Hostname gitlab.com
   IdentityFile ~/.ssh/id_rsa_gitlab
   TCPKeepAlive yes
   IdentitiesOnly yes

步骤2:克隆没有sudo的git repo。 文档:https://gitlab.com/help/ssh/README working-with-non-default-ssh-key-pair-paths

**有时在~/中有配置。ssh/config,但是,IdentityFile路径错误。您可以像这样检查文件名ls ~/.ssh/。gitlab的文件通常是id_ed25519。因此正确的配置是IdentityFile ~/.ssh/id_ed25519

我解决了git@gitlab.com:权限拒绝(公钥)问题使用以下说明

运行cat ~/.ssh/id_rsa.pub id_rsa副本。pub(公钥)到您的getlab '设置-> SSH密钥 运行cat ~/.ssh/id_rsa 将id_rsa(私钥)拷贝到“Code_repo->git_auth->id_rsa”

注意:如果您在DockerFile或其他任何地方使用root用户,请注意机器用户,然后在运行上述命令之前使用sudo su来获取root用户的公钥和私钥。