3

Using either Java or bash how can I list all possible owners and groups.

For instance if I was to do the following command in a shell with an obviously wrong owner:

chown ljafdj ~/Desktop/test.txt

My output would be

chown: invalid user: `ljafdj'

The same would be for chgrp

So how can I get a list of valid users using bash or Java?

It's for an Android application and I need to display the possibilities to my user so they can change if needed.

ekad
  • 14,436
  • 26
  • 44
  • 46
user577732
  • 3,956
  • 11
  • 55
  • 76
  • 1
    https://github.com/keesj/gomo/wiki/AndroidUserAndGroups – eboix Dec 31 '11 at 18:57
  • looks really promising thanks taking a look at it now – user577732 Dec 31 '11 at 18:58
  • Wait a second. Are you sure that you want the phone's hardcoded groups? Using AccountManager you can get their username@gmail.com and other accounts they have...... http://developer.android.com/reference/android/accounts/AccountManager.html#getAccounts() – eboix Dec 31 '11 at 19:10

2 Answers2

2

Groups in Android are hardcoded and "are used to isolate processes and grant permissions." However, if you want to get the online username, you can use AccountManager.getAccounts().

Community
  • 1
  • 1
eboix
  • 5,113
  • 1
  • 27
  • 38
1

Using the shell ("bash"):

For users:

getent passwd

For groups:

getent group

Just parse for the first field (before the colon) to get the actual names.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • Working on android and when i run this getent isn't found on my device and isn't part of busybox. I checked passwd which i found in /etc/passwd it only contains "root:x:0:0::/data/cron:/system/bin/sh and group doesn't exist in /etc any other ideas? thanks for the suggestion – user577732 Dec 31 '11 at 18:37
  • Have you had a look at `/etc/nsswitch.conf`? What does it define as valid sources for user names and groups? (that is, if such a file exists) – fge Dec 31 '11 at 18:40
  • Sorry - I didn't trust your "android" tag, given that it was the only mention of Android in the entire question. :-) – ziesemer Dec 31 '11 at 18:41
  • Sorry bout that should have been more descriptive about it being for android and @fge that file doesn't exist on my device gonna search for it on my device though but it's not at that path you gave – user577732 Dec 31 '11 at 18:46
  • I suggest you search for the way Android looks up its user names and groups: it appears it does not use the standard mechanics in use on Linux distributions. By chance, do you have an `/etc/pam.d` directory? Maybe looking in the files in this directory will give you hints... – fge Dec 31 '11 at 18:49
  • @fge nope that directory doesn't exist and yea i've been looking for a while and can't find how it looks up its user names been looking the past two days and finally broke down and decided to ask here ha – user577732 Dec 31 '11 at 18:53
  • @eboix system/core/include/private/android_filesystem_config.h doesn't seem to exist on my phone either :( Edit: read again and i see it's in libraries so i'm going to have to hardcode them and then read the user id of each app ugh ha but thanks a ton for the link – user577732 Dec 31 '11 at 19:00
  • who should i accept for this? ha i mean @eboix solved my issue but can't accept a comment gonna just accept ziesemer for now i guess – user577732 Dec 31 '11 at 19:31
  • Okay @user577732. I'll post my answer as an answer. With AccountManager? – eboix Dec 31 '11 at 19:31
  • @mice - Please refer to the above comments before down-voting. This was an appropriate answer at the time. (The question has since been edited.) I'd delete the answer, but there is useful information here (mostly in the comments) that should probably remain. – ziesemer Jan 31 '12 at 13:21