本文由我司收集整编,推荐下载,如有疑问,请与我司联系
如果程序是用
sudo
运行的,如何让用户获得真正的
uid
?
如果程序是用
sudo
运行的,如何让用户获得真正的
uid
?
[
英
]How
do
I
get
the
users
real
uid
if
the
program
is
run
with
sudo?
The
program
I
am
running
needs
root
privledges and therefore is run with sudo, but it also needs to know what user is running it.
getuid and geteuid both return root. How do I get the actual users username (or uid)?
我运行的程序需要
root
权限,因此使用
sudo
运行,但它还需要知道用户正在运行
它。
getuid
和
geteuid
都返回
root
。如何获取实际用户的用户名(或
uid
)?
Thanks!
谢谢!
16
sudo provides some environment variables to help you with exactly this case:
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 has pointed out in the comments that the user
can set these environment variables in some cases; the sudo(8) manpage includes in part:
steveayre
在评论中指出用户可以在某些情况下设置这些环境变量
;
sudo
(
8
)联机
帮助页包括:
The
sudoers
policy
subjects
variablespassed
on
the
command
line
to
the
same
restrictions
as
normalenvironment
variables
with
one
important
exception.
If
thesetenv
option is set in sudoers, the command to be run has theSETENV tag set or the command
matched
is
ALL,
the
user
may
setvariables
that
would
otherwise
be
forbidden.
See
sudoers(5)for more information. So be sure that you don’t grant ALL commands to users
when you need to rely upon this feature.
因此,当您需要依赖此功能时,请确保不向用户授予所有命令。
3
A Linux-specific audit_getloginuid() function provided by the auditing system may be