All users:
$ getent passwd
All groups:
$ getent group
All groups with a specific user:
$ getent group | grep username
Answer from EEAA on serverfault.comVideos
What is an example of chmod?
What is chmod 777 file permissions example?
All users:
$ getent passwd
All groups:
$ getent group
All groups with a specific user:
$ getent group | grep username
List users and their groups:
for user in $(awk -F: '{print $1}' /etc/passwd); do groups $user; done
List groups and their users:
cat /etc/group | awk -F: '{print
3, $4}' | while read group gid members; do
members=$members,
4 == $gid {print \",\" \$1}" /etc/passwd);
echo "$group: $members" | sed 's/,,*/ /g';
done
You can display with the help of compgen builtin command as follows:
To display all users run following command:
compgen -uTo display all groups run following command:
compgen -g
However you can also display all users by cut -d ":" -f 1 /etc/passwd.
Here we are going to use getent for the detailed the info
We can list the user with the following command:
getent passwd
We can list the group as follows:
getent group
To fetch detail a specific user
getent passwd lalit
Replace the lalit with your user name. Lalit will not be in every system :)
You can read the more into about getent here
In response to the comment by @Chris that
This only works for groups without spaces in the names!
I should mention that the accepted solution by @c4f4t0r and the solution by @bibi will work in most cases. I am running Cygwin, and the Windows part of the equation is probably why I hit this problem more often. Still, a group could be made with non-standard characters in normal Linux (I think), so I'll give the solution for group names with spaces. Spaces always make life fun!
I'll quickly give an example where spaces cause a problem. To see the group names with spaces, we look at the entire output of id
Copy$ id
uid=1(me) gid=545(Users)
groups=545(Users),66049(CONSOLE LOGON),11(Authenticated Users),
4095(CurrentSession),66048(LOCAL)
(Note: I did make the output a little more pleasing to the eye here on StackOverflow.)
From this, we see that the groups are {'Users', 'CONSOLE LOGON', 'Authenticated Users', 'CurrentSession', 'LOCAL'}
We can see the problem with the accepted solution in this case.
Copy$ echo "groups:" ; for i in $(id -Gn);do echo " - $i" ;done
groups:
- Users
- CONSOLE
- LOGON
- Authenticated
- Users
- CurrentSession
- LOCAL
A couple of groups get their names split up. To get the output we want, we need to use the id command, which takes a --zero ( -z ) flag. For more details on all the flags passed to id, see here.
Copy$ man id | grep -A 1 "\-\-zero"
-z, --zero
delimit entries with NUL characters, not whitespace;
Our approach will need to be a bit different to those given above, but follow a lot of the same principles:
Copy$ echo "groups:"; printf "%s" " - "; id -Gnz | \
awk 'BEGIN{FS="\0"; OFS="\n - "}{NF--; print}'
groups:
- Users
- CONSOLE LOGON
- Authenticated Users
- CurrentSession
- LOCAL
The reason that we have a slightly-more-complicated awk is that there is always a trailing NUL, which we do not want in this case. The \ allows me to continue onto the next line with the same command, making things easier to read. The command is equivalent to:
Copy$ echo "groups:"; printf "%s" " - "; id -Gnz | awk 'BEGIN{FS="\0"; OFS="\n - "}{NF--; print}'
How about this for a succinct solution that deals with spaces:
Copyid -Gnz | tr "\0" "\n"