How do I get the name of the active user via the command line in OS X?
如何通过OS X中的命令行获取活动用户的名称?
11 个解决方案
#1
as 'whoami' has been obsoleted, it's probably more forward compatible to use:
由于'whoami'已经过时,它可能更适合使用:
id -un
#2
If you'd like to display the full name (instead of the username), add the -F
flag:
如果您想显示全名(而不是用户名),请添加-F标志:
$ id -F
Andrew Havens
#3
Via here
Checking the owner of /dev/console seems to work well.
检查/ dev / console的所有者似乎运行良好。
stat -f "%Su" /dev/console
stat -f“%Su”/ dev / console
#4
EDIT
The whoami utility has been obsoleted by the id(1) utility, and is equivalent to id -un
. The command id -p
is suggested for normal interactive use.
whoami实用程序已被id(1)实用程序废弃,相当于id -un。建议将命令id -p用于正常的交互式使用。
#5
I'm pretty sure the terminal in OS X is just like unix, so the command would be:
我很确定OS X中的终端就像unix一样,所以命令是:
whoami
I don't have a mac on me at the moment so someone correct me if I'm wrong.
我此刻没有麦克风,所以如果我错了,有人会纠正我。
#6
If you want to know who's currently logged in to the system:
如果您想知道谁当前登录到系统:
$ w 15:56:14 up 5 days, 20:58, 6 users, load average: 0.43, 0.53, 0.50 USER TTY LOGIN@ IDLE JCPU PCPU WHAT me pts/2 Fri19 1:03m 0.98s 0.98s -/bin/bash me pts/3 09:55 6:00m 0.43s 0.43s /bin/bash me pts/5 15:56 0.00s 0.23s 0.00s w
(This is from a Linux system; the formatting on OS X may be slightly different, but the information should be about the same.)
(这是来自Linux系统; OS X上的格式可能略有不同,但信息应该大致相同。)
There may be multiple login sessions; UNIX is designed to be a multi-user system, after all.
可能有多个登录会话;毕竟,UNIX被设计为一个多用户系统。
#7
The question has not been completely answered, IMHO. I will try to explain: I have a crontab entry that schedules a bash shell command procedure, that in turn does some cleanup of my files; and, when done, sends a notification to me using the OS X notification center (with the command osascript -e 'display notification ...
). If someone (e.g. my wife or my daughter) switches the current user of the computer to her, leaving me in the background, the cron script fails when sending the notification.
恕我直言,问题尚未得到完全解答。我将尝试解释:我有一个crontab条目,它安排了一个bash shell命令过程,这反过来会对我的文件进行一些清理;完成后,使用OS X通知中心向我发送通知(使用命令osascript -e'显示通知...)。如果有人(例如我的妻子或我的女儿)将计算机的当前用户切换到她,让我留在后台,则cron脚本在发送通知时会失败。
So, Who is the current user means Has some other people become the effective user leaving me in the background? Do stat -f "%Su" /dev/console
returns the current active user name?
那么,谁是当前的用户意味着是否有其他人成为让我留在后台的有效用户? stat -f“%Su”/ dev / console是否返回当前活动用户名?
The answer is yes; so, now my crontab shell script has been modified in the following way:
答案是肯定的;所以,现在我的crontab shell脚本已经按以下方式修改:
...
if [ "$(/usr/bin/stat -f ""%Su"" /dev/console)" = "loreti" ]
then /usr/bin/osascript -e \
'display notification "Cleanup done" sound name "sosumi" with title "myCleanup"'
fi
#8
Define 'active user'.
定义“活跃用户”。
If the question is 'who is the logged in user', then 'who am i' or 'whoami' is fine (though they give different answers - 'whoami' reports just a user name; 'who am i' reports on terminal and login time too).
如果问题是“谁是登录用户”,那么'我是谁'或'whoami'是好的(虽然他们给出了不同的答案 - 'whoami'仅报告用户名;'我是谁'报告终端和登录时间也是如此)。
If the question is 'which user ID is the effective ID for the shell', then it is often better to use 'id'. This reports on the real and effective user ID and group ID, and on the supplementary group IDs too. This might matter if the shell is running SUID or SGID.
如果问题是'哪个用户ID是shell的有效ID',那么通常最好使用'id'。它会报告真实有效的用户ID和组ID,以及补充组ID。如果shell运行SUID或SGID,这可能很重要。
#9
#10
You can also use the logname
command from the BSD General Commands Manual under Linux or MacOS to see the username of the user currently logged in, even if the user is performing a sudo
operation. This is useful, for instance, when modifying a user's crontab while installing a system-wide package with sudo: crontab -u $(logname)
您还可以使用Linux或MacOS下的BSD General Commands Manual中的logname命令查看当前登录用户的用户名,即使用户正在执行sudo操作也是如此。这很有用,例如,在使用sudo安装系统范围的软件包时修改用户的crontab:crontab -u $(logname)
Per man logname
:
每个人的日志名称:
LOGNAME(1)
NAME
logname -- display user's login name
#11
You can also retrieve it from the environment variables, but that is probably not secure, so I would go with Andrew's answer.
你也可以从环境变量中检索它,但这可能不安全,所以我会选择Andrew的答案。
printenv USER
If you need to retrieve it from an app, like Node, it's easier to get it from the environment variables, such as
如果您需要从应用程序(如Node)中检索它,则可以更轻松地从环境变量中获取它,例如
process.env.USER
.
#1
as 'whoami' has been obsoleted, it's probably more forward compatible to use:
由于'whoami'已经过时,它可能更适合使用:
id -un
#2
If you'd like to display the full name (instead of the username), add the -F
flag:
如果您想显示全名(而不是用户名),请添加-F标志:
$ id -F
Andrew Havens
#3
Via here
Checking the owner of /dev/console seems to work well.
检查/ dev / console的所有者似乎运行良好。
stat -f "%Su" /dev/console
stat -f“%Su”/ dev / console
#4
EDIT
The whoami utility has been obsoleted by the id(1) utility, and is equivalent to id -un
. The command id -p
is suggested for normal interactive use.
whoami实用程序已被id(1)实用程序废弃,相当于id -un。建议将命令id -p用于正常的交互式使用。
#5
I'm pretty sure the terminal in OS X is just like unix, so the command would be:
我很确定OS X中的终端就像unix一样,所以命令是:
whoami
I don't have a mac on me at the moment so someone correct me if I'm wrong.
我此刻没有麦克风,所以如果我错了,有人会纠正我。
#6
If you want to know who's currently logged in to the system:
如果您想知道谁当前登录到系统:
$ w 15:56:14 up 5 days, 20:58, 6 users, load average: 0.43, 0.53, 0.50 USER TTY LOGIN@ IDLE JCPU PCPU WHAT me pts/2 Fri19 1:03m 0.98s 0.98s -/bin/bash me pts/3 09:55 6:00m 0.43s 0.43s /bin/bash me pts/5 15:56 0.00s 0.23s 0.00s w
(This is from a Linux system; the formatting on OS X may be slightly different, but the information should be about the same.)
(这是来自Linux系统; OS X上的格式可能略有不同,但信息应该大致相同。)
There may be multiple login sessions; UNIX is designed to be a multi-user system, after all.
可能有多个登录会话;毕竟,UNIX被设计为一个多用户系统。
#7
The question has not been completely answered, IMHO. I will try to explain: I have a crontab entry that schedules a bash shell command procedure, that in turn does some cleanup of my files; and, when done, sends a notification to me using the OS X notification center (with the command osascript -e 'display notification ...
). If someone (e.g. my wife or my daughter) switches the current user of the computer to her, leaving me in the background, the cron script fails when sending the notification.
恕我直言,问题尚未得到完全解答。我将尝试解释:我有一个crontab条目,它安排了一个bash shell命令过程,这反过来会对我的文件进行一些清理;完成后,使用OS X通知中心向我发送通知(使用命令osascript -e'显示通知...)。如果有人(例如我的妻子或我的女儿)将计算机的当前用户切换到她,让我留在后台,则cron脚本在发送通知时会失败。
So, Who is the current user means Has some other people become the effective user leaving me in the background? Do stat -f "%Su" /dev/console
returns the current active user name?
那么,谁是当前的用户意味着是否有其他人成为让我留在后台的有效用户? stat -f“%Su”/ dev / console是否返回当前活动用户名?
The answer is yes; so, now my crontab shell script has been modified in the following way:
答案是肯定的;所以,现在我的crontab shell脚本已经按以下方式修改:
...
if [ "$(/usr/bin/stat -f ""%Su"" /dev/console)" = "loreti" ]
then /usr/bin/osascript -e \
'display notification "Cleanup done" sound name "sosumi" with title "myCleanup"'
fi
#8
Define 'active user'.
定义“活跃用户”。
If the question is 'who is the logged in user', then 'who am i' or 'whoami' is fine (though they give different answers - 'whoami' reports just a user name; 'who am i' reports on terminal and login time too).
如果问题是“谁是登录用户”,那么'我是谁'或'whoami'是好的(虽然他们给出了不同的答案 - 'whoami'仅报告用户名;'我是谁'报告终端和登录时间也是如此)。
If the question is 'which user ID is the effective ID for the shell', then it is often better to use 'id'. This reports on the real and effective user ID and group ID, and on the supplementary group IDs too. This might matter if the shell is running SUID or SGID.
如果问题是'哪个用户ID是shell的有效ID',那么通常最好使用'id'。它会报告真实有效的用户ID和组ID,以及补充组ID。如果shell运行SUID或SGID,这可能很重要。
#9
getting username in MAC terminal is easy...
在MAC终端获取用户名很容易......
I generally use whoami
in terminal...
我一般在终端使用whoami ...
For example, in this case, I needed that to install Tomcat Server...
例如,在这种情况下,我需要安装Tomcat Server ...
#10
You can also use the logname
command from the BSD General Commands Manual under Linux or MacOS to see the username of the user currently logged in, even if the user is performing a sudo
operation. This is useful, for instance, when modifying a user's crontab while installing a system-wide package with sudo: crontab -u $(logname)
您还可以使用Linux或MacOS下的BSD General Commands Manual中的logname命令查看当前登录用户的用户名,即使用户正在执行sudo操作也是如此。这很有用,例如,在使用sudo安装系统范围的软件包时修改用户的crontab:crontab -u $(logname)
Per man logname
:
每个人的日志名称:
LOGNAME(1)
NAME
logname -- display user's login name
#11
You can also retrieve it from the environment variables, but that is probably not secure, so I would go with Andrew's answer.
你也可以从环境变量中检索它,但这可能不安全,所以我会选择Andrew的答案。
printenv USER
If you need to retrieve it from an app, like Node, it's easier to get it from the environment variables, such as
如果您需要从应用程序(如Node)中检索它,则可以更轻松地从环境变量中获取它,例如
process.env.USER
.