I'm attempting to clone a git repo using SSH protocol like so:

sudo git clone ryan@example.com:/var/www/repo

But I'm getting the "Permission denied (publickey)" error. However, I already placed my public key in the authorized_keys file, and it works fine when I SSH into the same server using the same user like so:

ssh ryan@example.com

It also works fine if I clone the same repository to a different machine. The only difference between the way I set up the two machines is this: On the machine that doesn't work, I copied and pasted the public key into authorized_keys manually. On the machine that does work, I set it up using ssh-copy-id. Is git using some different authentication method than SSH, even though I'm using the SSH protocol? Why can I connect using SSH but can't use git via SSH?


As suggested below, the issue was due to using sudo when cloning. This will then authenticate using the root user, and since I have disabled SSH access for root, it was blocked. I solved this by adding myself to the www-data group which owns the local directory, and then giving write permissions for the group.

  • Hello, Is your git server a self-hosted one? Are you using github, gitlab, bitbucke t or another provider? – Leo Sep 23 '18 at 5:26
  • It's a hosted Linode server. I'm not using anything but vanilla git. – RyanPalmer Sep 24 '18 at 17:41

When running commands with sudo they run as the target user (root in this case since that is the default user and you didn’t select a specific one with a -u username option) .

That means rather than your ssh keys, those belonging to root are used. Either configure git for the root user to use the key pair from your account or (generate and) use the root ssh key pair to get authorization to your git repo


As far as I can tell, your user ryan should have permissions over the /var/www/repo directory, which, by default shouldn't have.

So, you should probably give your user permissions over the directory, or make the user part of the group that has access to it.

ps. In your second example you are connecting to your user's home.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.