Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Nov 30 '12 at 9:08

This question came from our site for professional and enthusiast programmers.

4 Answers 4

up vote 2 down vote accepted

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.

share|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). –  AlexKey Nov 30 '12 at 9:55

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

share|improve this answer

Take a look at: http://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
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.