我运行的程序需要root权限,因此运行sudo,但它也需要知道用户正在运行它.getuid和geteuid都返回root.如何获取实际用户的用户名(或uid)?
谢谢!
sar*_*old 18
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
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
因此,ALL当您需要依赖此功能时,请确保不向用户授予命令.
| 归档时间: |
|
| 查看次数: |
7358 次 |
| 最近记录: |