How to access user's ssh keys from the program that is run with sudo?
The 开发者_JAVA百科dilemma: a Linux program that I'm working on should:
- Fetch a "package" over git+ssh:// protocol (using Git).
- Install that "package" in the system.
For git+ssh to work, Git needs to see my keys.
For the "package" installation, the program must have superuser privileges.
A limitation: the program should not elevate privileges (call sudo) by itself. User must explicitly invoke it with sudo. (Lets ignore the case when user runs the program while begin logged in as root — assuming he will setup keys correctly then.)
So, the question is: how to do ssh access with user keys from the program that is invoked with sudo?
use the -i identity_file ssh parameters. To tell git to use the proper command you should set the GIT_SSH variable to a file that will call ssh -i "$@".
If you can't do that then you should drop privileges while you do the git pull command.
sudo -u <original_user> git fetch git+ssh://
You can use the ${SUDO_USER} enviroment variable to find out who the original user was.
精彩评论