1

I'm using Raspbian.

I did successfully added the key and I can clone a private repo with ssh auth manually, using the command git clone [email protected]:USER/repo.git.

Here is how I set it up:

ssh-keygen -t rsa -b 4096 -C “email”
eval "$(ssh-agent -s)"
ssh-add -k ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub

However, if I try to run git clone command through a bash script, I get Permission denied (publickey).

I'm running the script as "pi", with sudo. The script starts with #! /bin/bash and whoami (on the script) returns root, instead of "pi".

Could someone help me out, please?

Thanks!

2
  • Are you running the script manually as root? Commented Nov 26, 2018 at 21:19
  • @ScottBro, I think so, as I'm using "sudo ./script.sh" Commented Nov 26, 2018 at 21:34

1 Answer 1

2

SSH attemps to read the keys from $HOME/.ssh. Since you are running as root, the script most likely fails to find anything under /home/root/.ssh.

The fix? Try sudo -E, which preserves the environment variables. For more info, consult sudo(8).

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

2 Comments

Awesome! It worked. Is there a way to set the "-E" option inside the script, so user would just run the script with "sudo ./script.sh"? Thanks!
I have sudo -E as an alias for myself. It is not recommended to elevate privileges in a script. It violates the principle of "least surprise". If this solves your problem, please mark the answer as accepted ;-)

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.