I'm trying to run the following comamnd as user flc:

sudo hg clone git+ssh://git@github.com:flc/myproject.git /opt/flc/projects/myproject

However, I'm getting this error:

Permission denied (publickey).

I know I have the keys in /home/flc/.ssh and it works if I run the command below (note without sudo, but writing to /tmp so don't need sudo)

hg clone git+ssh://git@github.com:flc/myproject.git /tmp/flc/projects/myproject

The thing is I need sudo so it can write to /opt/flc/projects/myproject

So my question is how can I use sudo and still get it to find the .ssh keys in /home/flc/.ssh and hence work?


You need to have key deployed for root, not for your user. When you do sudo the command executed under root user and not your.

Also you perhaps should reconsider how you do it. IMO this is not correct way to pull as sudo.

| improve this answer | |
  • If you're going to categorically state that something is not correct, you'd do well to point out what you consider to be correct. – ThatGraemeGuy Nov 30 '12 at 9:28
  • @GraemeDonaldson sure i would do that when i see next time question "what is correct way to clone repository?". But here i just pointed out that this is my opinion, but did not to go off-topic i allow others find it themselves (or post another question which can be answered by professionals). – Alexey Kamenskiy Nov 30 '12 at 9:55

If you use sudo, it means the super user (i.e. root) will launch the command.

So if it's required for you tu use the super user (which is not really recommended), you need to either recreate some keys for the super user : sudo ssh-keygen -t rsa would do the trick I think.

Or, can may just need to copy the /home/flc/.ssh in the root folder :

sudo cp -R /home/flc/.ssh /root/

But I really think you should keep in mind that using super user is not a good solution in general.

| improve this answer | |

Take a look at: https://unix.stackexchange.com/questions/33926/passing-ssh-options-to-git-clone

  • init empty repository and edit .git/config
  • define key location for that host in /root/.ssh/config
| improve this answer | |

You can use directory permissions and add the user used by hg in group with write permission in /opt/flc/projects/myproject.

| improve this answer | |

Your Answer

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