1

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.

4
  • 3
    When you run git with sudo, ssh is not looking in ~josh/.ssh for the key any more; it's looking in ~root/.ssh. Commented Sep 13, 2022 at 21:41
  • 2
    The simplest solution will be to clone to a directory you have access to, then use sudo to move it to the correct location. Commented Sep 13, 2022 at 21:48
  • Can you use sudo to run as ec2-user instead? You can create a dedicated key for that user and allow it to access the private repository using that key. Commented Sep 13, 2022 at 21:50
  • @chepner the end goal is to run this command using Ansible, where I already tried running it as ec2-user there with no luck. Your initial comment about ssh looking in ~root/.ssh was very helpful and allowed me to find what I think is the best solution for my problem Commented Sep 13, 2022 at 22:01

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.