gpt4 book ai didi

c - 如果程序使用 sudo 运行,我如何获得用户的真实 uid?

转载 作者:太空狗 更新时间:2023-10-29 16:33:49 25 4
gpt4 key购买 nike

我正在运行的程序需要 root 权限,因此使用 sudo 运行,但它还需要知道运行它的用户。 getuidgeteuid 都返回 root。如何获取实际用户的用户名(或 uid)?

谢谢!

最佳答案

sudo 提供了一些环境变量来帮助您处理这种情况:

   SUDO_UID        Set to the user ID of the user who invoked
sudo

SUDO_USER Set to the login of the user who invoked sudo

steveayre 在评论中指出,在某些情况下用户可以设置这些环境变量; sudo(8) 联机帮助页部分包含:

The sudoers policy subjects variables
passed on the command line to the same restrictions as normal
environment variables with one important exception. If the
setenv option is set in sudoers, the command to be run has the
SETENV tag set or the command matched is ALL, the user may set
variables that would otherwise be forbidden. See sudoers(5)
for more information.

因此,当您需要依赖此功能时,请确保不要向用户授予 ALL 命令。

关于c - 如果程序使用 sudo 运行,我如何获得用户的真实 uid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10272784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com