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

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

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

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

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


当前回答

步骤要做,得到同样的错误,但我修复了它。 Gitlab需要ssh-rsa,所以下面是运行ssh for rsa的代码

ssh-keygen -o -t rsa -b 4096 -C "name@gmail.com"

name@gmail.com是您的gitlab帐户电子邮件

It will prompt you to enter so just hit Enter after the below code is prompt, Enter file in which to save the key (/home/yourDesktopName/.ssh/id_rsa): It will prompt again you to enter so just hit Enter after the below code is prompt, Enter passphrase (empty for no passphrase): It will prompt again for the last you to enter so just hit Enter after the below code is prompt, Enter same passphrase again: You will show your ssh-rsa generate. Login to your Gitlab account and Go to the right navbar you will get setting and in the left sidebar you will get ssh key. Enter in it. Look above the prompt asking you to enter, you will get the path of ssh-rsa. Go to your SSH folder and get the id_rsa.pub Open it and get the key and Copy Paste to the Gitlab and you are nearly to done. Check by: ssh -T git@gitlab.com You will get: Welcome to GitLab, @joy4! Done.

其他回答

我有gitlab运行docker,这是我所做的解决我的问题。

发现在docker /var/log/gitlab/sshd/current内部出现了多条消息:

身份验证被拒绝:文件/var/opt/gitlab/.ssh/authorized_keys的所有权或模式错误

之后,我将该文件的所有权从99:users更改为git:users:

Chown git:users authorized_keys

我找了很久才找到这个。这对我来说是完美的。

Go to "Git Bash" just like cmd. Right click and "Run as Administrator". Type ssh-keygen Press enter. It will ask you to save the key to the specific directory. Press enter. It will prompt you to type password or enter without password. The public key will be created to the specific directory. Now go to the directory and open .ssh folder. You'll see a file id_rsa.pub. Open it on notepad. Copy all text from it. Go to https://gitlab.com/-/profile/keys or Paste here in the "key" textfield. Now click on the "Title" below. It will automatically get filled. Then click "Add key".

现在试一试,肯定会管用的。

我在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

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

我知道,我回答这个问题很晚,甚至StackOverflow确认我是否真的想回答。我回答是因为没有人真正描述了实际的问题,所以想分享。

最基本的

首先,了解这里的遥控器是什么。Remote是GitLab,而你的系统是本地的,所以当我们讨论远程源时,在你的git Remote -v输出中设置的URL就是你的远程URL。

的协议

基本上,Git的克隆/推/拉主要适用于两种不同的协议(也有其他协议)

HTTP协议 SSH协议

当你克隆一个repo(或更改远程URL)并使用HTTPs URL如https://gitlab.com/wizpanda/backend-app.git时,它使用第一种协议,即HTTP协议。

而如果你克隆了这个repo(或者改变了远程URL),并使用git@gitlab.com:wizpanda/backend-app.git这样的URL,那么它就会使用SSH协议。

HTTP协议

在这个协议中,每个远程操作,即克隆、推和拉,都使用简单的身份验证,即远程(在本例中为GitLab)的用户名和密码,这意味着对于每个操作,您必须输入您的用户名和密码,这可能很麻烦。

所以当你推/拉/克隆时,GitLab/GitHub用你的用户名和密码验证你,它允许你做操作。

如果你想尝试这样做,你可以通过运行命令git remote set-url origin < HTTP -git- URL >来切换到HTTP URL。

为了避免这种情况,可以使用SSH协议。

SSH协议

简单的SSH连接使用公私密钥对。因此,在您的情况下,GitLab无法对您进行身份验证,因为您使用SSH URL进行通信。现在,GitLab一定以某种方式了解你。为此,您必须创建一个公共-私有密钥对,并将公钥提供给GitLab。

现在当你用GitLab推/拉/克隆时,GIT(内部是SSH)将默认提供你的私钥给GitLab并确认你的身份,然后GitLab将允许你执行操作。

所以我不会重复默罕默德已经给出的步骤,我只是从理论上重复。

生成密钥对' SSH -keygen -t rsa -b 2048 -C "My Common SSH key " 默认情况下,生成的密钥对在~/中。SSH名为id_rsa。Pub(公钥)& id_rsa(私钥)。 您将把公钥存储到您的GitLab帐户(相同的密钥可以用于多个或任何服务器/帐户)。 当你克隆/推/拉时,GIT会提供你的私钥。 GitLab将私钥与公钥匹配,并允许您执行。

Tips

您应该始终创建一个至少2048字节的强rsa密钥。执行命令为ssh-keygen -t rsa -b 2048。

https://gitlab.com/help/ssh/README#generating-a-new-ssh-key-pair

一般认为

这两种方法都有各自的优点和缺点。在我输入上面的文本后,我去搜索了更多关于这个的信息,因为我从来没有读过关于这个的东西。

我找到了这个官方文档https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols,它讲述了更多关于这方面的内容。我在这里的观点是,通过阅读错误并对错误进行思考,你可以建立自己的理论或理解,然后可以匹配一些谷歌结果来解决问题:)

我花了好几个小时才修好的。下面的方法对我很有效。

在终端上执行这些命令。

ssh-keygen -t rsa -C "<your email address>" -b 4096 -t ed25519

将生成的密钥重命名为:id_rsa和id_rsa。将生成的公钥添加到gitlab服务器。

然后将ssh密钥添加到ssh代理,如下所示。

启动ssh-agent Eval "$(ssh-agent -s)" 如果生成的密钥名称不同,请在下面的命令中替换id_rsa ssh-add ~ / . ssh / id_rsa

要测试连接,请使用此命令。

ssh -T git@<git lab server url>

要排除故障,请使用以下命令。

ssh -Tv git@<git lab server url>
ssh -vvv git@<git lab server url>

现在可以将项目克隆到本地环境。