How To: Get the currently logged in user, in a more Apple approved way

NOTE: Due to macOS Monterey's prompts when Python 2 in invoked, please review the blog post here to get the logged in user instead. 

When running scripts via Jamf Pro, Munki or a launch daemon, you sometimes have a challenge to get the username of the currently logged in user.

Below has a method to achieve this, with a link to an Apple approved method.

The challenge

If you’re running a script via Jamf Pro at login or via Self Service, then $3 can be used to grab the username.

However, what if you need to check at another time or you don’t have Self Service?

A common method used is below & you’ll find it in a number of my scripts.

loggedInUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')

The recommendation

On seeing an example of the above, Frogor over at the ##OSX-Server IRC mentioned that this method was both a little “odd” & not Apple’s recommended way. Then, being the guy he is.. He not only pointed us in IRC to a link about Apple’s recommended method, but also gave an example of how to call it within bash. That example is below:

loggedInUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')

You’ll start to see the above soon in my new posts, & if you do use Fast User Switching then this may work better for you where other methods have failed.

24 thoughts on “How To: Get the currently logged in user, in a more Apple approved way

    • Whilst that works, it’s only as good as your last recon & depends on when that is.

      The above can be run as part of a script whenever.

  1. jaknz

    Best Practice is also to use $(do stuff here) instead of `do stuff here`. If that got reformatted, replace the back-ticks surrounding the python code with a dollar+open parenthesis and end the block with a close parenthesis.

  2. James Brickley

    Make sure specify /usr/bin/python:
    `/usr/bin/python -c ‘from SystemConfiguration…

    Specify the full path to python or it might use a /usr/local/bin/python and not load the modules that Apple provides with their customized python.

  3. What’s odd is the Technical Q&A link states:

    – It has no way of indicating that multiple users are logged in.

    – It has no way of indicating that a user is logged in but has switched to the login window.

    Perhaps I’m misunderstanding?

    • Forgot to add the line from the Q&A: “Because of these issues, routines like SCDynamicStoreCopyConsoleUser exist only for compatibility purposes. It’s likely that they will be formally deprecated in a future version of Mac OS X.”

  4. Randy

    This worked excellent!
    Wondering if there is a method to AWK out the first name instead of the entire name.
    So it would say “Welcome John”. Instead of “Welcome John Smith” or “Welcome Johns MacBook Air”
    I am no scripter by far, so I relay on the community to fill in what I do not know.
    Thanks People.

  5. Daniel

    This may be deprecated as Catalina 10.15+ approaches, and Python may be no longer be included in macOS.

  6. Let’s all put a nail in the coffin of the Python method and agree to use this elegant stat based method:
    consoleUser=$(stat -f %Su /dev/console)

    It’s so nice and compact.
    It works with fast user switching.
    You can even commit it to memory!

    At the login screen it will return “root”, the Python method will not return anything.
    It’s literally ~130x faster since it doesn’t have the overhead: Python=.528s, stat=.004s

Leave a Reply to GrahamCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.