149

How do I get the name of the active user via the command line in OS X?

Redwood
  • 66,744
  • 41
  • 126
  • 187

13 Answers13

302

as 'whoami' has been obsoleted, it's probably more forward compatible to use:

id -un
kent
  • 6,286
  • 4
  • 27
  • 32
39

If you'd like to display the full name (instead of the username), add the -F flag:

$ id -F
Andrew Havens
Andrew
  • 227,796
  • 193
  • 515
  • 708
31

I'm pretty sure the terminal in OS X is just like unix, so the command would be:

whoami

I don't have a mac on me at the moment so someone correct me if I'm wrong.

NOTE - The whoami utility has been obsoleted, and is equivalent to id -un. It will give you the current user

jvarela
  • 3,744
  • 1
  • 22
  • 43
Eric Koslow
  • 2,004
  • 2
  • 21
  • 33
20

whoami

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.

dfa
  • 114,442
  • 31
  • 189
  • 228
14

Via here

Checking the owner of /dev/console seems to work well.

stat -f "%Su" /dev/console

Redwood
  • 66,744
  • 41
  • 126
  • 187
  • 3
    This works great if you are ssh'ed into a machine and want to see if a user is logged on locally. If they are not, the command will return "root". – Tim Dearborn Jan 15 '16 at 01:17
  • 1
    This is an underrated answer. You cannot do `sudo id -un` to get the name of the logged-in user (as `id` is more "what is the id of the user who's running the `id` command). That makes `id` great for "what user is running my script", but not for admin duties. – joshfindit Feb 20 '21 at 16:09
6

There are two ways-

whoami

or

echo $USER
manas sharma
  • 463
  • 1
  • 6
  • 8
5

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)

Per man logname:

LOGNAME(1)

NAME
    logname -- display user's login name
Adam Link
  • 2,783
  • 4
  • 30
  • 44
  • 1
    This is definitely the cleanest and easiest solution. It works no matter what user is executing the command. – Maxime Feb 21 '19 at 00:57
4

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.)

There may be multiple login sessions; UNIX is designed to be a multi-user system, after all.

ephemient
  • 198,619
  • 38
  • 280
  • 391
2

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.

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?

The answer is yes; so, now my crontab shell script has been modified in the following way:

...
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
2

getting username in MAC terminal is easy...

I generally use whoami in terminal...

For example, in this case, I needed that to install Tomcat Server...

enter image description here

Alireza
  • 100,211
  • 27
  • 269
  • 172
1

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).

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

You can also retrieve it from the environment variables, but that is probably not secure, so I would go with Andrew's answer.

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

process.env.USER.

What Would Be Cool
  • 6,204
  • 5
  • 45
  • 42
0

you can open terminal and write down following command:

id -un

or

whoami

This will return your current login username.

Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52