git 在 gitlab 上获取权限被拒绝(公钥)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40427498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:36:35  来源:igfitidea点击:

Getting permission denied (public key) on gitlab

gitubuntusshgitlab

提问by Alexandre Fernandes Bartolomeu

My problem is that I can't push or fetch from GitLab. However, I can clone (via HTTP or via SSH). I get this error when I try to push :

我的问题是我无法从 GitLab 推送或获取。但是,我可以克隆(通过 HTTP 或通过 SSH)。当我尝试推送时出现此错误:

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

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

From all the threads I've looked, here is what I have done :

从我看过的所有线程来看,这是我所做的:

  • Set up an SSH key on my computer and added the public key to GitLab
  • Done the config --global for username and email
  • Cloned via SSH and via HTTP to check if it would resolve the issue
  • Done the ssh -T [email protected] command
  • 在我的电脑上设置一个 SSH 密钥并将公钥添加到 GitLab
  • 为用户名和电子邮件完成配置 --global
  • 通过 SSH 和 HTTP 克隆以检查是否可以解决问题
  • 完成 ssh -T [email protected] 命令

If you have any insight about how to resolve my issue, it would be greatly appreciated.

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

回答by Muhammad Laraib Khan

I found this after searching alot. It will work perfectly fine for me.

我在搜索了很多之后找到了这个。它对我来说非常好。

  1. Go to "Git Bash" just like cmd. Right click and "Run as Administrator".
  2. Type ssh-keygen
  3. Press enter.
  4. It will ask you to save the key to the specific directory.
  5. Press enter. It will prompt you to type password or enter without password.
  6. The public key will be created to the specific directory.
  7. Now go to the directory and open .sshfolder.
  8. You'll see a file id_rsa.pub. Open it on notepad. Copy all text from it.
  9. Go to https://gitlab.com/profile/keys.
  10. Paste here in the "key" textfield.
  11. Now click on the "Title" below. It will automatically get filled.
  12. Then click "Add key".
  1. 像 cmd 一样转到“Git Bash”。右键单击并“以管理员身份运行”。
  2. 类型 ssh-keygen
  3. 按回车键。
  4. 它会要求您将密钥保存到特定目录。
  5. 按回车键。它会提示您输入密码或不输入密码。
  6. 公钥将创建到特定目录。
  7. 现在转到目录并打开.ssh文件夹。
  8. 你会看到一个文件id_rsa.pub。在记事本上打开它。从中复制所有文本。
  9. 转到https://gitlab.com/profile/keys
  10. 将此处粘贴到“key”文本字段中。
  11. 现在点击下面的“标题”。它会自动填满。
  12. 然后单击“添加密钥”。

Now give it a shot and it will work for sure.

现在试一试,它肯定会起作用。

回答by Fedo Peralta

Step 1: Added a config file in ~/.ssh/configfile which looks like

第 1 步:在~/.ssh/config文件中添加一个配置文件,如下所示

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

Step 2: Just clone the git repo WITHOUT sudo.
Documentation: https://gitlab.com/help/ssh/README#working-with-non-default-ssh-key-pair-paths

第 2 步:只需在没有 sudo 的情况下克隆 git 存储库。
文档:https: //gitlab.com/help/ssh/README#working-with-non-default-ssh-key-pair-paths

回答by Hussain

I think the simple solution is to add private key to authentication agent (if your key is not ~/.ssh/id_rsa),

我认为简单的解决方案是将私钥添加到身份验证代理(如果您的密钥不是~/.ssh/id_rsa),

ssh-add ~/.ssh/<your private key>

You basically let the ssh-agenttake care of it.

你基本上让ssh-agent他们照顾它。

Additionally, you can add it permanently.

此外,您可以永久添加它

回答by TmTron

In my case it did not work in the WSL (Windows Subsystem for Linux).

就我而言,它在 WSL(Linux 的 Windows 子系统)中不起作用。

When I start the WSL, I must

当我启动 WSL 时,我必须

  • start ssh-agent_ eval $(ssh-agent -s)
  • add the key to the ssh-agent: ssh-add ~/.ssh/id_rsa
  • if prompted, enter the password
  • 启动 ssh-agent_ eval $(ssh-agent -s)
  • 将密钥添加到 ssh-agent: ssh-add ~/.ssh/id_rsa
  • 如果出现提示,请输入密码

Now the connection works.
We can test this with ssh -T [email protected]

现在连接有效。
我们可以用ssh -T [email protected]

notes:

笔记:

回答by rivanov

make sure you are not running sudo git clone [email protected]:project/somethiing.git, otherwise ssh will look in /root/.sshinstead of the key you uploaded ~/.ssh/id_rsa

确保您没有运行sudo git clone [email protected]:project/somethiing.git,否则 ssh 将查找/root/.ssh而不是您上传的密钥~/.ssh/id_rsa

回答by Elad

There is a very simple solution to this: instead of working with ssh - move to https. to do this: in your project folder you have a .git folder in there - you have a config file - open it in a text editor and change the line

对此有一个非常简单的解决方案:不要使用 ssh - 移至 https。要做到这一点:在您的项目文件夹中,您有一个 .git 文件夹 - 您有一个配置文件 - 在文本编辑器中打开它并更改该行

url [email protected]:yourname/yourproject.git

url [email protected]:yourname/yourproject.git

to

url = https://gitlab.com/yourname/yourproject.git

url = https://gitlab.com/yourname/yourproject.git

回答by rapaelec

if you are in Linux or macox , just try this in terminal:

如果您使用的是 Linux 或 macox ,请在终端中尝试:

ssh-add -l

if it return nothing, try this:

如果它什么都不返回,试试这个:

ssh-add

it must create identity in ~/.ssh/id_rsa

它必须在 ~/.ssh/id_rsa 中创建身份

after retry :

重试后:

ssh-add -l

it must return your identity, so after retry to clone, it's must work

它必须返回你的身份,所以在重试克隆后,它必须工作

NB: don't forget to add your ssh key in your profile gitlab

注意:不要忘记在您的个人资料 gitlab 中添加您的 ssh 密钥

thanks

谢谢

回答by skotperez

In my case, it wasn't a gitlab problem, but a sshd configuration one. The ssh server didn't allow connection except for a list of users. The user git, the one connecting remotely to gitlab, wasn't in that list. So, check this before anything else.

就我而言,这不是 gitlab 问题,而是 sshd 配置问题。除了用户列表外,ssh 服务器不允许连接。远程连接到 gitlab 的用户 git 不在该列表中。所以,在做任何其他事情之前先检查一下。

You can check your ssh server configuration in /etc/ssh/sshd_config. If you have a line with the option AllowUsers, add git to it:

您可以在/etc/ssh/sshd_config. 如果您有一行带有 option AllowUsers,请将 git 添加到其中:

AllowUsers user1 user2 user3 git

回答by xab

I have gitlab running with docker, this is what I did to fix my problem.

我有 gitlab 与 docker 一起运行,这是我为解决问题所做的工作。

Found that inside docker /var/log/gitlab/sshd/currentthere were multiple occurences of a message:

发现在docker /var/log/gitlab/sshd/current里面出现了多次一条消息:

Authentication refused: bad ownership or modes for file /var/opt/gitlab/.ssh/authorized_keys

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

After which I changed ownership of that file from 99:usersto git:userswith:

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

chown git:users authorized_keys

chown git:usersauthorized_keys

回答by Y. Joy Ch. Singha

Steps to be done, got same error but i fixed it. Gitlab wants ssh-rsa so below is the code to run ssh for rsa

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

  1. ssh-keygen -o -t rsa -b 4096 -C "[email protected]"
  1. ssh-keygen -o -t rsa -b 4096 -C "[email protected]"

[email protected] is your gitlab account email

[email protected] 是您的 gitlab 帐户电子邮件

  1. 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):

  2. It will prompt again you to enter so just hit Enter after the below code is prompt,

    Enter passphrase (empty for no passphrase):

  3. It will prompt again for the last you to enter so just hit Enter after the below code is prompt,

    Enter same passphrase again:

  4. You will show your ssh-rsa generate.

  5. 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.

  6. Look above the prompt asking you to enter, you will get the path of ssh-rsa.

  7. Go to your SSH folder and get the id_rsa.pub

  8. Open it and get the key and Copy Paste to the Gitlab and you are nearly to done.

  9. Check by: ssh -T [email protected]

  10. You will get: Welcome to GitLab, @joy4!

  11. Done.

  1. 它会提示你输入,所以在下面的代码提示后按 Enter 键,

    输入保存密钥的文件 (/home/yourDesktopName/.ssh/id_rsa):

  2. 它会再次提示您输入,因此在提示以下代码后按 Enter 键,

    输入密码(空表示没有密码):

  3. 它会再次提示您最后一次输入,因此在提示以下代码后按 Enter 键,

    再次输入相同的密码:

  4. 您将显示您的 ssh-rsa 生成。

  5. 登录到您的 Gitlab 帐户并转到右侧导航栏,您将获得设置,在左侧边栏中,您将获得 ssh 密钥。进入其中。

  6. 看上面提示你输入,你会得到ssh-rsa的路径。

  7. 转到您的 SSH 文件夹并获取 id_rsa.pub

  8. 打开它并获取密钥并复制粘贴到 Gitlab,您就快完成了。

  9. 检查方式: ssh -T [email protected]

  10. 你会得到: Welcome to GitLab, @joy4!

  11. 完毕。