I can't clone a repository via SSH Permission denied

,

Hi!

As you can see from the title, I am not able to clone a repository via SSH with command:
git clone git@192.168.0.250:agata_implantaciones/test

Obtaining this result:

I have created an SSH key on the client machine and added the public key in the SSH Keys settings but for some reason it keeps telling me that I don’t have permissions.

I leave you the result of this command:
GIT_SSH_COMMAND='ssh -vvvT' git clone git@192.168.0.250:agata_implantaciones/test

Cloning into 'test'...
OpenSSH_8.2p1 Ubuntu-4ubuntu0.1, OpenSSL 1.1.1f  31 Mar 2020
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: /etc/ssh/ssh_config line 54: Applying options for 192.168.0.250
debug2: resolve_canonicalize: hostname 192.168.0.250 is address
debug2: ssh_connect_direct
debug1: Connecting to 192.168.0.250 [192.168.0.250] port 22.
debug1: Connection established.
debug1: identity file /home/sacarino/.ssh/id_rsa type 0
debug1: identity file /home/sacarino/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.7p1 Debian-5+deb8u3
debug1: match: OpenSSH_6.7p1 Debian-5+deb8u3 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 192.168.0.250:22 as 'git'
debug3: hostkeys_foreach: reading file "/home/sacarino/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/sacarino/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from 192.168.0.250
debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,ext-info-c
debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,sk-ssh-ed25519@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,zlib
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
debug2: host key algorithms: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
debug2: ciphers stoc: aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com
debug2: compression stoc: none,zlib@openssh.com
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug1: kex: algorithm: curve25519-sha256@libssh.org
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
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:C20yV5k96n5tDy7uTDSKBSSNrlpWGK2WER6rdRsgEOM
debug3: hostkeys_foreach: reading file "/home/sacarino/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/sacarino/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from 192.168.0.250
debug1: Host '192.168.0.250' is known and matches the ECDSA host key.
debug1: Found key in /home/sacarino/.ssh/known_hosts:1
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/sacarino/.ssh/id_rsa RSA SHA256:3+XxTDZWeAVzW2L0ANg/RGVRb2oGVQSsbkU7GZ9zEvQ
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred publickey
debug3: authmethod_lookup publickey
debug3: remaining preferred:
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/sacarino/.ssh/id_rsa RSA SHA256:3+XxTDZWeAVzW2L0ANg/RGVRb2oGVQSsbkU7GZ9zEvQ
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
git@192.168.0.250: Permission denied (publickey,password).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I hope you can help me, thank you very much in advance!

Heya and welcome to Gitlab community.

Did you made sure that your keys have proper permissions set ?

chmod 700 ~/.ssh
chmod 600 ~/.ssh/your_private_key

Hey, thank!

The permissions are correct as you indicate and I still cannot find a solution

Can you check if you have authorized_keys.lock within ~/.ssh ? If you do make sure you delete it try running you original git clone command.

If the issue is still there can you try running git clone https://192.168.0.250/agata_implantaciones/test and let me know what you get?

I don’t have authorized_keys.lock inside ~/.ssh and cloning via https has worked perfectly

That should work for you .

In case it keeps happening for future repos or you want to try and fix this issue properly you should make sure that your ssh config was set correctly in ~/.ssh/config (if you have the config) and it’s pointing the path to the working set of keys.

Alternatively add the working ssh file using ssh-add .

run: ssh-add <PATH-TO-SSH-FILE>

example: ssh-add ~/.ssh/gitlab_rsa

Some of the info i mentioned here I found from gitlab source regarding ssh keys.

1 Like

Yes, but I need to perform the operations through ssh, I have configured the ssh in the following way:

Host 192.168.0.250
Preferredauthentications publickey
IdentityFile ~/.ssh/id_rsa

But the connection the publickey still doesn’t work and I don’t understand why.

Just to double check, you did upload the keys to your gitlab instance ?

I don’t understand you, could you be more specific and explain how to do that?

Check this part of gitlab docks to see how to do it.

I followed this guide to do it, create the key in my local and copy the public key adding it in SSH Keys

I would recommend that you create another key and upload it to gitlab, then clone it again.

I have already done it several times and the same thing happens

I have new news, I have put the ssh server log in debug and observed this in the /var/log/auth.log:

sshd[19421]: debug1: trying public key file /root/.ssh/authorized_keys
sshd[19421]: debug1: Could not open authorized keys '/root/.ssh/authorized_keys': Permission denied

For some reason the wrong directory is consulted, right?

Have you checked if there are keys on that path? If there are , try adding pointing them for use by using
ssh-add ~/root.ssh/id_rsa (or however you named your key).

I do not understand what you mean, I have key in my local and put the public key in my user’s configuration in GitLab.

In /var/opt/gitlab/.ssh/authorized_keys is where the keys are being registered but the ssh server consults /root/.ssh/authorized_keys.

image

I have forced the ssh server to query this file and now it works for me, but I don’t think it is the correct method

Hmm , i believe it should be using the key from id_rsa.pub and not from authorized keys. However if it works for you without issues, i wouldn’t worry about it.

@kevin.espineira I think your sshd still runs on port 22. You should set up Gitlab to expose Gitlab Shell on a different port. If you instead want Gitlab Shell to be on port 22 (e.g., you like the nicer URLs without explicit port numbers), you have to move sshd to a different port.