How can I display all users and groups with a command?
Solution 1
You can display with the help of compgen
builtin command as follows:
-
To display all users run following command:
compgen -u
-
To display all groups run following command:
compgen -g
However you can also display all users by cut -d ":" -f 1 /etc/passwd
.
Solution 2
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
Related videos on Youtube
Pandya
Started using Linux and StackExchange since Ubuntu 12.04 LTS. Then Upgraded to 14.04 LTS. Now I am using Debian GNU/Linux on my Laptop and PureOS on old Desktop computer. I recommend visiting the Philosophy of GNU Project As I've replaced Ubuntu with Debian GNU/Linux, Now my question(s) are became off-topic on AskUbuntu. So, I continue to Unix & Linux. The second reason for my shifting to U & L is I found U & L more interesting than AU since AU is only Ubuntu specific whereas U & L is a broad concept and in my opinion U & L deserves generic questions. (I know why SE has AU & U & L both).
Updated on September 18, 2022Comments
-
Pandya over 1 year
I want to display:
All users and
All groups
in my system using command-line.
users
andgroups
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?
-
steeldriver over 9 yearsNice! 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) -
muru over 9 years@steeldriver
compgen
does seem to work for non-local accounts (at least for LDAP). -
steeldriver over 9 years@muru I was referring to the second method specifically (
cat /etc/passwd | cut -d ...
) -
muru over 9 years@steeldriver Ah, sorry. I thought that was a more general observation.
-
Marinos An about 5 yearsWell, on my ubuntu, I have some files created by docker mount with
999:999
asuser:group
, but unfortunately none of the above commands prints them.