简体   繁体   English

无法使用 ssh 密钥克隆 Gitlab 私有存储库。 密钥验证不起作用

[英]Can't clone Gitlab private repo using ssh keys. Key authentication doesn't work

I have default key pair under my.ssh and added id_rsa.pub in https://gitlab.com/profile/keys but still can't access gitlab using this keys.我在 my.ssh 下有默认密钥对,并在https://gitlab.com/profile/keys中添加了 id_rsa.pub 但仍然无法使用此密钥访问 ZD0498FAC9D755142B3DFD9F0528170。

ssh -vT git@gitlab.com ssh -vT git@gitlab.com

can't login:无法登录:

$ ssh -vT git@gitlab.com
OpenSSH_7.9p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to gitlab.com [35.231.145.151] port 22.
debug1: Connection established.
debug1: identity file /Users/denysobukhov/.ssh/id_rsa type 0
debug1: identity file /Users/denysobukhov/.ssh/id_rsa-cert type -1
debug1: identity file /Users/denysobukhov/.ssh/id_dsa type -1
debug1: identity file /Users/denysobukhov/.ssh/id_dsa-cert type -1
debug1: identity file /Users/denysobukhov/.ssh/id_ecdsa type -1
debug1: identity file /Users/denysobukhov/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/denysobukhov/.ssh/id_ed25519 type -1
debug1: identity file /Users/denysobukhov/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/denysobukhov/.ssh/id_xmss type -1
debug1: identity file /Users/denysobukhov/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.9
debug1: Remote protocol version 2.0, remote software version ROSSSH
debug1: no match: ROSSSH
debug1: Authenticating to gitlab.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: diffie-hellman-group-exchange-sha256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha1 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha1 compression: none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(2048<7680<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:pZaBIOeFjqundQMWm8JAamGIUe63cxi7G4ZTpGM5C+Y
debug1: Host 'gitlab.com' is known and matches the RSA host key.
debug1: Found key in /Users/denysobukhov/.ssh/known_hosts:19
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: Will attempt key: /Users/denysobukhov/.ssh/id_rsa RSA SHA256:x3OEhLg/QPPUvr1f4IPo0+uHv8q73Da1TGjUnzD8FdQ
debug1: Will attempt key: /Users/denysobukhov/.ssh/id_dsa
debug1: Will attempt key: /Users/denysobukhov/.ssh/id_ecdsa
debug1: Will attempt key: /Users/denysobukhov/.ssh/id_ed25519
debug1: Will attempt key: /Users/denysobukhov/.ssh/id_xmss
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/denysobukhov/.ssh/id_rsa RSA SHA256:x3OEhLg/QPPUvr1f4IPo0+uHv8q73Da1TGjUnzD8FdQ
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /Users/denysobukhov/.ssh/id_dsa
debug1: Trying private key: /Users/denysobukhov/.ssh/id_ecdsa
debug1: Trying private key: /Users/denysobukhov/.ssh/id_ed25519
debug1: Trying private key: /Users/denysobukhov/.ssh/id_xmss
debug1: Next authentication method: password
git@gitlab.com's password:
debug1: Authentications that can continue: password
debug1: Next authentication method: password
Permission denied, please try again.
git@gitlab.com's password:

can't clone无法克隆

git clone git@gitlab.com:denysobukhov/myprj git 克隆 git@gitlab.com:denysobukhov/myprj

$ git clone git@gitlab.com:denysobukhov/myprj
Cloning into 'myprj'...
git@gitlab.com's password:

my config我的配置

cat /etc/ssh/ssh_config猫 /etc/ssh/ssh_config

$ cat /etc/ssh/ssh_config
Host *
    SendEnv LANG LC_*

adding to empty ~/.ssh/config the following didn't help:添加到空 ~/.ssh/config 以下没有帮助:

Host gitlab.com
    Hostname gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa

Why doesn't my key work?为什么我的钥匙不起作用?

Finally found the solution: generated a new ED25519 SSH key pair终于找到解决办法:生成新的ED25519 SSH密钥对

ssh-keygen -t ed25519 -C "email@example.com"

and added to ~/.ssh/config the following:并将以下内容添加到 ~/.ssh/config 中:

Host gitlab.com
  Hostname altssh.gitlab.com
  User git
  Port 443
  PreferredAuthentications publickey

Also verified that ~/.ssh/config modification is decisive.还验证了 ~/.ssh/config 修改是决定性的。

I had a similar problem (found this via Google) when trying to push/pull from private GitLab repos.尝试从私有 GitLab 存储库推/拉时,我遇到了类似的问题(通过 Google 发现)。

The git repos had been cloned using HTTPS, eg https://gitlab.com/user/repo.git , before ever setting up any SSH keys. The git repos had been cloned using HTTPS, eg https://gitlab.com/user/repo.git , before ever setting up any SSH keys.

In order for SSH authentication to be used for pulling/pushing, even after setting up the SSH key in the GitLab settings, the remote URL has to be changed to the SSH version git@gitlab.com:user/repo.git . In order for SSH authentication to be used for pulling/pushing, even after setting up the SSH key in the GitLab settings, the remote URL has to be changed to the SSH version git@gitlab.com:user/repo.git .

git remote set-url origin git@gitlab.com:user/repo.git

(Assumes your remote is called origin.) (假设您的遥控器称为原点。)

This may only be necessary but not sufficient though since I tried a bunch of other things without resetting the changes when they didn't work by themselves.这可能只是必要的,但还不够,因为我尝试了很多其他的事情,而没有在它们自己不起作用时重置更改。

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

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