如何从使用 sudo 运行的程序访问用户的 ssh 密钥? [英] How to access user's ssh keys from the program that is run with sudo?

查看:39
本文介绍了如何从使用 sudo 运行的程序访问用户的 ssh 密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

困境:我正在开发的 Linux 程序应该:

The dilemma: a Linux program that I'm working on should:

  1. 通过 git+ssh://协议获取包"(使用 Git).
  2. 在系统中安装那个包".

要使用 git+ssh,Git 需要查看我的密钥.

For git+ssh to work, Git needs to see my keys.

对于包"安装,程序必须具有超级用户权限.

For the "package" installation, the program must have superuser privileges.

一个限制:程序不应自行提升权限(调用 sudo).用户必须使用 sudo 显式调用它.(让我们忽略用户在开始以 root 身份登录时运行程序的情况——假设他随后会正确设置密钥.)

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.)

那么,问题是:如何使用 sudo 调用的程序中的用户密钥进行 ssh 访问?

So, the question is: how to do ssh access with user keys from the program that is invoked with sudo?

推荐答案

使用 -i identity_file ssh 参数.要告诉 git 使用正确的命令,您应该将 GIT_SSH 变量设置为将调用 ssh -i "$@" 的文件.

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 "$@".

如果你不能这样做,那么你应该在执行 git pull 命令时放弃特权.

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:// 

您可以使用 ${SUDO_USER} 环境变量来找出原始用户是谁.

You can use the ${SUDO_USER} enviroment variable to find out who the original user was.

这篇关于如何从使用 sudo 运行的程序访问用户的 ssh 密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆