Ubuntu – Need to use sudo with ssh

sshsudoUbuntu

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?

Best Answer

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.