1

I have the following problem. I want to clone a repo from our GitLab instance, however, I get the following error: git@gitlab.com: Permission denied (publickey). fatal: Could not read from remote repository.

I have already spent hours searching the internet, but none of the solutions have worked for me.

More Information: Win10, WSL2

How I have proceeded so far:

ssh-keygen -t ed25519 -C "your_email@example.com"
exec ssh-agent bash
ssh-add ~/.ssh/id_ed25519

Source (https://docs.github.com/de/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent?platform=linux)

Then Add the publickey to GitLab.

Nevertheless, I won't work.

Output of

ssh -Tv git@gitlab.com
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /home/<user>/.ssh/config
debug1: /home/<user>/.ssh/config line 1: Applying options for gitlab.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to gitlab.com [<ip-adress>] port 22.
debug1: Connection established.
debug1: identity file /home/<user>/.ssh/id_rsa.pub type -1
debug1: identity file /home/<user>/.ssh/id_rsa.pub-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug1: Authenticating to gitlab.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:ATGbzTPAlJZOY5gURztKPIuzAC/eN22PApBTfUW8oQA
debug1: Host 'gitlab.com' is known and matches the ECDSA host key.
debug1: Found key in /home/<user>/.ssh/known_hosts:13
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/<user>/.ssh/id_rsa.pub  explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/<user>/.ssh/id_rsa.pub
no such identity: /home/<user>/.ssh/id_rsa.pub: No such file or directory
debug1: No more authentication methods to try.
git@gitlab.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

In the process of me trying to fix this problem I've written a config file which looks like this:

Host <myhost>
    HostName <myhostname>
    User <username>
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519.pub
    PubkeyAcceptedKeyTypes=+ssh-rsa
    TCPKeepAlive Yes

Please be aware, that I have to censor some outputs.

If you have any ideas, which could help me, I would be very happy to hear them.

Edit: With the Steps provived in the Reply, I can clone now using WSL but not with Gitbash.

Erik
  • 11
  • 4
  • In `ssh-add ~/.ssh/id_ed25519` you add a private part of the keypair. To Gitlab you added the public part, `~/.ssh/id_ed25519.pub`, right? Then please [edit] the question and show the text output of `ssh -Tv git@gitlab.com` – phd Dec 14 '22 at 09:55
  • "Trying private key: /home//.ssh/id_rsa.pub" which doesn't exist. Seems `ssh` doesn't contact agent. Did you run these commands (`git` and `ssh -Tv`) in the same terminal where you activated agent (`exec ssh-agent bash`)? – phd Dec 14 '22 at 10:12
  • Yes, i did all the commands in the same terminal – Erik Dec 14 '22 at 10:17
  • "debug1: Will attempt key: /home//.ssh/id_rsa.pub **explicit**" Is it configured in `~/.ssh/config`? Can we see the file? – phd Dec 14 '22 at 10:24
  • I edited my question. You can see the file now – Erik Dec 14 '22 at 10:31
  • `` in `Host ` is `gitlab.com`. `User ` must be `User git`. And the section could be overriden (partially or entirely) with `Host *` if it is in the config. – phd Dec 14 '22 at 10:35
  • yes, i have it that way. My git config is just username and email – Erik Dec 14 '22 at 10:44
  • Add `IdentitiesOnly yes` to lock using exactly the key. See https://stackoverflow.com/search?q=%5Bssh%5D+IdentitiesOnly – phd Dec 14 '22 at 10:56
  • 1
    When using the agent, you may have to list the private key file (`~/.ssh/id_rsa` rather than `~/.ssh/id_rsa.pub`). I've seen this on at least some macOS systems anyway. In any case you've demonstrated at this point that the problem is in ssh, not Git; until you can get ssh working you can just concentrate on that part. – torek Dec 14 '22 at 10:58
  • @phd added this to my config, but same result – Erik Dec 14 '22 at 11:05

1 Answers1

1

The config file should be:

Host mygitlab                  <===
    HostName gitlab.com        <===
    User git                   <===
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519.pub
    PubkeyAcceptedKeyTypes=+ssh-rsa
    TCPKeepAlive Yes

And the test should then be:

ssh -Tv mygitlab

Or:

git clone mygitlab:<me>/<myrepo>
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi VonC, just for clarification: "User git" means that I have to put my git user name here or literally type "User git" in the Config? Other than that, the Issue still remains – Erik Dec 14 '22 at 12:31
  • @Erik When you connect to GitLab or GitHub, it is *always* with the service account 'git', never with your name. The remote hosting service will authenticate you through your key, associated to your account. – VonC Dec 14 '22 at 12:41
  • Thank you! I didn't know that. Still, it wont work... – Erik Dec 14 '22 at 12:50
  • @Erik Try restarting the ssh service with ```sudo service ssh restart``` – Kulshreshth K Dec 14 '22 at 13:06
  • Did restart. Same error :/ – Erik Dec 14 '22 at 13:36
  • `IdentityFile ~/.ssh/id_ed25519.pub` Don't use the ".pub" file here. It's the wrong file. See https://stackoverflow.com/a/29948797/13317 – Kenster Dec 14 '22 at 14:10
  • Hi @Kenster thank you for your input. I've updated my config file, but the Problem still persists.. – Erik Dec 14 '22 at 14:39