1

How do I view a list of users in a specific group and their passwords or attributes?

I know to use

cut -d: -f1 /etc/passwd

But this only lists users.

1

If you want to list users whose primary group is somegroup:

gid=$(getent group somegroup | awk -F : '{print $3}')
getent passwd | awk -F : -v gid="$gid" '$4 == gid'

This answer assumes a non-embedded Linux as it relies on the Glibc getent utility.

You can configure the fields you want in the output by tweaking the second awk command. For example, to print their real name and phone number only:

getent passwd | awk -F : -v gid="$gid" '$4 == gid {print $5}'

If you want to list users who have somegroup as a supplementary group, you need to first retrieve the user IDs from the group database.

usernames=$(getent group somegroup | awk -F : '{print $3 "," $4}')
gid=${usernames%%,*}; usernames=${usernames#*,}
getent passwd | awk -F : -v gid="$gid" -v usernames="$usernames" '
    BEGIN {split(usernames, names, /,/); for (name in names) user[name]=1}
    name[$1] || $4==gid
'

You can't print out users' passwords because they are not stored. You can print their password hash if you have the permission to access it. If your password database is local then password hashes are in the file /etc/shadow. If you get a list of usernames and other fields, you can join that with the shadow file:

… | awk … | join -t : -j 1 - /etc/shadow
0
#!/bin/bash

cut -d : -f 1 /etc/passwd | while read user
do
    # check if user is in specific group
    `groups $user` | grep -q <SPECIFICGROUP>
    if [ $? -ne 0 ] ; then
        continue;
    fi

    printf "User=${user},password=`awk -F: -v user=$user '$1 == user { print $2 ;} '`"
done

edit: does not handle the case if a username is the prefix of another username

  • replace cat /etc/shadow | grep $user by awk -F: -v user=$user '$1 == user { print $2 ;} ' – Archemar Mar 4 '15 at 10:48
  • nice, didn't know about that awk feature... – user236012 Mar 4 '15 at 11:31
  • ayyyy lmaoooooo – ayy lmao Mar 4 '15 at 11:36
0

Below is the command we can get users details in group and user password

#!/bin/bash
echo "enter the groupname"
read g
for user in `getent group $g| awk -F ":" '{print $NF}'| sed "s/,/ /g"`
do
echo $user
sed -n '/'$user'/p' /etc/passwd
done| sed '1i below are list of users in group and users password'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.