在 sudo 调用的 Bash 脚本中识别用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3522341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:36:03  来源:igfitidea点击:

Identify user in a Bash script called by sudo

linuxbashsudo

提问by quadmore

If I create the script /root/bin/whoami.shcontaining:

如果我创建/root/bin/whoami.sh包含以下内容的脚本:

#!/bin/bash
whoami

and this script is called by a user with a properly configured sudo, it will indicate

并且此脚本由具有正确配置的 sudo 的用户调用,它将指示

root

Is there a fast way to obtain the actual user in a script, or will I have to resort to parameters passing along this username?

有没有一种快速的方法来获取脚本中的实际用户,还是我必须求助于传递这个用户名的参数?

回答by evan

$SUDO_USER doesn't work if you are using sudo su -.
It also requires multiple checks - if $USER == 'root'then get $SUDO_USER.

如果您使用的是 $SUDO_USER 不起作用sudo su -
它还需要多次检查 - 如果$USER == 'root'然后 get $SUDO_USER

Instead of the command whoamiuse who am i. This runs the whocommand filtered for the current session. It gives you more info than you need. So, do this to get just the user:

而不是命令whoami使用who am i。这将运行who为当前会话过滤的命令。它为您提供了比您需要的更多的信息。所以,这样做是为了得到用户:

who am i | awk '{print }'

Alternatively (and simpler) you can use logname. It does the same thing as the above statement.

或者(更简单)您可以使用logname. 它的作用与上述语句相同。

This gives you the username that logged in to the session.

这为您提供了登录会话的用户名。

These work regardless of sudoor sudo su [whatever]. It also works regardless of how many times suand sudoare called.

这些工作不管sudosudo su [whatever]。无论调用多少次su,它也能工作sudo

回答by Brandon Horsley

I think $SUDO_USERis valid.

我认为$SUDO_USER是有效的。

#!/bin/bash
echo $SUDO_USER
whoami

回答by Alexei Tenitski

Here is how to get the username of the person who called the script no matter if sudo or not:

无论是否使用sudo,以下是获取调用脚本的人的用户名的方法:

if [ $SUDO_USER ]; then user=$SUDO_USER; else user=`whoami`; fi

or a shorter version

或更短的版本

[ $SUDO_USER ] && user=$SUDO_USER || user=`whoami`

回答by Srinivas

who am i | awk '{print $1}'didn't work for me but who|awk '{print $1}'will serve the job

who am i | awk '{print $1}'不适合我,但 who|awk '{print $1}'会为这份工作服务

回答by Rob

Using whoami, who am i, who, idor $SUDO_USERisn't right here.

使用whoami, who am i, who, idor$SUDO_USER不在这里。

Actually, whois never a solution to the question, as it will only list the logged in users, which might be dozens...

实际上,who从来都不是问题的解决方案,因为它只会列出已登录的用户,可能有几十个......

In my eyes, the only valuable answer is the use of logname.

在我看来,唯一有价值的答案是使用logname.

Hope this helps

希望这可以帮助

Rob

回答by msw

Odd, the system does distinguish between real and effective UIDs, but I can find no program that exports this at shell level.

奇怪的是,系统确实区分了真实和有效的 UIDs,但我找不到在 shell 级别导出它的程序。

回答by Jim Hunziker

If it's the UID you're looking for (useful for docker shenanigans), then this works:

如果它是您正在寻找的 UID(对 docker 恶作剧有用),那么这有效:

LOCAL_USER_ID=$(id -u $(logname))