I'm trying to get a list of all users and all groups on Mac OS X 10.5+. How can I do this?
我想要得到Mac OS X 10.5+上所有用户和所有组的列表。我该怎么做呢?
For example, the list of all users on my machine should return: _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
(that was painstakingly compiled manually).
例如,我的机器上所有用户的列表应该返回:_amkavisd, _appserver, _appserver, _atsserver, _carddav, _clamav, _coreaudiod, _cvmsroot, _cyrus, _devdocs, _dovecot, _eppc, _jabber, _jabber, _coreaudiod, _cvs_ms_ms_ms_ms_msroot, _dev, _dev, _dev, _dev, _dev, _web_dev_tw_tw_tech_tech_developmentd,_xgridagent、_xgridcontroller、守护进程、dave、nobody、root(这是手工编写的)。
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.
如何以编程方式获得该列表(以及所有组的相应列表)?我对其他(非基于c的)解决方案持开放态度,比如Applescript、commandline等。
Update a long time later
很久以后更新
TALlama's answer prompted me to investigate the API to Open Directory, and I found that this list can be easily acquired programmatically:
塔拉玛的回答促使我调查了打开目录的API,我发现这个列表很容易通过编程获得:
#import <OpenDirectory/OpenDirectory.h>
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]);
}
That will log the usernames of every user on the system. Substituting in kODRecordTypeGroups
will get you the list of all the groups.
这将记录系统中每个用户的用户名。代入柯达记录类型组将得到所有组的列表。
The -[ODQuery resultsAllowingPartial:error:]
method is a blocking call, so you'd either want to execute this code on a background thread, or use an <ODQueryDelegate>
to aggregate the results.
方法是一个阻塞调用,所以您要么希望在后台线程上执行此代码,要么使用
5 个解决方案
#1
87
The tool you want is almost certainly dscl. The shortest way to do it was already pointed out:
您需要的工具几乎肯定是dscl。最短的办法已经指出:
$ dscl . list /users
$ dscl . list /groups
If you want to output information about each user, though, use readall
:
如果您想要输出关于每个用户的信息,请使用readall:
$ dscl . readall /users
$ dscl . readall /groups
And if you need to programatically parse said information, use -plist to make your life easier:
如果你需要程序化地解析这些信息,使用-plist使你的生活更简单:
$ dscl -plist . readall /users
$ dscl -plist . readall /groups
#2
12
Open Directory approach (from: http://rickcogley.blogspot.com/2008/11/listing-open-directory-users-on-os-x.html):
打开目录方法(来自:http://rickcogley.blogspot.com/2008/11/listing-open-directory-users-on-os-x.html):
dscacheutil -q user
dscacheutil -q group
Take each line from the respective output that starts with "name:" strip off the "name:" and you have your list. If you do not have dscacheutil, you can use the manual commands:
从以“name:”开头的相应输出中提取每一行,去掉“name:”,就得到了列表。如果您没有dscacheutil,您可以使用手动命令:
root# dscl localhost list /Local/Default/Users
root# dscl localhost list /LDAPv3/127.0.0.1/Users
Old school approach for before Open Directory....(sigh): For list of users:
老式的方法之前开的目录....(叹气):用户列表:
- Grab the /etc/passwd file from the system.
- 从系统中获取/etc/passwd文件。
- Split it out by lines
- 把它分成几行
- Split out each line based on ":"
- 根据“:”将每行分开
- Take the first symbol for each line
- 取每行的第一个符号
For list of groups:
组列表:
- Grab the /etc/group file from the system.
- 从系统中获取/etc/group文件。
- Split it out by lines
- 把它分成几行
- Split out each line based on ":"
- 根据“:”将每行分开
- Take the first symbol for each line
- 取每行的第一个符号
#3
8
Non-garbbled/no-tempfile commands:
Non-garbbled / no-tempfile命令:
# dscl . list /users
# dscl . list /groups
#4
1
check out, for example, dsexport.
例如,查看dsexport。
Here are some examples:
下面是一些例子:
dsexport /tmp/export.out /Local/Default dsRecTypeStandard:Groups
dsexport /tmp/export.out /Local/Default dsRecTypeStandard:Users
the outputs are a bit rubbish, but something like sed could clean them up for you.
输出有点垃圾,但是像sed这样的东西可以帮你清理。
#5
1
Back in the old days, we'd do this trivially with the NetInfo Kit, but today there's no tidy Objective-C way to do it. You'll have to dig in to the OpenDirectory API.
在过去,我们会用NetInfo工具包做这个简单的事情,但是现在还没有一个干净的Objective-C方法。您将不得不深入到OpenDirectory API。
#1
87
The tool you want is almost certainly dscl. The shortest way to do it was already pointed out:
您需要的工具几乎肯定是dscl。最短的办法已经指出:
$ dscl . list /users
$ dscl . list /groups
If you want to output information about each user, though, use readall
:
如果您想要输出关于每个用户的信息,请使用readall:
$ dscl . readall /users
$ dscl . readall /groups
And if you need to programatically parse said information, use -plist to make your life easier:
如果你需要程序化地解析这些信息,使用-plist使你的生活更简单:
$ dscl -plist . readall /users
$ dscl -plist . readall /groups
#2
12
Open Directory approach (from: http://rickcogley.blogspot.com/2008/11/listing-open-directory-users-on-os-x.html):
打开目录方法(来自:http://rickcogley.blogspot.com/2008/11/listing-open-directory-users-on-os-x.html):
dscacheutil -q user
dscacheutil -q group
Take each line from the respective output that starts with "name:" strip off the "name:" and you have your list. If you do not have dscacheutil, you can use the manual commands:
从以“name:”开头的相应输出中提取每一行,去掉“name:”,就得到了列表。如果您没有dscacheutil,您可以使用手动命令:
root# dscl localhost list /Local/Default/Users
root# dscl localhost list /LDAPv3/127.0.0.1/Users
Old school approach for before Open Directory....(sigh): For list of users:
老式的方法之前开的目录....(叹气):用户列表:
- Grab the /etc/passwd file from the system.
- 从系统中获取/etc/passwd文件。
- Split it out by lines
- 把它分成几行
- Split out each line based on ":"
- 根据“:”将每行分开
- Take the first symbol for each line
- 取每行的第一个符号
For list of groups:
组列表:
- Grab the /etc/group file from the system.
- 从系统中获取/etc/group文件。
- Split it out by lines
- 把它分成几行
- Split out each line based on ":"
- 根据“:”将每行分开
- Take the first symbol for each line
- 取每行的第一个符号
#3
8
Non-garbbled/no-tempfile commands:
Non-garbbled / no-tempfile命令:
# dscl . list /users
# dscl . list /groups
#4
1
check out, for example, dsexport.
例如,查看dsexport。
Here are some examples:
下面是一些例子:
dsexport /tmp/export.out /Local/Default dsRecTypeStandard:Groups
dsexport /tmp/export.out /Local/Default dsRecTypeStandard:Users
the outputs are a bit rubbish, but something like sed could clean them up for you.
输出有点垃圾,但是像sed这样的东西可以帮你清理。
#5
1
Back in the old days, we'd do this trivially with the NetInfo Kit, but today there's no tidy Objective-C way to do it. You'll have to dig in to the OpenDirectory API.
在过去,我们会用NetInfo工具包做这个简单的事情,但是现在还没有一个干净的Objective-C方法。您将不得不深入到OpenDirectory API。