Note that there are some explanatory texts on larger screens.

plurals
  1. POList of all users and groups
    text
    copied!<p>I'm trying to get a list of <strong>all</strong> users and all groups on Mac OS X 10.5+. How can I do this?</p> <p>For example, the list of all users on my machine should return: <code>_amavisd, _appowner, _appserver, _ard, _atsserver, _calendar, _carddav, _clamav, _coreaudiod, _cvmsroot, _cvs, _cyrus, _devdocs, _dovecot, _eppc, _installer, _jabber, _lda, _locationd, _lp, _mailman, _mcxalr, _mdnsresponder, _mysql, _pcastagent, _pcastserver, _postfix, _qtss, _sandbox, _screensaver, _securityagent, _serialnumberd, _softwareupdate, _spotlight, _sshd, _svn, _teamsserver, _timezone, _tokend, _trustevaluationagent, _unknown, _update_sharing, _usbmuxd, _uucp, _windowserver, _www, _xgridagent, _xgridcontroller, daemon, dave, nobody, root</code> (that was painstakingly compiled manually).</p> <p>How can I get that list (and the corresponding list of all groups) programmatically? I'm open to alternative (non-c based) solutions, such as Applescript, commandline, etc.</p> <hr> <p><strong>Update a long time later</strong></p> <p><a href="https://stackoverflow.com/a/1308064/115730">TALlama's answer</a> prompted me to investigate the API to Open Directory, and I found that this list can be easily acquired programmatically:</p> <pre><code>#import &lt;OpenDirectory/OpenDirectory.h&gt; ODSession *s = [ODSession defaultSession]; ODNode *root = [ODNode nodeWithSession:s name:@"/Local/Default" error:nil]; ODQuery *q = [ODQuery queryWithNode:root forRecordTypes:kODRecordTypeUsers attribute:nil matchType:0 queryValues:nil returnAttributes:nil maximumResults:0 error:nil]; NSArray *results = [q resultsAllowingPartial:NO error:nil]; for (ODRecord *r in results) { NSLog(@"%@", [r recordName]); } </code></pre> <p>That will log the usernames of every user on the system. Substituting in <code>kODRecordTypeGroups</code> will get you the list of all the groups.</p> <p>The <code>-[ODQuery resultsAllowingPartial:error:]</code> method is a <em>blocking</em> call, so you'd either want to execute this code on a background thread, or use an <code>&lt;ODQueryDelegate&gt;</code> to aggregate the results.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload