Friday, October 7, 2022

[SOLVED] Clone private git repo to remote host using sudo

Issue

I would like to run the following command:

ssh -A josh@remote_ip sudo git clone [email protected]:private/private-repo.git /home/ec2-user/private-repo 

but I get an error:

Cloning into '/home/ec2-user/private-repo'...
Permission denied (publickey).
fatal: Could not read from remote repository.

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

The command works when I want to clone into a directory I own (~/private-repo) without sudo. The command doesn't work if I include sudo into a directory I already own.

Since I need to use sudo to write into the other user's directory, how can I get this command to work? Logging in as ec2-user is not a possible solution.


Solution

Thank you to @chepner for pointing me in the direction of the issue: my ssh key does not get passed to ~root/.ssh when I use sudo.

To fix this, I used visudo to add this line to my /etc/sudoers file:

Defaults    env_keep += "SSH_AUTH_SOCK"

Source



Answered By - Josh K
Answer Checked By - Marie Seifert (WPSolving Admin)