3

How to get a list of usernames assigned to a group in FreeBSD 11.1?

This Question asks for a list of users or a list of groups.

This Question asks for users who happen to be members of multiple specific groups.

➥ But how to ask simply for a list of users currently assigned to a single specific group, such wheel?

I prefer a solution in a single command if possible. But if not feasible, a script would be useful and educational.

5
  • Is a moderately complicated script parsing /etc/passwd and /etc/groups OK, or do you want a system command?
    – Law29
    Dec 2, 2018 at 21:52
  • @Law29 Either, or both. I added note in Question. Dec 2, 2018 at 21:57
  • 2
    Why not just getent group groupname? Dec 2, 2018 at 22:02
  • @NasirRiley Can you show example? Dec 2, 2018 at 22:03
  • @BasilBourque It's just like I typed it. Replace groupname with the group that you want. Dec 2, 2018 at 22:12

5 Answers 5

5

You can select users with specific group from list of all users given by pw utility:

pw usershow -P -a | grep groupname

Result will be something like:

Login Name: username1     #1024         Group: groupname          #1002
Login Name: username2     #1025         Group: groupname          #1002
1
  • 1
    This is useful because it indicates the pw utility. No idea why it was downvoted (and my three-month-old answer got just downvoted too).
    – Law29
    Mar 21, 2019 at 16:51
2

The proper way to show all users in a certain group under FreeBSD is:

# pw groupshow groupname
1

Disregarding system scripts like getent, users can be in a group in two ways: either it is their principal group, defined in /etc/passwd, or they have it as a secondary group in /etc/groups.

GROUPID=1000
sed -rn "s/^[^:]*:[^:]*:$GROUPID://p" < /etc/group | tr ',' '\n'
sed -rn "s/^([^:]*):[^:]*:[^:]*:$GROUPID:.*/\\1/p" < /etc/passwd

Should do it nicely. awk would probably look nicer...

GROUPID=1000
awk -F: -v "g=$GROUPID" '{if ($3==g) print $1;}' /etc/passwd
awk -F: -v "g=$GROUPID" '{if ($3==g) print $4;}' /etc/group | tr ',' '\n'
0

https://www.cyberciti.biz/faq/linux-list-all-members-of-a-group/ thread show multiple ways of doing this. I personally prefer members method. but the method below is native and works well.

$ grep 'grpup-name-here' /etc/group
$ grep 'ftponly' /etc/group
$ grep -i --color 'ftponly' /etc/group
0

pw group show ftpgroup

ftpgroup:*:2001:

pw group show wheel

wheel:*:0:root,fred,carl

pw group show video

video:*:44:lightdm

pw user show ftp

ftp:*:2002:2001::0:0:PureFTP user:/nonexistent:/usr/sbin/nologin

pw user show carl

carl:$6$SDkXHtJyIp53egpl$XBg3cuem.RLW63Pa0E16Qw4hmc96lXFgC/fTwCSnDEgYE/OhairDv.mX193ToC6PaQfsp.NegsqEYMHSMxinE/:1003:1003::0:0:Carl Dubak:/home/carl:/usr/local/bin/fish

pw user show fred

fred:$6$alWrCEOtGuU1Bx7t$oWXCNkP4t4yQVn5.0ZKuJqAtCNXkR5ywcdI0Guxqv3myDI.u/qrqUzBX7BW4jmwLFHDd00tCdyGt3A9Q6T4s4/:1002:1002::0:0:Fred Finster:/home/fred:/usr/local/bin/fish

getent passwd {carl}

carl:$6$SDkXHtJyIp53egpl$XBg3cuem.RLW63Pa0E16Qw4hmc96lXFgC/fTwCSnDEgYE/OhairDv.mX193ToC6PaQfsp.NegsqEYMHSMxinE/:1003:1003:Carl Dubak:/home/carl:/usr/local/bin/fish

getent passwd {fred}

fred:$6$alWrCEOtGuU1Bx7t$oWXCNkP4t4yQVn5.0ZKuJqAtCNXkR5ywcdI0Guxqv3myDI.u/qrqUzBX7BW4jmwLFHDd00tCdyGt3A9Q6T4s4/:1002:1002:Fred Finster:/home/fred:/usr/local/bin/fish

id

id=0(root) gid=0(wheel) groups=0(wheel),5(operator)

id 1003

uid=1003(carl) gid=1003(carl) groups=1003(carl),0(wheel),5(operator)

id 1002

uid=1002(fred) gid=1002(fred) groups=1002(fred),0(wheel),5(operator),145(webcamd)

id 5

uid=5(kmem) gid=65533(nogroup) groups=65533(nogroup)

id {operator}

uid=2(operator) gid=5(operator) groups=5(operator)

You must log in to answer this question.

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