Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you know who is the active user when using Terminal Mac?

See Users with Activity Monitor in Mac OS X The simplest way to get basic user details is to use Activity Monitor from an Administrator user account. This will be inclusive, but the data is a bit limited for some uses as you'll see: Launch “Activity Monitor” in Mac OS X, found within /Applications/Utilities/

How can I see my username in CMD?

In the box, type cmd and press Enter. The command prompt window will appear. Type whoami and press Enter. Your current user name will be displayed.

Who am I command in Mac?

You can get into single user mode using Command+S while booting, then run ls /Users and it should show all the users which will give you a good idea of which one is you.


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

id -un

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

$ id -F
Andrew Havens

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.


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


Via here

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

stat -f "%Su" /dev/console


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.


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