Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Trying to find out if a particular user is logged into the machine, specifically the user using the graphical user interface.

Is this possible via command line?

share|improve this question
Wait GUI and command line? Both? Or just command line? I'm confused. – Vervious Aug 26 '10 at 2:46
@Nano8Blazex, made the question more clear for you. – Gerald Kaszuba Aug 26 '10 at 3:10
:D that's great! – Vervious Aug 26 '10 at 4:29
up vote 15 down vote accepted


  • Open the Accounts preference pane in System Preferences. The pre-selected user account will be the active user account.
  • If fast user switching is active its menu extra (the menu on the right side of the menu bar) can be configured to show the name of the active user.

Command Line:

  • Check the owner of /dev/console

    stat -f '%u %Su' /dev/console
  • Write a program that uses the official API (SCDynamicStoreCopyConsoleUser; see below)

In a C program:

The C code in Technical Q&A QA1133: Determining console user login status shows how to determine which user owns the active GUI session.

For example:

/* Adapted from QA1133:
#include <assert.h>
#include <SystemConfiguration/SystemConfiguration.h>

int main(int argc, char **argv) {
    SCDynamicStoreRef store;
    CFStringRef name;
    uid_t uid;
#define BUFLEN 256
    char buf[BUFLEN];
    Boolean ok;

    store = SCDynamicStoreCreate(NULL, CFSTR("GetConsoleUser"), NULL, NULL);
    assert(store != NULL);
    name = SCDynamicStoreCopyConsoleUser(store, &uid, NULL);

    if (name != NULL) {
        ok = CFStringGetCString(name, buf, BUFLEN, kCFStringEncodingUTF8);
        assert(ok == true);
    } else {
        strcpy(buf, "<none>");

    printf("%d %s\n", uid, buf);

    return 0;
share|improve this answer
Nice accepted answer steal :) – Gerald Kaszuba Aug 28 '10 at 4:36
See manpage getlogin. – Itachi Dec 24 '14 at 11:32

Via the command line, who and users should work.

share|improve this answer
Ah, who and look for "console". Thanks. – Gerald Kaszuba Aug 26 '10 at 3:09
@Gerald: Using who is not accurate on my 10.4 system when using fast user switching (the system I am using right now shows another user on “console” even though my GUI session is the active one). Maybe it is more reliable in newer versions. – Chris Johnsen Aug 26 '10 at 5:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .