Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sudo git as another user with ssh-agent for key access

Tags:

git

sudo

ubuntu

i have ssh-agent setup for the user www-data and the password saved for the private key

from root i need to run "sudo -u www-data git pull" so it doesn't ask for the git private key password. sudo doesn't run the .bashrc script so not sure how to pull this off?

any suggestions?

like image 711
jadent Avatar asked Feb 06 '12 01:02

jadent


1 Answers

I do this, and do an ssh-add as part of my .bash_profile:

sudo -u otherusername ssh-agent bash -l

Unfortunately, I don't seem to have any history when I use the up-arrow when doing this. I do, however, when I run the same command minus the sudo part.

By the way, this is what I have in my bash profile to auto-start ssh-agent (only once) and add an RSA key (only once):

# Start ssh-agent & add key
if [[ -z $SSH_AGENT_PID ]]; 
then 
    echo Starting ssh-agent automatically...
    ssh-agent bash -l
elif [[ 0 == `ssh-add -l | grep "(RSA)" -c` ]];
then
    echo Adding ssh key automatically...
    # you might have your key in a different location:
    ssh-add ~/.ssh/idents/id_rsa
fi
like image 124
kghastie Avatar answered Oct 01 '22 20:10

kghastie