Linux current user and group

Show all users and their groups/vice versa

I know I have existing groups and users but I’m not sure about their association. Is there an shell command I can use to list all users or all groups and a command to list all groups/users for a specified user/group? So something like showusers would list all users, and showgroups -u thisuser would show all the groups that have thisuser in it.

8 Answers 8

All groups with a specific user:

$ getent group | grep username 

I found that there is a user named speech-dispatcher that belongs to group audio (based on groups speech-dispatcher ). But it is not listed under getent group command! What is the problem?

+1 since this will also list users/groups not found in the conventional /etc/passwd & /etc/group files i.e. when a system is configured to use central directories such NIS and LDAP, or any other alternative user/group database, as long as that supports user/group enumeration.

List users and their groups:

for user in $(awk -F: '' /etc/passwd); do groups $user; done 

List groups and their users:

cat /etc/group | awk -F: '' | while read group gid members; do members=$members,$(awk -F: "\$4 == $gid " /etc/passwd); echo "$group: $members" | sed 's/,,*/ /g'; done 

While that would probably work, it seems a bit overly complicated, doesn’t it, when there are perfectly good simple one-shot commands to do this?

Читайте также:  Linux консоль имена файлов

It certainly wouldn’t get anything that lives in a centralized repository. And that’s definitely information that you’d want to see.

If you dont care about remote users such as LDAP or NIS, to list users and their associated groups in a simple way:

cut -d: -f1 /etc/passwd | xargs groups 
root : root myuser : root www-data fuse anotheruser : anotheruser cdrom floppy audio dip video plugdev scanner bluetooth netdev 

This has the same problem as Chang’s answer in that it ignores users/groups originating in databases such as LDAP, NIS, etc.

This very neatly outputs the information in an incredibly clear format though, so can still be a useful first step. It helped jog my memory regarding the syntax of /etc/group and /etc/passwd!

List all users

While cat /etc/passwd shows all users (and a bunch of other stuff), cut -d ‘:’ -f 1 is a simple way to split each line with ‘:’ as a delimiter and extract just the first field (users). Pretty much the same as awk version.

List all groups

Guess what, very simmilar to listing users. Just parse /etc/group instead.

Another interesting way, maybe closer to what OP wanted, is compgen. Not sure about compatibility issues though.

Hi Elliot Baily, welcome to Server Fault! Please note that this question is over 5 years old, and already has a correct and accepted answer. Also note that your solution only works if users are stored in /etc/passwd; the accepted answer also works for other user databases (such as NIS or LDAP). If you want to answer old questions (which is perfectly fine!) you might want to have a look at the list of unanswered questions — plenty of questions looking for some love!

for user in $(getent passwd |awk -F: ''); do groups $user; done 
cat /etc/passwd # show all users cat /etc/group # show all groups cat /etc/passwd | grep group # show all users with specified group 

In contrast with the already accepted this does not list users/groups that originate from a remote user database such as LDAP, NIS etc.

Читайте также:  Где лежат исполняемые файлы linux

Источник

How to find out what group a given user has?

This appears to be pretty useful as well. It has more verbose output than the ‘groups’ command, so if you need the group id/user id use this!

On Linux/OS X/Unix to display the groups to which you (or the optionally specified user) belong, use:

which is equivalent to groups [user] utility which has been obsoleted on Unix.

On OS X/Unix, the command id -p [user] is suggested for normal interactive.

Explanation on the parameters:

-G , —groups — print all group IDs

-n , —name — print a name instead of a number, for -ugG

-p — Make the output human-readable.

or just study /etc/groups (ok this does probably not work if it uses pam with ldap)

Below is the script which is integrated into ansible and generating dashboard in CSV format.

sh collection.sh #!/bin/bash HOSTNAME=`hostname -s` for i in `cat /etc/passwd| grep -vE "nologin|shutd|hal|sync|root|false"|awk -F':' '' | sed 's/[[:space:]]/,/g'`; do groups $i; done|sed s/\:/\,/g|tr -d ' '|sed -e "s/^/$HOSTNAME,/"> /tmp/"$HOSTNAME"_inventory.txt sudo cat /etc/sudoers| grep -v "^#"|awk ''|grep -v Defaults|sed '/^$/d;s/[[:blank:]]//g'>/tmp/"$HOSTNAME"_sudo.txt paste -d , /tmp/"$HOSTNAME"_inventory.txt /tmp/"$HOSTNAME"_sudo.txt|sed 's/,[[:blank:]]*$//g' >/tmp/"$HOSTNAME"_inventory_users.txt 

My output stored in below text files.

cat /tmp/ANSIBLENODE_sudo.txt cat /tmp/ANSIBLENODE_inventory.txt cat /tmp/ANSIBLENODE_inventory_users.txt 

Источник

How can I display all users and groups with a command?

users and groups commands display users currently logged in, and groups a user belongs to respectively.

How to display a list of all users and all groups by command-line?

2 Answers 2

You can display with the help of compgen builtin command as follows:

    To display all users run following command:

Читайте также:  Автозапуск linux init d

However you can also display all users by cut -d «:» -f 1 /etc/passwd .

Nice! it might be preferable to use getent passwd / getent group instead of cat’ing the local files ( getent should work for non-local accounts as well)

Well, on my ubuntu, I have some files created by docker mount with 999:999 as user:group , but unfortunately none of the above commands prints them.

Here we are going to use getent for the detailed the info

We can list the user with the following command:

We can list the group as follows:

To fetch detail a specific user

Replace the lalit with your user name. Lalit will not be in every system 🙂

You can read the more into about getent here

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529

Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector