如何从使用 sudo 运行的程序访问用户的 ssh 密钥?

发布于 2024-10-10 02:50:11 字数 311 浏览 0 评论 0原文

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

  1. 通过 git+ssh:// 协议(使用 Git)获取“包”。
  2. 在系统中安装该“包”。

为了让 git+ssh 工作,Git 需要查看我的密钥。

对于“包”安装,程序必须具有超级用户权限。

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

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

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

  1. Fetch a "package" over git+ssh:// protocol (using Git).
  2. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上的星空 2024-10-17 02:50:11

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

如果您不能这样做,那么您应该在执行 git pull 命令时放弃权限。

sudo -u <original_user> git fetch git+ssh:// 

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文